开发者

CreateProcess in other desktop with lpDesktop

Update

The StartupInfomation struct need the ANSI charset.

Hallo,

I'm want to create a new desktop and start an process there. The creation of the desktop works probably. But if I create a process there by setting up the lpDesktop variable I get an error "The Application failed to initialize properly (0xc0000142)". I've already set the lpDesktop to "Default", "winsta0\Default" and to my other desktop name. Without changing the the lpDesktop var its starts the process inside my default desktop.

    public Process CreateProcess(string path, string commandline)
    {
        if (!IsCreated) return null;

        const uint normalPriorityClass = 0x0020;

        NativeMethods.ProcessInformation processInfo;开发者_如何学JAVA
        var startInfo = new NativeMethods.StartupInfomation();
        var pSec = new NativeMethods.SecurityAttributes();
        var tSec = new NativeMethods.SecurityAttributes();

        startInfo.cb = Marshal.SizeOf(startInfo);
        startInfo.lpDesktop = Name;
        startInfo.lpTitle = Name;

        pSec.nLength = Marshal.SizeOf(pSec);
        tSec.nLength = Marshal.SizeOf(tSec);

        var result = NativeMethods.CreateProcess(path, commandline,
                                    ref pSec, ref tSec, true, normalPriorityClass,
                                    IntPtr.Zero, null, ref startInfo, out processInfo);

        return !result ? null : Process.GetProcessById(processInfo.dwProcessId);
    }

    [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
    internal struct StartupInfomation
    {
        public Int32 cb;
        public string lpReserved;
        public string lpDesktop;
        public string lpTitle;
        public Int32 dwX;
        public Int32 dwY;
        public Int32 dwXSize;
        public Int32 dwYSize;
        public Int32 dwXCountChars;
        public Int32 dwYCountChars;
        public Int32 dwFillAttribute;
        public Int32 dwFlags;
        public Int16 wShowWindow;
        public Int16 cbReserved2;
        public IntPtr lpReserved2;
        public IntPtr hStdInput;
        public IntPtr hStdOutput;
        public IntPtr hStdError;
    }

string desktopName = "otherDesktop";
IntPtr Handle = NativeMethods.CreateDesktop(desktopName, IntPtr.Zero, IntPtr.Zero, 0,
                                         NativeMethods.AccessMask.GenericAll, IntPtr.Zero);
NativeMethods.SwitchDesktop(handle);
NativeMethods.SetThreadDesktop(handle);
CreateProcess(Environment.GetEnvironmentVariable("windir") + @"\explorer.exe", null);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜