简体   繁体   English

使用“ CreateProcessAsUser” API创建的新流程是创建流程的子流程

[英]New process that got created using “CreateProcessAsUser” API is a child of the creating process

I have a windows service (process - x) launching another GUI application (MVVM) (process - y) but process y is a child of process x. 我有一个Windows服务(进程-x)启动另一个GUI应用程序(MVVM)(进程-y),但是进程y是进程x的子级。 Even though these processes run as different user accounts for some reason the log file that is being created for process y is being created at a location that is valid for process x. 即使由于某些原因这些进程以不同的用户帐户运行,也会在对进程x有效的位置上创建为进程y创建的日志文件。 %USERPROFILE% environment variable is being used in the path to the log file. 在日志文件的路径中使用了%USERPROFILE%环境变量。 So, for process x which is running as "LocalSystem" account the environment variable is being evaluated to C:\\Windows\\SysWOW64\\config\\systemprofile. 因此,对于以“ LocalSystem”帐户运行的进程x,环境变量被评估为C:\\ Windows \\ SysWOW64 \\ config \\ systemprofile。 For process y which is running as a current logged-in windows user the environment variable is being evaluated to process x's value instead of C:\\Users[loginID]. 对于以当前登录的Windows用户身份运行的进程y,正在评估环境变量以处理x​​的值,而不是C:\\ Users [loginID]。 This is because process y is a child of process x. 这是因为进程y是进程x的子级。 So, I need to know how to break that parent-child relationship. 所以,我需要知道如何打破这种亲子关系。

I use CreateProcessAsUser API to create process y from process 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
                                        );

Please let me know how to remove the parent-child relationship. 请让我知道如何删除亲子关系。

The problem is that CreateProcessAsUser only runs the process with the given user's credentials (security context), but not with any profile information. 问题在于, CreateProcessAsUser仅使用给定用户的凭据(安全上下文)运行进程,而不使用任何配置文件信息运行进程。

The MSDN page http://msdn.microsoft.com/en-us/library/windows/desktop/ms682429(v=vs.85).aspx suggests loading the profile and environment block using LoadUserProfile and CreateEnvironmentBlock . MSDN页面http://msdn.microsoft.com/zh-cn/library/windows/desktop/ms682429(v=vs.85).aspx建议使用LoadUserProfileCreateEnvironmentBlock加载配置文件和环境块。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM