Search code examples
cwindowshwnd

getting the HWND for my own application in C


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?


Solution

  • 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;
     }