0

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?

Community
  • 1
  • 1
the91end
  • 101
  • 1
  • 7

1 Answers1

1

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/

ScottTx
  • 1,483
  • 8
  • 12
  • does it mean the dialog doesn't have a message handling? So i must add an exported function that calls the CWinApp::PreTranslateMessage ? – the91end Jun 26 '12 at 02:34
  • Correct, modeless dialogs do not have their own message handler. This processs hooks into the message loop of the main application. – ScottTx Jun 26 '12 at 14:05
  • It says `the application's main message pump must call a routine exported by the DLL`, does it mean the process which loads the dll? As i described before, i injected the dll into a running process where i don't have control over it. – the91end Jun 26 '12 at 14:18
  • A couple of things you should probably do - create your dialog on a separate thread and then install a windows message hook. I've edited my answer with links that may help. – ScottTx Jun 26 '12 at 15:52
  • i have looked into your second link, and i have researched a little about it. It seems a second thread shouldn't be created inside InitInstance because it will crash. So i have to create the thread from the injector right? So i'm thinking of creating 2 separate dll, 1 for hooking winsock and the other for handling the dialog. Is this what you mean? – the91end Jun 26 '12 at 18:00
  • You only need one DLL. Don't start the thread in InitInstance. Put that code in another function. Call that from the application that is installing the hook (your app). This lets the DLL initialize properly before the thread is created. All your winsock hooking and modeless dialog code run on the thread. – ScottTx Jun 26 '12 at 18:19
  • thank you ScottTx. Now it's working. I don't even need the modeless dialog, i change it into a modal. – the91end Jun 27 '12 at 06:11