c++ - 线程退出时,dll 中的 mfc 无模式对话框被破坏

标签 c++ dll mfc dialog modeless

我想从注入(inject)另一个进程的MFC dll打开一个MFC无模式对话框,dll的工作是 Hook winsock发送和接收,对话框将是与dll通信的接口(interface)。 dll 应该能够在对话框运行时运行 Hook 。

BOOL CDriverApp::InitInstance()
{
    CWinApp::InitInstance();

    if (!AfxSocketInit())
    {
        AfxMessageBox(IDP_SOCKETS_INIT_FAILED);
        return FALSE;
    }

    AfxMessageBox("I'm In!");

    DetourTransactionBegin();
    DetourUpdateThread( GetCurrentThread() );
    DetourAttach( &(PVOID &)RealSend, MySend );
    DetourAttach( &(PVOID &)RealRecv, MyRecv );
    if ((DetourTransactionCommit()) == NO_ERROR)
    {
        AfxMessageBox("Winsock hooked");
    }
    dlg = new ControlDlg();
    m_pMainWnd = dlg;
    if(dlg->Create(IDD_CONTROL_DLG))
    {
        dlg->ShowWindow(SW_SHOW);
    }

    //ExitThread(0);
    return TRUE; <--- 
}

dlg 是属于CDriverApp

的对话框

据我观察,对话框被销毁是因为线程已经退出并且保存对话框的内存被删除。

The thread '_DllMainCRTStartup' (0x418) has exited with code 1657602048 (0x62cd0000).

我已阅读 MFC modeless dialog close immediately线程,但我的 InitInstance() 已经从一开始就返回了 true,所以这是一个不同的问题(我认为)

那么,我的问题是如何防止对话框被破坏?或者阻止线程退出?还是可以通过模态对话框实现?

最佳答案

这可能是你的问题:

Regular DLLs must have a CWinApp-derived class and a single object of that application class, as does an MFC application. However, the CWinApp object of the DLL does not have a main message pump, as does the CWinApp object of an application.

Note that the CWinApp::Run mechanism does not apply to a DLL, because the application owns the main message pump. If the DLL opens modeless dialogs or has a main frame window of its own, the application's main message pump must call a routine exported by the DLL that in turn calls the CWinApp::PreTranslateMessage member function of the DLL's application object.

http://msdn.microsoft.com/en-US/library/f22wcbea(v=vs.80 )

编辑:

这显示了如何使用 cWnd 而不是 CDialog 来完成您正在做的事情。我个人认为这是更好的方法。

http://codinganswer.com/c/cwnd-in-a-new-thread-in-a-dll.html

这是一个将消息 Hook 附加到无模式的示例。

http://support.microsoft.com/kb/q187988/

关于c++ - 线程退出时,dll 中的 mfc 无模式对话框被破坏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11193794/

相关文章:

c++ - 在 C++ 中创建一个标准的指针队列

c++ - 动态内存分配、指针成员和析构函数

c++ - 如何让一个方法访问其他模板类实例的私有(private)成员?

c++ - 如何通过 DLL 函数的参数将变量返回给 Excel?

c++ - 使用 rundll32 运行 C++ DLL - 缺少条目

c++ - 将 C 库链接到 Qt 项目的问题

c++ - dll 函数(签名)的 visual studio 项目导出列表

c++ - 如何填充矩形(c++、mfc)

visual-studio - CListCtrl 设置字体样式为粗体

c++ - DoModal() 没有将 dlg 框作为模态显示