【问题标题】:CreateProcessasuser - AccessViolationErrorCreateProcessasuser - AccessViolationError
【发布时间】:2012-01-09 17:18:04
【问题描述】:

我正在尝试使用 createProcessasUser 从 Windows 服务 (LocalSystem) 启动 Gui 托盘应用程序 - 如下所示:

    public static System.Diagnostics.Process StartProcessInSession(int sessionID, String commandLine)
    {
        IntPtr userToken;
        if (WTSQueryUserToken(sessionID, out userToken))
        {
            //note that WTSQueryUserToken only works when in context of local system account with SE_TCB_NAME
            IntPtr lpEnvironment;
            if (CreateEnvironmentBlock(out lpEnvironment, userToken, false))
            {
                StartupInfo si = new StartupInfo();
                si.cb = Marshal.SizeOf(si);
                si.lpDesktop = "winsta0\\default";
                si.dwFlags = STARTF.STARTF_USESHOWWINDOW;
                si.wShowWindow = ShowWindow.SW_SHOW;
                ProcessInformation pi;
                if (CreateProcessAsUser(userToken, null, new StringBuilder(commandLine), IntPtr.Zero, IntPtr.Zero, false, CreationFlags.CREATE_NEW_CONSOLE | CreationFlags.CREATE_UNICODE_ENVIRONMENT, lpEnvironment, null, ref si, out pi))
                {
                    CloseHandle(pi.hThread);
                    CloseHandle(pi.hProcess);
                    //context.Undo();
                    try
                    {
                        return System.Diagnostics.Process.GetProcessById(pi.dwProcessId);
                    }
                    catch (ArgumentException e)
                    {
                        //The process ID couldn't be found - which is what always happens because it has closed
                        return null;
                    }
                }
                else
                {
                    int err = Marshal.GetLastWin32Error();
                    throw new System.ComponentModel.Win32Exception(err, "Could not create process.\nWin32 error: " + err.ToString());
                }
            }
            else
            {
                int err = Marshal.GetLastWin32Error();
                throw new System.ComponentModel.Win32Exception(err, "Could not create environment block.\nWin32 error: " + err.ToString());
            }
        }
        else
        {
            int err = System.Runtime.InteropServices.Marshal.GetLastWin32Error();
            if (err == 1008) return null; //There is no token
            throw new System.ComponentModel.Win32Exception(err, "Could not get the user token from session " + sessionID.ToString() + " - Error: " + err.ToString());
        }
    }

我是这样使用函数的:

   protected override void OnStart(string[] args)
    {   
       _agentProcess = StartProcessInSession(WTSGetActiveConsoleSessionId(), "Some_correct_path");  
    }

这实际上工作了一段时间,但在我的一次运行中它突然停止工作......执行 CreateProccessAsUser 命令时出现以下错误(不能再深入了)

{"Attempted to read or write protected memory. This is often an indication that other memory is corrupt."}

我不知道为什么会发生这种情况,甚至不知道如何进一步调试,无论如何有什么想法吗?因为这对我没有任何意义。

创建进程作为用户定义:

[DllImport("advapi32.dll", SetLastError = true, CharSet = CharSet.Auto)]
        static extern bool CreateProcessAsUser(IntPtr hToken, String lpApplicationName, [In] StringBuilder lpCommandLine, IntPtr /*to a SecurityAttributes struct or null*/ lpProcessAttributes, IntPtr /*to a SecurityAttributes struct or null*/ lpThreadAttributes, bool bInheritHandles, CreationFlags creationFlags, IntPtr lpEnvironment, String lpCurrentDirectory, ref StartupInfo lpStartupInfo, out ProcessInformation lpProcessInformation);

谢谢

【问题讨论】:

  • lpEnvironment 是否设置为有效指针?而userToken 是一个有效的HANDLE
  • 你是如何定义CreateProcessAsUser的?
  • 两个值都在设置中,我将如何确定句柄是否有效?
  • @Menyh:基本是:不是0,不是-1,是4的倍数。除此之外,您还可以使用 Process Monitor 列出应用程序的有效句柄。

标签: c# windows winapi uac


【解决方案1】:

您的ProcessInformation 类型是值类型(结构)还是引用类型(类)?

显示其定义和CreateProcessAsUser 的 p/invoke 声明。

顺便说一句,如果您使用正确的属性,所有GetLastWin32Error 检查都是由 p/invoke 为您完成的。

【讨论】:

  • [StructLayout(LayoutKind.Sequential)] internal struct ProcessInformation { #region Data Members (4) public int dwProcessId;公共 int dwThreadId;公共 IntPtr hProcess;公共 IntPtr hThread; #endregion 数据成员 }
猜你喜欢
  • 2010-09-07
  • 2020-07-26
  • 2010-12-01
  • 1970-01-01
  • 1970-01-01
  • 2010-10-14
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多