我编写了一个简单的Windows服务,它将启动onstart()服务方法中指定的exe
.启动服务后,exe启动它只出现在内存中但它没有显示在资源管理器中.
我正在尝试calc.exe从我的代码中启动一个.它在内存中显示了exe,但它并没有进入我在浏览器中的视图(即).
下面是我在onStart()方法中启动exe的代码
Process pr=new Process();
pr.StartInfo.FileName="calc.exe";
pr.StartInfo.WindowStyle=ProcessWindowStyle.Maximized;
pr.StartInfo.CreateNoWindow=false;
pr.Start();
// pr.WaitForExit();
Run Code Online (Sandbox Code Playgroud)
dev*_*imi 10
服务在Vista或更高版本的其他会话中运行,而直接从服务启动的应用程序默认在同一会话中启动.可以在其他会话中启动应用程序 - 您必须找到用户会话的ID并使用CreateProcessAsUser.
如果有多个用户登录并且您需要为所有用户启动程序,则必须找到所有会话的ID.
这是示例代码:
int session = Win32.WTSGetActiveConsoleSessionId();
if (session == 0xFFFFFFFF)
{
return false;
}
IntPtr userToken;
bool res = Win32.WTSQueryUserToken(session, out userToken);
if (!res)
{
this.log.WriteEntry("Error WTSQueryUserToken");
return false;
}
string path = GetPath();
string dir = Path.GetDirectoryName(path);
Win32.STARTUPINFO si = new Win32.STARTUPINFO();
si.lpDesktop = "winsta0\\default";
si.cb = Marshal.SizeOf(si);
Win32.PROCESS_INFORMATION pi = new Win32.PROCESS_INFORMATION();
Win32.SECURITY_ATTRIBUTES sa = new Win32.SECURITY_ATTRIBUTES();
sa.bInheritHandle = 0;
sa.nLength = Marshal.SizeOf(sa);
sa.lpSecurityDescriptor = IntPtr.Zero;
if (!Win32.CreateProcessAsUser(userToken, // user token
path, // exexutable path
string.Empty, // arguments
ref sa, // process security attributes ( none )
ref sa, // thread security attributes ( none )
false, // inherit handles?
0, // creation flags
IntPtr.Zero, // environment variables
dir, // current directory of the new process
ref si, // startup info
out pi)) // receive process information in pi
{
int error = Marshal.GetLastWin32Error();
this.log.WriteEntry("Error CreateProcessAsUser:" + error);
return false;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
11233 次 |
| 最近记录: |