2

I'm launching a new process from a service (Administrative privileges) in C# by using a custom DLL written in c++ that basically modifies the security and privileges to reduce it. This is because the new process when run through Ring 0 of Windows 7 does not allow it to access the hardware encoder in OpenCL (Windows driver design limitation). So I impersonate the Windows Explorer privileges and launch it using this DLL.

The crux of the DLL file is:

                                typedef BOOL (WINAPI *LPFN_CreateProcessWithTokenW)(
                                HANDLE hToken,
                                DWORD dwLogonFlags,
                                LPCWSTR lpApplicationName,
                                LPWSTR lpCommandLine,
                                DWORD dwCreationFlags,
                                LPVOID lpEnvironment,
                                LPCWSTR lpCurrentDirectory,
                                LPSTARTUPINFOW lpStartupInfo,
                                LPPROCESS_INFORMATION lpProcessInfo
                                );
                            LPFN_CreateProcessWithTokenW fnCreateProcessWithTokenW=NULL;
                            HINSTANCE hmodAdvApi32=LoadLibraryA("AdvApi32");
                            if(hmodAdvApi32)
                                fnCreateProcessWithTokenW=(LPFN_CreateProcessWithTokenW)GetProcAddress(hmodAdvApi32, "CreateProcessWithTokenW");
                            if(fnCreateProcessWithTokenW)
                            {
                                bRet=fnCreateProcessWithTokenW(hNewToken, 0, 
                                    szProcessName, szCmdLine, 
                                    0, NULL, NULL, &StartupInfo, &ProcInfo);
                                if(!bRet)
                                    hr=HRESULT_FROM_WIN32(GetLastError());
                            }

Now the question is how do I redirect the output from this process to my C# program to monitor the output? This is critical since I need to know how the process is progressing or hanging so the C# app can take appropriate actions. So can I:

  1. Redirect/capture output from an existing process using a process id?
  2. How can I pass a callback function from c# to a dll to register that output handler with the above function so I can capture the output from the c# app.
rboy
  • 2,018
  • 1
  • 23
  • 35
  • 2
    can't u use a pipe? ....Named Pipes – internals-in Nov 06 '13 at 05:51
  • You're right, I finally managed to figure it out. Get the StdOut and StdErr console handle and attach a pipe to it, then run a background thread to keep monitoring the output of the pipe. – rboy Jan 15 '14 at 22:11
  • Although I'll say it wasn't as easy as that, there were loads of complications which I had to run through using C# and Marshalling and pInvokes and ofcourse Security issues. – rboy Jan 15 '14 at 22:12
  • pInvokes ? 1)http://msdn.microsoft.com/en-us/library/system.io.pipes.namedpipeserverstream(v=vs.110).aspx 2)http://www.codeproject.com/Tips/492231/Csharp-Async-Named-Pipes 3)http://www.blakewatts.com/namedpipepaper.html – internals-in Jan 16 '14 at 03:48

0 Answers0