我使用System.Diagnostic.Process
类在使用 .Net WCF Web 服务的服务器 PC 上启动进程。他们开始顺利,我可以看到他们从任务管理器运行,但没有显示窗口。你们认为我怎样才能克服这个问题?
问问题
1639 次
3 回答
3
如果你想显示一个 GUI 并且用户能够与之交互,你必须启动一个交互过程。您的 Web 服务目前可能不是这种情况。
于 2012-07-06T12:13:09.453 回答
1
你不应该这样做,但是......
您可以在当前会话中以用户身份启动程序
IntPtr UserTokenHandle = IntPtr.Zero;
WTSQueryUserToken ( WTSGetActiveConsoleSessionId(), ref UserTokenHandle);
PROCESS_INFORMATION ProcInfo = new PROCESS_INFORMATION();
STARTUPINFOW StartInfo = new STARTUPINFOW();
StartInfo.cb = Convert.ToUInt32(System.Runtime.InteropServices.Marshal.SizeOf(StartInfo));
CreateProcessAsUser ( UserTokenHandle, @"C:\dir\MyApp.exe",
IntPtr.Zero,
IntPtr.Zero,
IntPtr.Zero,
false,
0,
IntPtr.Zero,
null,
ref StartInfo,
ref ProcInfo);
if (!(UserTokenHandle == IntPtr.Zero))
{
CloseHandle ( UserTokenHandle);
}
所需的导入和结构:
[DllImport("kernel32.dll", EntryPoint = "WTSGetActiveConsoleSessionId", SetLastError = true)]
public static extern uint WTSGetActiveConsoleSessionId ();
[DllImport("Wtsapi32.dll", EntryPoint = "WTSQueryUserToken", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool WTSQueryUserToken ( uint SessionId, ref IntPtr phToken );
[DllImport("kernel32.dll", EntryPoint = "CloseHandle", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool CloseHandle ( [InAttribute()]
IntPtr hObject );
[DllImport("advapi32.dll", EntryPoint = "CreateProcessAsUserW", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool CreateProcessAsUser (
[InAttribute()]
IntPtr hToken,
[InAttribute(), MarshalAs(UnmanagedType.LPWStr)]
string lpApplicationName, System.IntPtr lpCommandLine,
[InAttribute()]
IntPtr lpProcessAttributes,
[InAttribute()]
IntPtr lpThreadAttributes,
[MarshalAs(UnmanagedType.Bool)]
bool bInheritHandles, uint dwCreationFlags,
[InAttribute()]
IntPtr lpEnvironment,
[InAttribute(), MarshalAsAttribute(UnmanagedType.LPWStr)]
string lpCurrentDirectory,
ref STARTUPINFOW lpStartupInfo,
ref PROCESS_INFORMATION lpProcessInformation );
[StructLayout(LayoutKind.Sequential)]
public struct SECURITY_ATTRIBUTES
{
public uint nLength;
public IntPtr lpSecurityDescriptor;
[MarshalAs(UnmanagedType.Bool)]
public bool bInheritHandle;
}
[StructLayout(LayoutKind.Sequential)]
public struct STARTUPINFOW
{
public uint cb;
[MarshalAs(UnmanagedType.LPWStr)]
public string lpReserved;
[MarshalAs(UnmanagedType.LPWStr)]
public string lpDesktop;
[MarshalAs(UnmanagedType.LPWStr)]
public string lpTitle;
public uint dwX;
public uint dwY;
public uint dwXSize;
public uint dwYSize;
public uint dwXCountChars;
public uint dwYCountChars;
public uint dwFillAttribute;
public uint dwFlags;
public ushort wShowWindow;
public ushort cbReserved2;
public IntPtr lpReserved2;
public IntPtr hStdInput;
public IntPtr hStdOutput;
public IntPtr hStdError;
}
[StructLayout(LayoutKind.Sequential)]
public struct PROCESS_INFORMATION
{
public IntPtr hProcess;
public IntPtr hThread;
public uint dwProcessId;
public uint dwThreadId;
}
于 2012-07-06T12:21:10.937 回答
0
您在谈论 Web 服务,因此它可能也作为 Windows 服务运行?在这种情况下,您可以启动流程,但永远不会看到任何表单。
Windows 服务旨在在您的计算机上运行,即使没有用户登录。因此,它永远不知道在哪里显示您要运行的应用程序!
您可以摆弄一个名为“允许与桌面交互”(或类似的东西)的设置,但服务并不意味着以这种方式使用。
于 2012-07-06T12:07:54.503 回答