Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to bring window on top of the process created through CreateProcess

I am launching a process from my application using CreateProcess API and I want to bring the window of the new process to top. Is there a way to do it? Do we have any flag or something like that with CreateProcess?

like image 314
Rahul Avatar asked Jul 27 '11 08:07

Rahul


People also ask

What is windows CreateProcess?

The CreateProcess function creates a new process, which runs independently of the creating process.

What is Create process?

The fundamental Windows process management function is CreateProcess, which creates a process with a single thread. Specify the name of an executable program file as part of the CreateProcess call. It is common to speak of parent and child processes, but Windows does not actually maintain these relationships.

What is STARTUPINFO?

The STARTUPINFO structure is used with the CreateProcess function to specify main window properties if a new window is created for the new process. For graphical user interface (GUI) processes, this information affects the first window created by the CreateWindow function and shown by the. ShowWindow function.


1 Answers

You can try to use the STARTUPINFO structure which is passed in with CreateProcess and set SW_SHOW. I'm not sure this will help bring the focus to the top though. If that doesn't work then try the following.

First off, do not use FindWindow(), it is unnecessarily unreliable since it only works via the window name and class name. Instead, from your CreateProcess() call you should read lpProcessInformation and grab the dwProcessId. Then call EnumWindows() and have your callback look something like this:

BOOL CALLBACK EnumWindowsProc( HWND hwnd, LPARAM lParam ) {
  DWORD dwPID;

  GetWindowThreadProcessId( hwnd, &dwPID );

  if( dwPID == lParam ) {
    SetWindowPos( hwnd, HWND_TOP, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE );

    // Or just SetFocus( hwnd );
    return FALSE;
  }

  return TRUE;
}

When calling EnumWindows() you will need to pass in the PID you grabbed earlier as the lParam like so:

EnumWindows( EnumWindowsProc, ( LPARAM )( PI -> dwProcessId ) );
like image 113
Mike Kwan Avatar answered Sep 17 '22 02:09

Mike Kwan