我正在尝试做的是编写一个小包装器,它允许我在当前锁定的用户的 Windows GUI 上启动一个应用程序以使用服务。包装器调用如下:“wrapper.exe mspaint arg1 arg2 < some-piped-input”。一切正常,除了管道的东西。目前我正在创建 3 个管道(用于 stdin、stdout、stderr),它们应该将输入重定向到包装器(在示例中为一些管道输入)到 GUI 应用程序的进程和输出/错误到包装器的标准输出/标准错误。
我没有收到任何错误,它只是不起作用。
使用以下代码,这些管道都不起作用:
pi = new PROCESS_INFORMATION();
sa = new SECURITY_ATTRIBUTES();
saProcess = new SECURITY_ATTRIBUTES();
saThread = new SECURITY_ATTRIBUTES();
si = new STARTUPINFO();
sa.nLength = (uint)Marshal.SizeOf(sa);
sa.lpSecurityDescriptor = IntPtr.Zero;
sa.bInheritHandle = true;
saProcess.nLength = (uint)Marshal.SizeOf(saProcess);
saThread.nLength = (uint)Marshal.SizeOf(saThread);
si.cb = (uint)Marshal.SizeOf(si);
// Create in/out/err pipes
subprocessStdIn = new IntPtr();
subprocessStdOut = new IntPtr();
subprocessStdErr = new IntPtr();
// Get main processes in/out/err pipes
mainProcessStdIn = GetStdHandle(StdHandle.Stdin);
mainProcessStdOut = GetStdHandle(StdHandle.Stdout);
mainProcessStdErr = GetStdHandle(StdHandle.Stderr);
// this ---------------> subprocess
if (CreatePipe(ref subprocessStdIn, ref mainProcessStdIn, ref sa, 0x0001))
{
Console.WriteLine("Pipe stdIn --> pIn created.");
si.hStdInput = subprocessStdIn;
}
// this <--------------- subprocess
if (CreatePipe(ref mainProcessStdOut, ref subprocessStdOut, ref sa, 0x0001))
{
Console.WriteLine("Pipe stdOut <-- pOut created.");
si.hStdOutput = subprocessStdOut;
}
// this <--------------- subprocess
if (CreatePipe(ref mainProcessStdErr, ref subprocessStdErr, ref sa, 0x0001))
{
Console.WriteLine("Pipe stdErr <-- pErr created.");
si.hStdError = subprocessStdErr;
}
si.lpDesktop = @"WinSta0\Default"; //Modify as needed
si.dwFlags = STARTF_USESHOWWINDOW | STARTF_FORCEONFEEDBACK | STARTF_USESTDHANDLES;
si.wShowWindow = SW_SHOW;
CreateProcessAsUser(token, null, cmdLine, ref saProcess, ref saThread, false, CREATE_UNICODE_ENVIRONMENT, envBlock, null, ref si, out pi);