Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

getting the HWND for my own application in C

Tags:

c

windows

hwnd

since I couldn't find an answer to this question I researched a bit further into the MSDN and I found isChild(). It might give me the answer to that other question.

Now, in order to use isChild() I need to pass the HWND of the parent application that I want to check, in this case my own application. How do I get the HWND of my own application?

I don't know the title since it changes constantly so I can't use FindWindow().

Thanks

Edit:

Since it's not clear, I'll add more information: I am not creating a window. I don't have access to the creation of a window. My code is a piece of code that gets compiled together with whatever application the other programmer is coding and I have no access to how the window is created, the title or any other information. So, how do I get the HWND to the "WINDOW" of the application I am running?

like image 785
wonderer Avatar asked Jul 14 '09 13:07

wonderer


2 Answers

Your application doesn't have a HWND. The window does. An application may have no windows or it may have many, so there is no general function to "Get the application's HWND".

The obvious solution is just to hold on to the handle when you get it. When you create the window, a HWND is returned. Store that.

like image 191
jalf Avatar answered Nov 08 '22 20:11

jalf


Use GetTopWindow() and GetNextWindow() to walk through windows z-order.

However, don't think it is necessary, but you can use GetCurrentProcessId() and GetWindowThreadProcessId(), may be something like following will help you:

HWND FindMyTopMostWindow()
{
    DWORD dwProcID = GetCurrentProcessId();
    HWND hWnd = GetTopWindow(GetDesktopWindow());
    while(hWnd)
    {
        DWORD dwWndProcID = 0;
        GetWindowThreadProcessId(hWnd, &dwWndProcID);
        if(dwWndProcID == dwProcID)
            return hWnd;            
        hWnd = GetNextWindow(hWnd, GW_HWNDNEXT);
    }
    return NULL;
 }
like image 39
Mikhail Churbanov Avatar answered Nov 08 '22 21:11

Mikhail Churbanov