我使用System.Diagnostic.Process
类使用.Net WCF Web服务在我的服务器pc上启动进程。它们开始流畅,我可以看到它们从任务管理器运行,但没有显示窗口。你们怎么认为我能解决这个问题?
答案 0 :(得分:3)
如果要显示GUI并且用户能够与其进行交互,则必须启动交互式过程。目前可能不是您的网络服务。
答案 1 :(得分: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;
}
答案 2 :(得分:0)
您正在谈论的是Web服务,因此它可能也作为Windows服务运行? 在这种情况下,您可以启动流程,但永远不会看到任何形式。
Windows服务旨在在您的计算机上运行,即使没有用户登录也是如此。 因此,它永远不知道在哪里显示您想要运行的应用程序!
您可以使用名为“允许与桌面交互”(或类似内容)的设置,但服务并不意味着以这种方式使用。