开发者

C# STARTUPINFO Flags to show UI for a process started from a Service in XP

开发者 https://www.devze.com 2023-03-31 14:54 出处:网络
I\'m launching a process from a windows service in XP,开发者_如何学编程 I\'m just launching the process not trying to interact with it. The process starts but the UI does not show. I believe I need to

I'm launching a process from a windows service in XP,开发者_如何学编程 I'm just launching the process not trying to interact with it. The process starts but the UI does not show. I believe I need to set some flags in STARTUPINFO to make process visible, and hoping someone could show how and what flags to set.

    sPath = @"C:\Windows\notepad.exe";

    string Message = string.Empty;

    // Variables
    PROCESS_INFORMATION processInfo = new PROCESS_INFORMATION();
    STARTUPINFO startInfo = new STARTUPINFO();

    Boolean bResult = false;
    IntPtr hToken = IntPtr.Zero;

    try
    {
        // Logon user
        bResult = LogonUser(
            "Test",
            "VirtualXP-23639",
            "test",
            LogonType.LOGON32_LOGON_INTERACTIVE,
            LogonProvider.LOGON32_PROVIDER_DEFAULT,
            out hToken
        );
        if (!bResult) { throw new Exception("Logon error #" + Marshal.GetLastWin32Error()); }

        // Create process
        startInfo.cb = Marshal.SizeOf(startInfo);
        startInfo.lpDesktop = "winsta0\\default";

        bResult = CreateProcessAsUser(
            hToken,
            null,
            sPath,
            IntPtr.Zero,
            IntPtr.Zero,
            false,
            0,
            IntPtr.Zero,
            null,
            ref startInfo,
            out processInfo
        );

        if (!bResult)
        {
            Message = "Failed to Create Process on Desktop/Console.  Code=" + Marshal.GetLastWin32Error().ToString();
            Logging.LogError(Ascension.CM.Common.Enums.ApplicationModuleEnums.Service, Message, "Ascension.CM.ServiceWorker.ProcessLauncher.XpLaunchDesktopProcess", null);
        }


    }
    finally
    {
        // Close all handles
        CloseHandle(hToken);
        CloseHandle(processInfo.hProcess);
        CloseHandle(processInfo.hThread);
    }
}


You'll at least need to allow the service to interact with the desktop, so in services.msc, click on your serivce an go to properties, then logon and select allow to interact with desktop..


I would suggest that you use the Process class in the .net framework.

Process.Start("notepad.exe")

This should have your desired effect.


Thanks guys, but I've found a solution.

I ended up using WTSQueryUserToken to get the current logged in user and then used DuplicateTokenEx to get a token that I used with CreateProcessAsUser to start the process.

For XP use session id 0 and for win7 use WTSGetActiveConsoleSessionId to get the current session Id.

This works fine with out having to use the "Allow to interact with Desktop" property.

Thanks

0

精彩评论

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