Run Win32 A process, which starts process B - obtains ID / HWND for B

Well, I spent a little time on this site figuring out how to start the “child” process (i.e. the new parent window set for me) using Win32 calls from C #. It works like this until it crosses the boundaries of the UAC. Good.

Now I'm trying to do this with the uninstall program (process A), which loads a temporary program (process B) that actually does the job. Process A leaves after creating B. My code requires a process identifier from which to get a window handle that is passed to SetParent. It looks something like this:

Process p = new Process();
try
{
    p.EnableRaisingEvents = true;
    p.StartInfo.FileName = fileName;
    p.StartInfo.Arguments = arguments;
    if (p.Start())
    {
        p.WaitForInputIdle(10000);
        IntPtr pHwnd = p.MainWindowHandle;
        if (pHwnd == IntPtr.Zero)
        {
            return null;
        }
        IntPtr currentHwnd = Process.GetCurrentProcess().MainWindowHandle;
        if (SetParent(pHwnd, currentHwnd) == 0)
        {
            if (Marshal.GetLastWin32Error() == 5) // access denied
            {
                // Need to launch privileged process that launches process 
                // and sets parent on UAC enabled OS.
            }
            else
            {
                return null;
            }
        }
        // AND SO ON AND SO FORTH

Works great until p leaves. In this case, p starts the arrow after starting p '. Despite this, p never has a window handle.

, p, , p ' (, , ) p'? HWND id, .

!

+3
1

, , p null, MainWindowHandle. , .

           using (Process proc = new Process())
            {

                proc.StartInfo.FileName = filename;
                proc.StartInfo.UseShellExecute = false;
                proc.StartInfo.WorkingDirectory = ClientInstallPath;
                proc.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;

                proc.Start();

                if (proc != null)
                {
                    proc.WaitForExit();
                    returnCode = proc.ExitCode;
                }
            }
0

All Articles