My title says it all! I'm completely mystified by this - have been working at it all day with no resolution.

The fundamental assumption under which I am laboring is that when a Dll is loaded into a host's process, the Dll will receive a DLL_PROCESS_ATTACH message. I have been working with Dlls for years and have never had a problem with this. Frequently I'll put code in a DLL_PROCESS_ATTACH handler to open a debug log file if a #define such as MYDEBUG is defined. In most cases a person can even get away with putting a MessageBox() in DLL_PROCESS_ATTACH although the MSDN docs warn against putting any code in DLL_PROCESS_ATTACH that calls anything but kernel functions.

Are there any compilation/linking switches that specify that a dll is not to receive typical dll messages such as DLL_PROCESS_ATTACH?

I have been fooling with several development environments and computers in my attempt to get to the bottom of this. Where I ran into this problem is just recently I converted a very large (large by my standards - maybe 20,000 lines of code) eMbedded Visual C++ Windows CE project to Visual Studio 2008 C++. In that project I have several custom grid controls I wrote in a Dll. Everything works fine there and I can now run the project on desktop Windows. I decided to see if I could get the project running using GCC and CodeBlocks. I got everything to compile finally but when I tried to run the project my grid custom controls were failing on the CreateWindow() calls. First thing I did to try to see where in the Dll things were failing was to turn on my debug output logging file in the Dll. Within the Dll in the DLL_PROCESS_ATTACH code is where I have statements to open the log file and register my custom control classes (I know - RegisterClassEx() isn't a kernel function! Lets put aside that issue as its not pertinent to this problem here). Naturally, after running the program I was looking for my log file and I didn't find it. This was yesterday. All day today I've spent writting simple "Hello, World!" type Dlls and hosts with GCC and CodeBlocks trying to find out what in the devil is going on and I've gotta tell you I'm stumped!

I have no problem with Visual Studio C++ (VC9) in getting DLL_PROCESS_ATTACH. I just tried a simple VC 6 project. No problem there. Here it is...

//dllTest.cpp             dll
#include <windows.h>
#include <stdio.h>
FILE* fp=NULL;


extern "C" void __declspec(dllexport) SomeFunction(const char* szBuffer)
{
 printf("%s\n",szBuffer);
}


BOOL __stdcall DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
{
 switch (fdwReason)
 {
  case DLL_PROCESS_ATTACH:
       puts("In DLL_PROCESS_ATTACH");
       fp=fopen("Output.txt","w");
       fprintf(fp,"In DLL_PROCESS_ATTACH\n");
       break;
  case DLL_PROCESS_DETACH:
       fprintf(fp,"In DLL_PROCESS_DETACH\n");
       fclose(fp);
       break;
 }

 return TRUE;
}

/*
Output

In DLL_PROCESS_ATTACH
In DLL_PROCESS_DETACH
*/

Here's a host...

//host program
#include <stdio.h>
extern "C" __declspec(dllimport) void SomeFunction(char*);

int main()
{
 SomeFunction("Hello, World!");
 getchar();

 return 0;
}

/*
In DLL_PROCESS_ATTACH
Hello, World!
*/

This same code in CodeBlocks (GCC) produces the "Hello, World!" string from SomeFunction() which is satisfactorily imported from the Dll, but there is no file created; there is no console output from the puts() call in the Dll. Nothing! Can anyone help me understand what's going on here? You can be nasty about it if you want to. I deserve a good beating!

Haah! I was right all along! I just did a search over in the CodeBlocks forum and came up with this...

http://forums.codeblocks.org/index.php/topic,11335.0.html

Talk about a 'gotcha'! In GCC if a dll is compiled as a C++ program DllMain() is mangled and the internal fabricated DllMain will be called instead of the one in one's code. The trick is to prepend an extern "C" to DllMain()!!!!

Below is a dll and host recoded to show what works. Hopefully, if anybody else runs into this problem they will find this topic (I lost more than a day on this!).

//Example Dll
#include <windows.h>
#include <stdio.h>
FILE* fp=NULL;

extern "C" void __declspec(dllexport) SomeFunction(const char* szBuffer)
{
 printf("%s\n",szBuffer);
}

extern "C" BOOL __stdcall DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
{
 switch(fdwReason)
 {
  case DLL_PROCESS_ATTACH:
       puts("In DLL_PROCESS_ATTACH");
       fp=fopen("Output.txt","w");
       fprintf(fp,"In DLL_PROCESS_ATTACH\n");
       fclose(fp);
       break;
  case DLL_PROCESS_DETACH:
       fp=fopen("Output.txt","a");
       puts("In DLL_PROCESS_DETACH");
       fprintf(fp,"In DLL_PROCESS_DETACH\n");
       fclose(fp);
       break;
 }

 return TRUE;
}
//Example Host With Console/File Output
#include <stdio.h>
extern "C" __declspec(dllimport) void SomeFunction(char*);

int main()
{
 SomeFunction("Hello, World!");
 getchar();

 return 0;
}


/*
Display
=====================
In DLL_PROCESS_ATTACH
Hello, World!


File
=====================
In DLL_PROCESS_ATTACH
In DLL_PROCESS_DETACH
*/
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.