在 MFC 中创建 CPaneDialog 时断言失败

Assertion failed when creating CPaneDialog In MFC

本文关键字:断言 失败 CPaneDialog 创建 MFC      更新时间:2023-10-16

我有一个简单的 .exe MFC 项目和一个静态链接的 MFC 的 DLL 项目。它导出以下函数以创建 CPaneDialog:

extern "C" __declspec(dllexport) void init_toolbox_gui(HWND ptr) {
AFX_MANAGE_STATE(AfxGetStaticModuleState());
CPaneDialog *_gui = new CPaneDialog;
CWnd *p = CWnd::FromHandle(ptr);
_gui->Create(_T("DialogBar"), p, TRUE, (IDD_DIALOG1),
    WS_CHILD | WS_VISIBLE | WS_CLIPSIBLINGS | WS_CLIPCHILDREN | CBRS_LEFT | CBRS_FLOAT_MULTI,
    0);
}

我从我的主.exe应用程序类(派生自 CWinAppEx)调用它,如下所示:

// dll is a handle to the DLL lib
ToolboxInitFunc func = (ToolboxInitFunc)GetProcAddress(dll, "init_toolbox_gui"); 
func(m_pMainWnd->GetSafeHwnd());

这在以下调试断言中失败,void CMFCDragFrameImpl::Init(CWnd* pDraggedWnd)

m_pDockManager = afxGlobalUtils.GetDockingManager(pDockSite);
ENSURE(m_pDockManager != NULL);

我可以给出该函数的完整代码,但它来自标准 MFC 库。

这是调用堆栈:

toolbox-3d.dll!CMFCDragFrameImpl::Init(CWnd * pDraggedWnd) Line 106 C++
toolbox-3d.dll!CPane::CreateEx(unsigned long dwStyleEx, const wchar_t * lpszClassName, unsigned long dwStyle, const tagRECT & rect, CWnd * pParentWnd, unsigned int nID, unsigned long dwControlBarStyle, CCreateContext * pContext) Line 177   C++
toolbox-3d.dll!CDockablePane::CreateEx(unsigned long dwStyleEx, const wchar_t * lpszCaption, CWnd * pParentWnd, const tagRECT & rect, int bHasGripper, unsigned int nID, unsigned long dwStyle, unsigned long dwTabbedStyle, unsigned long dwControlBarStyle, CCreateContext * pContext) Line 175   C++
toolbox-3d.dll!CDockablePane::Create(const wchar_t * lpszWindowName, CWnd * pParentWnd, CSize sizeDefault, int bHasGripper, unsigned int nID, unsigned long dwStyle, unsigned long dwTabbedStyle, unsigned long dwControlBarStyle) Line 148 C++
toolbox-3d.dll!CPaneDialog::Create(const wchar_t * lpszWindowName, CWnd * pParentWnd, int bHasGripper, const wchar_t * lpszTemplateName, unsigned int nStyle, unsigned int nID, unsigned long dwTabbedStyle, unsigned long dwControlBarStyle) Line 48   C++
toolbox-3d.dll!CPaneDialog::Create(const wchar_t * lpszWindowName, CWnd * pParentWnd, int bHasGripper, unsigned int nIDTemplate, unsigned int nStyle, unsigned int nID) Line 42 C++
toolbox-3d.dll!init_toolbox_gui(HWND__ * ptr) Line 45   C++

可能出了什么问题?

不能"独立"使用对接功能。

这种对接的东西需要一个专门准备的CFrameWndEx类。如果 MFC 项目使用此类,则当您要使用 DLL 时,将强制使用动态链接的 MFC。

您获得的 ASSERT 只是一个指标,表明当前模块(您的 DLL)既没有框架也没有停靠管理器来支持此类可停靠窗格。

原因很简单。静态链接的 EXE 和 DLL 将只使用自己的 CObject 表示形式,因此 MFC 内部使用的所有 IsKindOf 调用都只能在模块内部工作。