【发布时间】: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#