What i'm trying to do is writing a little wrapper, that allows me to start an application on the Windows GUI of the currently locked in User from withing a service. The wrapper is called as following: "wrapper.exe mspaint arg1 arg2 < some-piped-input". Everything works fine, except the pipe stuff. At the moment i'm creating 3 pipes (for stdin, stdout, stderr), that should redirect the input, that is piped to the wrapper (in the example some-piped-input) to the process of the GUI-Application and the output/error to stdout/stderr of the wrapper.
I do not get any errors, it just doesn't work.
With the following code none of these pipes works:
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);