Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass a Window Handle to a sub-process?

Tags:

windows

winapi

I am working on a special project with an unusual design.

In my application/process, I will create a sub process to do some work. In my process, I need to get the feedback from sub process. I want to pass the Windows handle of my application/process to this sub process, so I am post message from this sub process.

How can I pass Window Handle to sub process? My sub process is an command line application without Window UI and with the main function like this:

int APIENTRY _tWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
                   LPTSTR lpCmdLine, int nCmdShow)

Should I pass Window Handle to the main function? How?

Thanks

like image 380
Watterry Avatar asked Aug 16 '12 07:08

Watterry


People also ask

How do you put a handle on a window?

However, you can obtain the window handle by calling FindWindow() . This function retrieves a window handle based on a class name or window name. Call GetConsoleTitle() to determine the current console title. Then supply the current console title to FindWindow() .

How do I find my windows handle ID?

getWindowHandle( ): When a website opens, we need to handle the main window i.e the parent window using driver. getWindowHandle( ); method. With this method, we get a unique ID of the current window which will identify it within this driver instance.

What is window handle C#?

Once the Selenium WebDriver instance is instantiated, a unique alphanumeric id is assigned to the window. This is called window handle and is used to identify browser windows. Since the id is unique, it is used by the Selenium WebDriver to switch between different windows (or tabs).

What is a handle to a window?

Hi Fardeen, a Window Handle is a unique identifier that holds the address of all the windows. This is basically a pointer to a window, which returns the string value. This window handle function helps in getting the handles of all the windows. It is guaranteed that each browser will have a unique window handle.


2 Answers

Window handles (HWNDs) are global to the system so you can just print the handle as a decimal number into a string buffer, pass it as a string on the command line when you spawn your sub-process with CreateProcess, and call _wtoi() or similar to convert the string back to a handle again.

like image 114
Jonathan Potter Avatar answered Sep 18 '22 18:09

Jonathan Potter


  1. Create a named mutex (global), so both processes could access it, and take it.
  2. Spawn the second process. It should wait for the mutex to be freed.
  3. You can then pass the window handle using any Inter-Process Communication method; the easiest one may be Shared Memory. Just write the duplicated handle into shared memory.
  4. Release the mutex, so that second process could grab it.
  5. Read the handle from the shared memory. It is now safe to use it.

The whole mutex manipulation is only to make sure second process will not read from shared memory before anything is written there.

(thanks to @JonathanPotter for comments)

like image 32
Lyth Avatar answered Sep 21 '22 18:09

Lyth