C#中的CreateDesktop

时间:2014-06-25 13:06:14

标签: c# desktop

我正在尝试运行一个进程,但希望它被隐藏 - 所以我想,我想在不同的桌面上运行它。

public static void LaunchCommand2(string strCommand)
        {
            // Variables
            PROCESS_INFORMATION processInfo = new PROCESS_INFORMATION();
            STARTUPINFO startInfo = new STARTUPINFO();
            Boolean bResult = false;
            IntPtr hToken = IntPtr.Zero;
            UInt32 uiResultWait = WAIT_FAILED;
            IntPtr desktop = CreateDesktop("temp", IntPtr.Zero, IntPtr.Zero, 0, (long)DESKTOP_ACCESS_MASK.GENERIC_ALL, IntPtr.Zero);
            startInfo.lpDesktop = "temp";
            try
            {
                startInfo.cb = Marshal.SizeOf(startInfo);
                startInfo.dwFlags = STARTF_USESHOWWINDOW;
                startInfo.wShowWindow = 0;
                unsafe
                {
                    WTSQueryUserToken(WTSGetActiveConsoleSessionId(),out hToken);
                }

                bResult = Win32.CreateProcessAsUser(
                    hToken,
                    null,
                    strCommand,
                    IntPtr.Zero,
                    IntPtr.Zero,
                    false,
                    0,
                    IntPtr.Zero,
                    null,
                    ref startInfo,
                    out processInfo
                );
                if (!bResult) { throw new Exception("CreateProcessAsUser error #" + Marshal.GetLastWin32Error()); }

                // Wait for process to end
                uiResultWait = WaitForSingleObject(processInfo.hProcess, INFINITE);
                if (uiResultWait == WAIT_FAILED) { throw new Exception("WaitForSingleObject error #" + Marshal.GetLastWin32Error()); }
            }
            finally
            {
                // Close all handles
                CloseHandle(hToken);
                CloseHandle(processInfo.hProcess);
                CloseHandle(processInfo.hThread);
            }
        }

但它不起作用。我不知道我是否正确地将新桌面设置为startInfo。提前谢谢。

0 个答案:

没有答案