Get window handle from process id

Hi,

does anybody know how to get a window handle by knowing its process id

thanks for your help




Answer this question

Get window handle from process id

  • yfradkin

    That's the process ID you want, GetWindowThreadProcessId() sets it. Just declare a variable and pass it (well, actually reference/pointer to it) to GetWindowThreadProcessId(), after that variable would be set to process ID:

    uint processId;

    GetWindowThreadProcessId(hwnd, out processId);

    // processId now contains process ID for hwnd.



  • Ben Hall (UK)

    Thank you very much Ilya, your replies was very helpful

    THANK YOU



  • Dache

    Dear Ilya,

    thanks alot for your reply.

    here is my declaration

    [DllImport("coredll")] public static extern uint
    GetWindowThreadProcessId(IntPtr hwnd, out uint lpdwProcessId);

    but i don't know what is the lpdwProcessId i'd be grateful if you could tell me where to get it.

    thanks



  • tolily

    Enumerate all windows (EnumWindows()), get process ID for each one (GetWindowThreadProcessId()). Compare it with process ID you've got and if the same you found your window.



  • Vlince

    Hi,

    this is the api call i used

    [DllImport("coredll")]
    public static extern uint GetWindowThreadProcessId(IntPtr hwnd);

    but strangely returned IDs that doesn't match with any of the running processes!!

    even when  i try to kill the processes using these strange ids using

    System.Diagnostics.Process.GetProcessById(  the obtained id  ).Kill();

    i recieve and exception saying that this process is not running!!!!

    do you have any suggestions

    thanks alot

     



  • John Luke

    Your declaration is incorrect. GetWindowThreadProcessId() takes two arguments one of which is a pointer to the process ID to get back and it’s missing from your declaration. Returned value is a thread ID which is why it does not match any process ID. Here’s the function prototype:

    DWORD GetWindowThreadProcessId(

    HWND hWnd,

    LPDWORD lpdwProcessId

    );



  • Get window handle from process id