开发者

C++ DLL Function Export. DLL does not stay loaded

hi I've got the following problem, and I cannot figure out what is going on.

DLL code mylib.cpp (mylib.dll):

#include <Windows.h>
#include <tchar.h>

__declspec(dllexport) LRESULT CALLBACK HookProc(int nCode, WPARAM wParam, LPARAM lParam) {
    return CallNextHookEx(NULL, nCode, wParam, lParam);
    }

BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserverd){

// Perform actions based on the reason for calling.
switch( fdwReason ) 
{ 
    case DLL_PROCESS_ATTACH:
     // Initialize once for each new process.
     // Return FALSE to fail DLL load.
        MessageBox(NULL,
        _T("DLL Loaded"),
        _T("DLL Loaded"),
        NULL);
        break;

    case DLL_THREAD_ATTACH:
     // Do thread-specific initialization.
        MessageBox(NULL,
        _T("DLL Unloaded"),
        _T("DLL Unloaded"),
        NULL);
        break;
    }
    return TRUE;
}

Program code my_prog.cpp:

#include <Windows.h>
#include <tchar.h>

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow){

    FARPROC pHookProc;
    static HINSTANCE hInstDLL;

    hInstDLL = LoadLibrary(_T("mylib.dll"));
    pHookProc = GetProcAddress(hInstDLL, "HookProc");
    if (!pHookProc) {
        MessageBox(NULL,
            _T("GetProcAddress failed"),
            _T("GetProc开发者_Go百科Address failed"),
            NULL);
    }
    return 0;
}

Both files compile without any errors. Whenever I run my_prog.exe it would give a message "DLL Loaded", then right away It would give message "DLL unloaded" and, as a result, GetProcAddress() fails. Could someone shine some light on it for me please. Why does it unload the DLL instantaneously?

Thank you all in advance.

EDITED:

I've replaced DLL_THREAD_ATTACH by DLL_PROCESS_DETACH as c-smile suggested. I check and function exports as: long __stdcall HookProc(int,unsigned int,long) (1)(0x00001000). GetProcAddress() still fails. I get "DLL Loaded", GetProcAddress() failed, "DLL Unloaded"


  1. Replace DLL_THREAD_ATTACH by DLL_PROCESS_DETACH
  2. Make sure that your function is exported exactly as "HookProc".
  3. If no use .def file to define export name of the function.


Two things:

  1. Don't assume that DLL_THREAD_ATTACH means things are going wrong. It's something that should happen when something links to your DLL as c-smile said.
  2. Since this is a C++ compilation unit, your export will have the mangled name ?HookProc@@YGJHIJ@Z - that's why GetProcAddress(hInstDLL, "HookProc") fails - it's not the right name.

    Use

    extern "C"  __declspec(dllexport) LRESULT CALLBACK HookProc(int nCode, WPARAM wParam, LPARAM lParam);
    

    And you'll get a more manageable name of _HookProc@12, so GetProcAddress(hInstDLL, "_HookProc@12") should work.

If you want an even nicer name, I think you'll need to use a DEF file, From http://msdn.microsoft.com/en-us/library/3y1sfaz2.aspx:

dllexport of a C++ function will expose the function with C++ name mangling. If C++ name mangling is not desired, either use a .def file (EXPORTS keyword) or declare the function as extern "C".

A .def file like the following should do the trick (note: the EXPORTS keyword seems to be case sensitive):

EXPORTS
    HookProc=_HookProc@12

Pass the .def file to the linker using the /def:whatever.def option.


Instead of MessageBox, can you try printf? MessageBox is modal and it is probably messing things up.


In the DLL code, when using DLL_PROCESS_DETACH:

"The lpReserved parameter indicates whether the DLL is being unloaded as a result of a FreeLibrary call, a failure to load, or process termination."

So I would check that parameter, it might help narrow down the problem.

I would also be checking the return value of LoadLibrary to make sure the actual Load succeeded. If the LoadLibrary is failing you can try using 'GetLastError()' API for more information.

Also you are not doing a 'FreeLibrary(hInst);' after your LoadLibrary.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜