C# 使用lpDesktop在其他桌面中创建进程

C# 使用lpDesktop在其他桌面中创建进程,c#,C#,更新 StartupInformation结构需要ANSI字符集。 你好 我想创建一个新的桌面并在那里启动一个进程。 桌面的创建可能会起作用。 但是如果我通过设置lpDesktop变量在那里创建一个进程 我收到一个错误“应用程序未能正确初始化(0xc0000142)”。 我已经将lpDesktop设置为“默认”、“winsta0\Default”和 我的另一个桌面名称。在不更改LPVAR的情况下,它将启动 默认桌面中的进程 public Process CreateProcess(str

更新

StartupInformation结构需要ANSI字符集。

你好

我想创建一个新的桌面并在那里启动一个进程。 桌面的创建可能会起作用。 但是如果我通过设置lpDesktop变量在那里创建一个进程 我收到一个错误“应用程序未能正确初始化(0xc0000142)”。 我已经将lpDesktop设置为“默认”、“winsta0\Default”和 我的另一个桌面名称。在不更改LPVAR的情况下,它将启动 默认桌面中的进程

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



进程使用的DLL从其DllMain()入口点返回FALSE。为什么从你的问题中完全无法理解,你需要更多地了解这个过程本身。请与所有者联系以获得支持。@Hans Passant所有者是Microsoft“explorer.exe”^^^我已经看过许多源代码,相同的“更改桌面启动过程”是如何实现的。没什么帮助。我将在不同于Windows7的操作系统上尝试它。也许这是一个访问权限问题。你是通过服务来做这件事的吗?会话0隔离不允许您访问其他桌面。据我所知,没有办法做到这一点。
    [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);