使用lpDesktop在其他桌面上创建CreateProcess

时间:2011-08-07 18:30:16

标签: c#

更新

StartupInfomation结构需要ANSI字符集。

喂,

我想创建一个新桌面并在那里启动一个进程。 桌面的创建可能是有效的。 但是如果我通过设置lpDesktop变量来创建一个进程 我收到错误“应用程序无法正确初始化(0xc0000142)”。 我已经将lpDesktop设置为“Default”,“winsta0 \ Default”并将其设置为 我的其他桌面名称。不改变lpDesktop var它的开始 我的默认桌面内的进程。

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

        const uint normalPriorityClass = 0x0020;

        NativeMethods.ProcessInformation processInfo;
        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 个答案:

没有答案