以下代码是服务启动外部程序的方法,在Windows10上可以正常启用,在服务器上无法正常启动,未获取到异常,无Windows错误日志。
try
{
string appStartPath = this.VPNInstallPosition + "\.exe";
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)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);
_GetProcessError = "无法判断程序是否已打开,只能判断打开程序无错误";
return true;
}
catch (Exception ex)
{
_GetProcessError = "打开程序进程失败," + ex.Message.Replace("\n", "").Replace("\r", "");
return false;
}