exe启动时CreateProcessAsUser在启动

时间:2016-01-29 13:58:24

标签: c# .net windows-services createprocessasuser

情况如下:我有一个C#.NET Windows服务(作为本地系统运行),使用CreateProcessAsUser在当前登录的用户帐户下启动卫星C#.NET可执行文件(无窗口WinForms)。 p>

有时它有效,有时......它没有。我可以看到exe在任务管理器中出现了一秒钟,并且消失了。 exe的主要方法没有被击中,所以我认为问题来自CreateProcessAsUser。

事件查看器显示代码为0xc06d007e的应用程序错误,这没有任何帮助。还有一个Windows错误报告,它说要查看转储文件,但是从给定的路径中缺少这些文件。

我不知道如何调试它。如果至少我能看到真正的错误。

这完全是随机的。

编辑:

以下是一些问题。

public static uint? LaunchAsCurrentUser(string cmdLine)
{
    IntPtr token = GetCurrentUserToken();

    if (token == IntPtr.Zero)
        return null;

    IntPtr envBlock = GetEnvironmentBlock(token);
    uint? processId = LaunchProcessAsUser(cmdLine, token, envBlock);
    if (envBlock != IntPtr.Zero)
        DestroyEnvironmentBlock(envBlock);

    CloseHandle(token);
    return processId;
}

private static uint LaunchProcessAsUser(string cmdLine, IntPtr token, IntPtr envBlock)
{
    PROCESS_INFORMATION processInformation = new PROCESS_INFORMATION();
    SECURITY_ATTRIBUTES saProcess = new SECURITY_ATTRIBUTES();
    SECURITY_ATTRIBUTES saThread = new SECURITY_ATTRIBUTES();
    saProcess.nLength = (uint)Marshal.SizeOf(saProcess);
    saThread.nLength = (uint)Marshal.SizeOf(saThread);

    STARTUPINFO si = new STARTUPINFO();
    si.cb = (uint)Marshal.SizeOf(si);

    //if this member is NULL, the new process inherits the desktop
    //and window station of its parent process. If this member is
    //an empty string, the process does not inherit the desktop and
    //window station of its parent process; instead, the system
    //determines if a new desktop and window station need to be created.
    //If the impersonated user already has a desktop, the system uses the
    //existing desktop.

    si.lpDesktop = string.Empty; //Modify as needed
    si.dwFlags = STARTF_USESHOWWINDOW | STARTF_FORCEONFEEDBACK;
    si.wShowWindow = SW_SHOW;
    //Set other si properties as required.

    bool result = CreateProcessAsUser(
        token,
        null,
        cmdLine,
        ref saProcess,
        ref saThread,
        false,
        CREATE_UNICODE_ENVIRONMENT,
        envBlock,
        null,
        ref si,
        out processInformation);

    if (!result)
    {
        int error = Marshal.GetLastWin32Error();
        string message = String.Format("CreateProcessAsUser Error: {0}", error);
        throw new ApplicationException(message);
    }

    return processInformation.dwProcessId;
}

private static IntPtr GetEnvironmentBlock(IntPtr token)
{
    IntPtr envBlock = IntPtr.Zero;
    bool retVal = CreateEnvironmentBlock(ref envBlock, token, false);
    if (retVal == false)
    {
        // Environment Block, things like common paths to My Documents etc.
        // Will not be created if "false"
        // It should not adversley affect CreateProcessAsUser.

        string message = String.Format("CreateEnvironmentBlock Error: {0}", Marshal.GetLastWin32Error());
        throw new ApplicationException(message);

    }

    return envBlock;
}

private static IntPtr GetCurrentUserToken()
{
    IntPtr currentToken = IntPtr.Zero;
    IntPtr primaryToken = IntPtr.Zero;
    IntPtr WTS_CURRENT_SERVER_HANDLE = IntPtr.Zero;

    int dwSessionId = 0;
    IntPtr hUserToken = IntPtr.Zero;
    IntPtr hTokenDup = IntPtr.Zero;

    IntPtr pSessionInfo = IntPtr.Zero;
    int dwCount = 0;

    WTSEnumerateSessions(WTS_CURRENT_SERVER_HANDLE, 0, 1, ref pSessionInfo, ref dwCount);

    Int32 dataSize = Marshal.SizeOf(typeof(WTS_SESSION_INFO));

    Int64 current = (long)pSessionInfo;
    for (int i = 0; i < dwCount; i++)
    {
        WTS_SESSION_INFO si =
            (WTS_SESSION_INFO)Marshal.PtrToStructure((IntPtr)current, typeof(WTS_SESSION_INFO));
        if (WTS_CONNECTSTATE_CLASS.WTSActive == si.State)
        {
            dwSessionId = si.SessionID;
            break;
        }

        current += dataSize;
    }

    WTSFreeMemory(pSessionInfo);

    bool bRet = WTSQueryUserToken(dwSessionId, out currentToken);
    if (bRet == false)
        return IntPtr.Zero;

    bRet = DuplicateTokenEx(currentToken, TOKEN_ASSIGN_PRIMARY | TOKEN_ALL_ACCESS, IntPtr.Zero, SECURITY_IMPERSONATION_LEVEL.SecurityImpersonation, TOKEN_TYPE.TokenPrimary, out primaryToken);
    if (bRet == false)
        return IntPtr.Zero;

    return primaryToken;
}

1 个答案:

答案 0 :(得分:0)

只需要改变这个:

si.lpDesktop = string.Empty;

对此:

si.lpDesktop = null;

...所以新进程继承其父进程桌面。现在它完美无缺。