Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dllmain not being called?

Tags:

c++

I was writing a dllmain like this:

#include "main.h"
#include "init.h"
#include <iostream>
BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
{
//std::cout<<"hi\n"; //only for debug. did not shown.
switch (fdwReason)
{
    case DLL_PROCESS_ATTACH:
        // attach to process
        // return FALSE to fail DLL load
        //std::cout<<"hello\n"; //only for debug. did not shown.
        init(); //did not run :(
        break;

    case DLL_PROCESS_DETACH:
        // detach from process
        break;

    case DLL_THREAD_ATTACH:
        // attach to thread
        break;

    case DLL_THREAD_DETACH:
        // detach from thread
        break;
}
return TRUE; // succesful
}

but after a test program uses LoadLibrary(), nothing happened, no hello or hi on the screen. Would you like to figure out the problem? Many Thanks!

P.S. I have watched the question DllMain not being called but it still not help.

PS2: the caller program is like

int main()
{
cout<<"This is a test program to test.\n";
HINSTANCE hinstDLL;
hinstDLL=LoadLibrary("ijl15.dll");
cout<<"Look like everything goes well.\n";
cout<<hinstDLL;
return 0;
}

The tester program outputs:

This is a test program to test.
Look like everything goes well.
0x6a980000
Process returned 0 (0x0)   execution time : 0.007 s
Press any key to continue.
like image 259
xxbidiao Avatar asked May 28 '11 13:05

xxbidiao


People also ask

Does LoadLibrary call DllMain?

The DllMain function is an optional method of entry into a dynamic-link library (DLL). If the function is used, it is called by the system when processes and threads are initialized and terminated, or upon calls to LoadLibrary and FreeLibrary. A handle to the DLL.

Is DllMain required?

DllMain is not mandatory. If you have some initialization code required to run when loading the dll, you should create a DllMain function, and treat the initialization there. Otherwise it's not required.

How does DllMain work?

When the system calls the DllMain function with the DLL_PROCESS_ATTACH value, the function returns TRUE if it succeeds or FALSE if initialization fails. If the return value is FALSE when DllMain is called because the process uses the LoadLibrary function, LoadLibrary returns NULL.

What is DLL entry point?

A DLL can optionally specify an entry-point function. If present, the system calls the entry-point function whenever a process or thread loads or unloads the DLL. It can be used to perform simple initialization and cleanup tasks.


1 Answers

After some tries(alot :( ) I found that I missed

#define DLL_EXPORT extern "C" __declspec(dllexport)

This makes the function name correctly and finally the DLLMain is successfully called. Anyway thanks you all!

like image 80
xxbidiao Avatar answered Oct 07 '22 09:10

xxbidiao