How to find main HWND handle of MFC

I have a DLL file that is running under a parent piece of software. The parent software uses MFC and displays a main GUI window. What I want to do is create my own little modeless dialog box to pop up on the screen and sit right next to the main window. To do this I think I need the HWND of the parent's main window so I can find it's RECTangle and then move my DLL's window to where I want it.

If at all possible, I'd like not to change the source of the parent. How could I do this?

A second, possibly related, question is whether I should use MFC, or plain windows API, to create my DLL's dialog box. I want the method that will be easier to get it going, and I have read on MSDN that I may need the parent MFC application to send me messages for my MFC to work, which I'd rather not do.


I don't know if you can create an MFC dll that can find MFC objects created in another module. I'm pretty sure that MFC can be used to create dialogs from DLLs, regardless of whether or not MFC is used in the application.

That said, if you are not already an MFC expert, and have no real wish to become one, creating a dialog box from a dll is quite easy. MFC's CDialog is really a thin wrapper over CreateDialog so you don't gain much.

I can think of at least two approaches to find the application window if the app/dll interface doesn't hand it over:

  • Use FindWindow to locate the window based on its title, or classname. As frameworks like MFC sometimes generate window classnames dynamically this might be problematic.

  • Use EnumWindows to enumerate all top level windows. GetWindowThreadProcessId can test if it belongs to the current process.


  • Call GetGUIThreadInfo on the main thread. This gets you a bunch of HWNDs. Pick any valid one (not all values may be filled) and find its top level ancestor with GetAncestor(GA_ROOT) . Unlike EnumWindows , this doesn't require enumeration, and unlike FindWindow this doesn't require specialized knowledge


    为了获得父母的PID - 而不是HWND--请看http://www.codeexperts.com/showthread.php?1380-get-parent-process-id-from-child-process-id&p = 2845&viewfull = 1#post2845

    DWORD GetParentProcessID(DWORD dwProcessID)
    {
        DWORD dwParentProcessID = -1 ;
        HANDLE          hProcessSnapshot ;
        PROCESSENTRY32  processEntry32 ;
    
        hProcessSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0) ;
        if(hProcessSnapshot != INVALID_HANDLE_VALUE)
        {
            processEntry32.dwSize = sizeof(PROCESSENTRY32) ;
            if(Process32First(hProcessSnapshot, &processEntry32))
            {
                do
                {
                    if (dwProcessID == processEntry32.th32ProcessID)
                    {
                        dwParentProcessID = processEntry32.th32ParentProcessID ;
                        break ;
                    }
                }
                while(Process32Next(hProcessSnapshot, &processEntry32)) ;
    
                CloseHandle(hProcessSnapshot) ;
            }
        }
    
        return dwParentProcessID ;
    }
    
    链接地址: http://www.djcxy.com/p/41196.html

    上一篇: 在WPF浮动分离选项卡中托管Win32窗口

    下一篇: 如何找到MFC的主要HWND句柄