开发者

Start Win32 process A which starts process B--get ID / HWND for B

开发者 https://www.devze.com 2023-03-10 04:23 出处:网络
Okay, I spent a bit of time on this site figuring out how to start a \"child\" process (i.e., new process sets window parent to me) using Win32 calls from C#. It kinda works so long as it doesn\'t cro

Okay, I spent a bit of time on this site figuring out how to start a "child" process (i.e., new process sets window parent to me) using Win32 calls from C#. It kinda works so long as it doesn't cross UAC boundaries. Fine.

Now I'm trying to do this with an uninstall program (process A) that bootstraps a temporary program (process B) which actually does the work. Process A goes away after creating B. My code requires a process ID from which to get an window handle which gets passed to SetParent. 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 laun开发者_Python百科ch privileged process that launches process 
                // and sets parent on UAC enabled OS.
            }
            else
            {
                return null;
            }
        }
        // AND SO ON AND SO FORTH

Works great so long as p doesn't go away. In this case p goes boom after starting p'. Regardless, p never has a window handle.

So how do I monitor p to see if it starts p' and get the id (or more importantly the window handle) for p'? I can get the HWND from the id, but I need to get one or the other.

Thanks!


A simple solution might be, check to see if p is null before trying to get its MainWindowHandle. Here's some sample code which you can adapt if needed.

           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

精彩评论

暂无评论...
验证码 换一张
取 消