将stdin / stdout / stderr完全传递给使用CreateProcessAsUser创建的进程

时间:2013-04-29 12:22:48

标签: c# winapi service pipe createprocessasuser

我正在尝试做的是编写一个小包装器,它允许我在当前锁定的用户的Windows GUI上启动服务。包装器调用如下:“wrapper.exe mspaint arg1 arg2< some-piped-input”。一切都很好,除了管道的东西。 目前我正在创建3个管道(对于stdin,stdout,stderr),它应该重定向输入,通过管道输送到包装器(在示例中为some-piped-input)到GUI-Application的进程和输出/错误到包装器的stdout / stderr。

我没有得到任何错误,它只是不起作用。

使用以下代码,这些管道都不起作用:

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);

0 个答案:

没有答案