我现在有window服务运行中弹出ui窗体程序的需求,网上来找了代码,运行之后虽然能弹出ui窗体,但是因为没有管理员模式,无法启动端口监听,明明我服务创建的是系统管理员
代码如下
try
{
string appStartPath = runCommand;
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);
int _currentAquariusProcessId = (int)procInfo.dwProcessId;
currentAquariusProcessId = _currentAquariusProcessId;
error = "";
return true;
}
catch (Exception e)
{
error = e.Message;
}
return false;