简体   繁体   English

CreateProcessasuser-AccessViolationError

[英]CreateProcessasuser - AccessViolationError

I am trying to start a Gui tray application from a windows service (LocalSystem) using createProcessasUser - like so: 我正在尝试使用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());
        }
    }

I am using the function as so: 我这样使用该功能:

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

This actually worked for a little while, but in one of my runs it suddenly stopped working... giving the following error when executing the CreateProccessAsUser command (cant go any deeper) 这实际上工作了一段时间,但是在我的一次运行中,它突然停止工作...在执行CreateProccessAsUser命令时,出现以下错误(更深一点)

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

I have no idea why this is happening or even how to debug this any further, anyhow has any idea?? 我不知道为什么会这样,甚至不知道如何进一步调试它,无论如何也没有想法? because this doesnt make any sense to me. 因为这对我没有任何意义。

CreateProccessasuser definition: CreateProccessas用户定义:

[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);

Thanks 谢谢

Is your ProcessInformation type a value type (struct) or a reference type (class)? 您的ProcessInformation类型是值类型(结构)还是引用类型(类)?

Show its definition and the p/invoke declaration for CreateProcessAsUser . 显示其定义和CreateProcessAsUser的p / invoke声明。

BTW, all that GetLastWin32Error checking is done for you by p/invoke if you use the right attributes. 顺便说一句,如果您使用正确的属性,则通过p / invoke为您完成所有GetLastWin32Error检查。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM