今天突然发现,自己之前写的windows服务无法启动后台进程,本来以为是关于服务的代码写的有问题,可是通过日志调试了一下以后,发现并没有什么问题,而且启动进程的
代码也执行了但是,进程并没有运行起来,后来就百度了一下,原来是需要一个引用--Cjwdev.WindowsApi.dll
这里先附上下载地址:http://pan.baidu.com/s/1qYwHtX6
把Cjwdev.WindowsApi.dll添加引用到项目中,然后就是传入进程的执行文件filePath 就可以启动这个进程了。
string appStartPath =filPath;
IntPtr userTokenHandle = IntPtr.Zero;
ApiDefinitions.WTSQueryUserToken(ApiDefinitions.WTSGetActiveConsoleSessionId(), ref userTokenHandle);
ApiDefinitions.PROCESS_INFORMATION procInfo = new ApiDefinitions.PROCESS_INFORMATION();
ApiDefinitions.STARTUPINFO startInfo = new ApiDefinitions.STARTUPINFO();
startInfo.cb = (uint)System.Runtime.InteropServices.Marshal.SizeOf(startInfo);
ApiDefinitions.CreateProcessAsUser(
userTokenHandle,
appStartPath,
"",
IntPtr.Zero,
IntPtr.Zero,
false,
0,
IntPtr.Zero,
null,
ref startInfo,
out procInfo);
if (userTokenHandle != IntPtr.Zero)
ApiDefinitions.CloseHandle(userTokenHandle);
int _currentAquariusProcessId = (int)procInfo.dwProcessId;
对了这个还需要添加命名空间:using Cjwdev.WindowsApi;