【问题标题】:New process that got created using "CreateProcessAsUser" API is a child of the creating process使用“CreateProcessAsUser”API 创建的新进程是创建进程的子进程
【发布时间】:2014-05-22 22:08:25
【问题描述】:

我有一个 Windows 服务(进程 - x)启动另一个 GUI 应用程序(MVVM)(进程 - y),但进程 y 是进程 x 的子进程。即使这些进程由于某种原因作为不同的用户帐户运行,为进程 y 创建的日志文件也是在对进程 x 有效的位置创建的。 %USERPROFILE% 环境变量正在日志文件的路径中使用。因此,对于作为“LocalSystem”帐户运行的进程 x,环境变量被评估为 C:\Windows\SysWOW64\config\systemprofile。对于作为当前登录的 windows 用户运行的进程 y,环境变量被评估为进程 x 的值而不是 C:\Users[loginID]。这是因为进程 y 是进程 x 的子进程。所以,我需要知道如何打破这种亲子关系。

我使用 CreateProcessAsUser API 从进程 x 创建进程 y。

        // flags that specify the priority and creation method of the process
        int dwCreationFlags = NORMAL_PRIORITY_CLASS | CREATE_NEW_CONSOLE;

        // create a new process in the current user's logon session
        bool result = CreateProcessAsUser(usertoken,                                                        // client's access token
                                        null,                                                        // file to execute
                                        String.Format("{0} {1}", applicationName,  arguments),                     // command line
                                        ref sa,                                                                 // pointer to process SECURITY_ATTRIBUTES
                                        ref sa,                                                                 // pointer to thread SECURITY_ATTRIBUTES
                                        false,                                                                  // handles are not inheritable
                                        dwCreationFlags,                                                        // creation flags
                                        IntPtr.Zero,                                                            // pointer to new environment block 
                                        workingDirectory,                                                         // name of current directory 
                                        ref si,                                                                 // pointer to STARTUPINFO structure
                                        out procInfo                                                            // receives information about new process
                                        );

请告诉我如何删除父子关系。

【问题讨论】:

    标签: c#


    【解决方案1】:

    问题在于CreateProcessAsUser 仅使用给定用户的凭据(安全上下文)运行进程,而不使用任何配置文件信息。

    MSDN 页面http://msdn.microsoft.com/en-us/library/windows/desktop/ms682429(v=vs.85).aspx 建议使用LoadUserProfileCreateEnvironmentBlock 加载配置文件和环境块。

    【讨论】:

    • 感谢您的意见。我能够通过使用 usertoken 调用 CreateEnvironmentBlock 来启动进程来解决这个问题 - 是的。此外,当环境块被传递给 CreateProcessAsUser 时,我们需要确保使用 CREATE_UNICODE_ENVIRONMENT 创建标志。
    • @CP1:确实,CreateEnvironmentBlock 函数为您创建了一个 Unicode 环境块,所以当您将它传递给 CreateProcessAsUser 时,您需要告诉它它是 Unicode(没有它,它假定ANSI)
    猜你喜欢
    • 1970-01-01
    • 2014-12-30
    • 1970-01-01
    • 2014-04-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多