0
votes

I want to open a MFC modeless dialog from a MFC dll injected into another process, the dll's job is to hook the winsock send & recv, and the dialog will be the interface to communicate with the dll. The dll should be able to run the hook while the dialog is running.

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 is the dialog which is a member of CDriverApp

From what i have observed, the dialog is destroyed because the thread has exited and the memory that hold the dialog is removed.

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

I have read MFC modeless dialog close immediately thread, but my InitInstance() already returned true from the first place, so it's a different problem (i think)

So, my question is how to prevent the dialog from destroyed? Or perhaps prevent the thread from exit? or is it doable with a modal dialog?

1

1 Answers

1
votes

This may be your problem:

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)

EDIT:

THis shows how to do what you are doing with a cWnd instead of a CDialog. Personally I think thats a better way to go.

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

Here is an example of attaching a message hook to a modeless.

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