www.digitalmars.com         C & C++   DMDScript  

c++.windows.32-bits - DllMain Not Being Executed

reply JimP <JimP_member pathlink.com> writes:
Ihave a dll program that compiles with no warnings or errors.  It is compiled
with the options:
dmc.exe -mn -c   -x -WD -w-

and linked with the options:
link.exe /NOLOGO /DELEXECUTABLE /EXETYPE:NT /SUBSYSTEM:WINDOWS

The calling program uses explicit linking with LoadLibrary and GetProcAddress.
Both of these functions execute without getting an error.  But the DllMain
function is never called.  I put a messagebox in the DllMain function to verify
this.

Why isn't the DllMain function being called????
Dec 21 2005
parent reply JimP <JimP_member pathlink.com> writes:
In article <doc7a5$2veo$1 digitaldaemon.com>, JimP says...
Ihave a dll program that compiles with no warnings or errors.  It is compiled
with the options:
dmc.exe -mn -c   -x -WD -w-

and linked with the options:
link.exe /NOLOGO /DELEXECUTABLE /EXETYPE:NT /SUBSYSTEM:WINDOWS

The calling program uses explicit linking with LoadLibrary and GetProcAddress.
Both of these functions execute without getting an error.  But the DllMain
function is never called.  I put a messagebox in the DllMain function to verify
this.

Why isn't the DllMain function being called????
I tried this with the Borland and GCC (MinGW) compilers. It worked OK with both. It must be something about the Digital Mars compiler. An option? Something I am omitting? Or is it just not executed with explicit linking? Any suggestions would be appreciated.
Dec 22 2005
parent reply "Gisle Vanem" <giva users.sourceforge.net> writes:
"JimP" <JimP_member pathlink.com> wrote:

 I tried this with the Borland and GCC (MinGW) compilers.  It worked OK with
 both.  It must be something about the Digital Mars compiler.  An option?
 Something I am omitting?  Or is it just not executed with explicit linking?
How did you declare DllMain()? --gv
Dec 24 2005
parent reply Alex Roschin <Alex_member pathlink.com> writes:
In article <dojkr4$gum$1 digitaldaemon.com>, Gisle Vanem says...
"JimP" <JimP_member pathlink.com> wrote:

 I tried this with the Borland and GCC (MinGW) compilers.  It worked OK with
 both.  It must be something about the Digital Mars compiler.  An option?
 Something I am omitting?  Or is it just not executed with explicit linking?
How did you declare DllMain()? --gv
I have the same problem - neither DllMain, nor constructors for global objects are executed. I am making a C++ DLL that should be loaded by LoadLibrary call. Here is a sample code: // dll.cpp #include<windows.h> extern "C" BOOL WINAPI DllMain(HINSTANCE hinst,DWORD reason,LPVOID lpReserved) { MessageBox(NULL,"DllMain","dll",MB_OK); return TRUE; } // Global object class TSomeClass { public: int i; TSomeClass(void){MessageBox(NULL,"Constructor","dll",MB_OK);}; }; TSomeClass SomeObject; // Function extern "C" __declspec(dllexport) int CALLBACK DoSomething(int CallMode,LPVOID Data,LPVOID ExtParam) { static BOOL FirstCall=TRUE; if(FirstCall) { FirstCall=FALSE; MessageBox(NULL,"Function","dll",MB_OK); } return 0; } The compiler command line: c:\prog\dm\bin\dmc.exe dll.cpp -c -mn -WD -Ae -I"c:\prog\dm\include" -I"c:\prog\dm\include\Win32" Linker command line: c:\prog\dm\bin\link.exe /DELEXECUTABLE /NODEBUG /EXETYPE:NT dll.obj , testdll.dll , , user32.lib kernel32.lib Everything compiles without errors. When I call LoadLibrary I see neither "DllMain" message, nor "Constructor" message, but function DoSomething executes normally and produces "Function" message at first call. I tried to add "/ENTRY:_DllMainCRTStartup" linker switch as I've seen in some examples, but nothing changes. Please tell me what am I doing wrong.
Mar 27 2006
parent reply Bertel Brander <bertel post4.tele.dk> writes:
Alex Roschin wrote:
 Please tell me what am I doing wrong.
I took my code from: http://home20.inet.tele.dk/midgaard/tipwin20060212.html And changed the source code for the dll to: #include <windows.h> #include "mydll.h" class DllClass { public: DllClass() { MessageBox(0, "Hello World", "DllClass", MB_OK); } }; DllClass DllObject; __declspec(dllexport) BOOL APIENTRY DllMain(HANDLE hModule, DWORD ul_reason_for_call, LPVOID lpReserved) { switch (ul_reason_for_call) { case DLL_PROCESS_ATTACH: case DLL_THREAD_ATTACH: case DLL_THREAD_DETACH: case DLL_PROCESS_DETACH: MessageBox(0, "Something", "DllMain", MB_OK); break; } return TRUE; } __declspec(dllexport) int DoSomething(int a, int b) { return a*b*2; } __declspec(dllexport) Whatever::Whatever(int aX, int aY) : X(aX), Y(aY) { } __declspec(dllexport) int Whatever::Get() { return X*Y; } And build with these commands: dmc -mn -WD mydll.cpp kernel32.lib implib.exe /noi mydll.lib mydll.dll dmc mytest.cpp mydll.lib And the MessageBox'es popup as expected. It can be noted that I don't use LoadLibrary, but use the dll throug a .lib created by implib.exe. I don't know if it will make any difference. -- Absolutely not the best homepage on the net: http://home20.inet.tele.dk/midgaard But it's mine - Bertel
Mar 28 2006
parent reply Alex Roschin <Alex_member pathlink.com> writes:
In article <e0bupn$2v9n$1 digitaldaemon.com>, Bertel Brander says...
I took my code from:
http://home20.inet.tele.dk/midgaard/tipwin20060212.html
Thank you! I've found the thing that caused the problem: 1) When I compile and link the example with the single command (without "-c" option), everything works fine - I see all message boxes. 2) When I try to compile and to link separately with dmc.exe -c -mn -WD mydll.cpp link.exe mydll.obj , testdll.dll , , kernel32.lib neither DllMain, nor constructor message boxes show up. I believe that in first case dmc.exe calls link.exe with certain options which I don't know. I tried to play with some, but in vain. Since the single command build works fine, I think I will use it. Thanks again.
Mar 29 2006
parent reply Nic Tiger <g_tiger progtech.ru> writes:
Alex Roschin пишет:

 In article <e0bupn$2v9n$1 digitaldaemon.com>, Bertel Brander says...
 I took my code from:
 http://home20.inet.tele.dk/midgaard/tipwin20060212.html
Thank you! I've found the thing that caused the problem: 1) When I compile and link the example with the single command (without "-c" option), everything works fine - I see all message boxes. 2) When I try to compile and to link separately with dmc.exe -c -mn -WD mydll.cpp link.exe mydll.obj , testdll.dll , , kernel32.lib neither DllMain, nor constructor message boxes show up. I believe that in first case dmc.exe calls link.exe with certain options which I don't know. I tried to play with some, but in vain. Since the single command build works fine, I think I will use it. Thanks again.
It seems that OPTLINK will not generate valid DLL when .def is not specified. DMC.EXE in fact generates simple .DEF and supplies it to linker: LIBRARY "tasklet.dll" DESCRIPTION 'tasklet' EXETYPE NT SUBSYSTEM WINDOWS CODE SHARED EXECUTE DATA WRITE Neither LIBRARY, nor CODE SHARED cannot be specified with OPTLINK switches, hence I think they (or their absence) are cause of troubles. Nic Tiger.
Dec 16 2009
parent Name <e-mail test.com> writes:
Web News test...
Apr 26 2010