My DllMain is not getting invoked.
-
I'm trying to use DllMain() to take care of some library/thread initialization and cleanup stuff, but no matter what I try I cannot get DllMain invoked for any of the four events for which it is supposed to be getting invoked! (I.e., process attach, thread attach, thread detach, process detach.) I found the following sentence in the MSDN description of DllMain():
"DllMain is a placeholder for the library-defined function name. You must specify the actual name you use when you build your DLL. For more information, see the documentation included with your development tools."
Is there some Dev Studio project setting I should be making to identify the name of my library entry point function? Another tidbit of information: I'm using simple load-time linking for my DLL. The MSDN DllMain description makes reference to LoadLibrary and FreeLibrary (functions you use for run-time dynamic linking). I sould be interested in hearing confirmation that DllMain IS compatible with simple load-time dynamic linking. Thanks much, Matt
-
I'm trying to use DllMain() to take care of some library/thread initialization and cleanup stuff, but no matter what I try I cannot get DllMain invoked for any of the four events for which it is supposed to be getting invoked! (I.e., process attach, thread attach, thread detach, process detach.) I found the following sentence in the MSDN description of DllMain():
"DllMain is a placeholder for the library-defined function name. You must specify the actual name you use when you build your DLL. For more information, see the documentation included with your development tools."
Is there some Dev Studio project setting I should be making to identify the name of my library entry point function? Another tidbit of information: I'm using simple load-time linking for my DLL. The MSDN DllMain description makes reference to LoadLibrary and FreeLibrary (functions you use for run-time dynamic linking). I sould be interested in hearing confirmation that DllMain IS compatible with simple load-time dynamic linking. Thanks much, Matt
Matthew, further to your (previous) question: DllMain of your *.dll library gets called on 1.program startup, if your user staticly linked to the *.lib library of your *.dll project when compiling his app. (also called load time or explicit linking) 2.your user calling LoadLibrary, FreeLibrary from his code (also called runtime or implicit linking) In case 1./ the user of your library has to get from you a *.lib and a *.h file of your library in order to compile his project, and of course your *.dll to run with your *.dll attached. In case 2./ the user of your library has to get from you an *.h file of your library (i.e. function prototypes) to compile his code (by calling LoadLibrary, GetProcAddress and FreeLibrary) and your *.dll to run the library functions. Peter Molnar
-
Matthew, further to your (previous) question: DllMain of your *.dll library gets called on 1.program startup, if your user staticly linked to the *.lib library of your *.dll project when compiling his app. (also called load time or explicit linking) 2.your user calling LoadLibrary, FreeLibrary from his code (also called runtime or implicit linking) In case 1./ the user of your library has to get from you a *.lib and a *.h file of your library in order to compile his project, and of course your *.dll to run with your *.dll attached. In case 2./ the user of your library has to get from you an *.h file of your library (i.e. function prototypes) to compile his code (by calling LoadLibrary, GetProcAddress and FreeLibrary) and your *.dll to run the library functions. Peter Molnar
Peter, Thanks for your confirmation that this at least should work. So why then is my DllMain not getting invoked. Here's my current inplementation:
#include "MyHeader.h"
#include "windows.h"BOOL DllMain(HINSTANCE , DWORD pReason, LPVOID ) {
try {
printf("I'M HERE!\n");
int *x = NULL;
*x = 20;
switch (pReason) {
case DLL_PROCESS_ATTACH:
MyProcessAttach();
MyThreadAttach();
break;
case DLL_THREAD_ATTACH:
MyThreadAttach();
break;
case DLL_THREAD_DETACH:
MyThreadDetach();
break;
case DLL_PROCESS_DETACH:
MyThreadDetach();
MyProcessDetach();
break;
default:
break;
}
}
catch(std::exception& x) {
std::cerr << x.what() << std::endl;
return FALSE;
}
return FALSE;
}where all the stuff in bold is just some garbage I've added trying to get it to print something, or crash or generate some failed load situation to show me that the function is getting called. It would certainly seem this function is never getting invoked. What the heck is going on? Again, do I need some project setting to identify this as an entry point function for the library? Matt
-
Peter, Thanks for your confirmation that this at least should work. So why then is my DllMain not getting invoked. Here's my current inplementation:
#include "MyHeader.h"
#include "windows.h"BOOL DllMain(HINSTANCE , DWORD pReason, LPVOID ) {
try {
printf("I'M HERE!\n");
int *x = NULL;
*x = 20;
switch (pReason) {
case DLL_PROCESS_ATTACH:
MyProcessAttach();
MyThreadAttach();
break;
case DLL_THREAD_ATTACH:
MyThreadAttach();
break;
case DLL_THREAD_DETACH:
MyThreadDetach();
break;
case DLL_PROCESS_DETACH:
MyThreadDetach();
MyProcessDetach();
break;
default:
break;
}
}
catch(std::exception& x) {
std::cerr << x.what() << std::endl;
return FALSE;
}
return FALSE;
}where all the stuff in bold is just some garbage I've added trying to get it to print something, or crash or generate some failed load situation to show me that the function is getting called. It would certainly seem this function is never getting invoked. What the heck is going on? Again, do I need some project setting to identify this as an entry point function for the library? Matt
I have created a small project which contains 4 lines of code:
#include <windows.h>
BOOL DllMain(HINSTANCE , DWORD , LPVOID )
{
::MessageBox(NULL,"test","Caption",MB_OK);
return TRUE;
}and call it
HINSTANCE hInstance = LoadLibrary("pathto.dll");
works just fine. Try the same. What is hInstance when you debug? Peter Molnar
-
Peter, Thanks for your confirmation that this at least should work. So why then is my DllMain not getting invoked. Here's my current inplementation:
#include "MyHeader.h"
#include "windows.h"BOOL DllMain(HINSTANCE , DWORD pReason, LPVOID ) {
try {
printf("I'M HERE!\n");
int *x = NULL;
*x = 20;
switch (pReason) {
case DLL_PROCESS_ATTACH:
MyProcessAttach();
MyThreadAttach();
break;
case DLL_THREAD_ATTACH:
MyThreadAttach();
break;
case DLL_THREAD_DETACH:
MyThreadDetach();
break;
case DLL_PROCESS_DETACH:
MyThreadDetach();
MyProcessDetach();
break;
default:
break;
}
}
catch(std::exception& x) {
std::cerr << x.what() << std::endl;
return FALSE;
}
return FALSE;
}where all the stuff in bold is just some garbage I've added trying to get it to print something, or crash or generate some failed load situation to show me that the function is getting called. It would certainly seem this function is never getting invoked. What the heck is going on? Again, do I need some project setting to identify this as an entry point function for the library? Matt
If this is VC6: in the Link tab, select the Output category, and ensure that Entry Point Symbol is either blank or set to _DllMainCRTStartup. You should also check that the
/NOENTRY
switch is not specified. Both of you: be careful with your dependencies. The only routines you can guarantee will be initialised before your entry point is called are those implemented byKERNEL32.DLL
. You should also be able to call C library functions if you're linking with the C run-time. -
I have created a small project which contains 4 lines of code:
#include <windows.h>
BOOL DllMain(HINSTANCE , DWORD , LPVOID )
{
::MessageBox(NULL,"test","Caption",MB_OK);
return TRUE;
}and call it
HINSTANCE hInstance = LoadLibrary("pathto.dll");
works just fine. Try the same. What is hInstance when you debug? Peter Molnar
Much thanks to Peter Molnar for mailing me a completely trivial DevStudio project containing a DllMain() that got properly invoked. What I was missing (and what I had never seen documented) was the APIENTRY declaration for the DLL entry point function. The DllMain must be declared like this:
BOOL APIENTRY DllMain(HINSTANCE , DWORD , LPVOID )
Once those 8 pesky characters were added, it worked great. Also, for those who care, this works independently of the application type (i.e., works for both console apps and windows apps) and (as Peter said in an earlier message on this thread) works independently of how the library is loaded. (I.e., you can use either load-time dynamic linking or run-time dynamic linking.) Matt