xxbidiao
xxbidiao

Reputation: 874

Dllmain not being called?

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.

Upvotes: 7

Views: 12699

Answers (4)

xxbidiao
xxbidiao

Reputation: 874

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!

Upvotes: 8

sean e
sean e

Reputation: 11925

Is LoadLibrary actually returning a value (what is displayed by your cout<<hinstDLL call) ?

Is your dll in the same directory or available via the PATH environment variable?

Do you have multiple versions of your dll in different places (release vs debug)?

Upvotes: 0

user2100815
user2100815

Reputation:

You are severely limited in what you can do in DLLMain. specifically, doing any I/O is normally a no-no. It's there to do some simple initialisation, not to act like main() does in an executable.

Upvotes: 1

Puppy
Puppy

Reputation: 146910

I suspect that it's your console interaction code that is off. You could try doing something a little less subtle, like opening a window or ShellExecute()ing a sound.

Upvotes: 1

Related Questions