已知DLL的HMODULE,如何获取加载该DLL的应用程序的主窗口句柄HWND?主应用程序可能不同,但肯定都有窗口。

解决方案 »

  1.   

    AfxGetMainWnd  
    CWnd* AfxGetMainWnd( );Return ValueIf the server has an object that is in-place active inside a container, and this container is active, this function returns a pointer to the frame window object that contains the in-place active document. If there is no object that is in-place active within a container, or your application is not an OLE server, this function simply returns the m_pMainWnd of your application object.If AfxGetMainWnd is called from the application's primary thread, it returns the application's main window according to the above rules. If the function is called from a secondary thread in the application, the function returns the main window associated with the thread that made the call.ResIf your application is an OLE server, call this function to retrieve a pointer to the active main window of the application instead of directly referring to the m_pMainWnd member of the application object.If your application is not an OLE server, then calling this function is equivalent to directly referring to the m_pMainWnd member of your application object.See Also   CWinThread::m_pMainWnd
      

  2.   

    非常感谢楼上的帮助。DLL实现部分COM接口,并只能以规则DLL方式出现,不支持MFC,主应用程序也不是通过MFC实现,以GetMainWnd/AfxGetMainWnd方式无法获取主窗口。
      

  3.   

    先用EnumProcess枚举进程,然后用EnumProcessModules枚举每个进程的模块,将枚举到的模块句柄和你已知的HMODULE比较,如果相等,再枚举Top-Level窗口。你可以看看MSDN杂志的文章中的第二部分。
    http://msdn.microsoft.com/msdnmag/issues/02/07/CQA/
    Good Luck!!
      

  4.   

    那通过参数传主窗口句柄到DLL,行吗?
      

  5.   

    回 laiyiling(陌生人[MVP]) :
    EnumProcess或者FindWindow的方式是可行的,但
    1. 麻烦,是否有简单的方法
    2. 宿主程序可能是多种。
    回 syy64(太平洋) :
    宿主程序并不是自己开发的,比如MediaPlayer之类的
      

  6.   

    DLL是一些Source/Transform Filter,因为某些特殊需求需要主窗口的句柄。主窗口甚至可能是暴风影音、RealPlayer。还有个方法是获取RenderFilter以获取IVideoWindow,也需要通过枚举方式实现。使用laiyiling(陌生人[MVP])提供的范例实现没有出现任何问题。非常感谢两位热心帮助:)