Microsoft KB Archive/314657

= PRB: DLL Fails to Initialize If User-Defined Entry Point Is Used =

Article ID: 314657

Article Last Modified on 4/4/2007

-

APPLIES TO


 * Microsoft Visual C++ .NET 2002 Standard Edition

-



This article was previously published under Q314657



SYMPTOMS
When you use the /clr and /entry options to build a dynamic link library (DLL), the DLL may not be initialized properly. As a result, the application may not start.



CAUSE
This happens if the user-defined entry point function has an incorrect signature.



RESOLUTION
If you are using the /entry switch along with /clr to compile your DLL, use the following entry point parameters: BOOL WINAPI (LPVOID hinstDLL,                                  DWORD  dwReason,                                   LPVOID lpvReserved) -or- BOOL WINAPI (void* hinstDLL,                                  DWORD dwReason,                                   void* lpvReserved)



Steps to Reproduce the Behavior
The following steps demonstrate the problem:   Compile the following code to create a DLL:
 * 1) include 

int g_x = 0;

BOOL WINAPI MyDllMain( HINSTANCE hinstDLL,  // handle to the DLL module  DWORD fdwReason,     // reason for calling function  LPVOID lpvReserved   // reserved ) /* To get rid of the error uncomment this code and comment the code above BOOL WINAPI MyDllMain( LPVOID hinstDLL,    DWORD  fdwReason,       LPVOID lpvReserved   )*/ { g_x = 1; return 1; }

extern &quot;C&quot; __declspec(dllexport) int GetVal { return g_x; }                    Use the following command lines to build the DLL. This will create Library.dll and Library.lib.

cl /c /clr /Zi library.cpp

link /debug /entry:MyDllMain /dll library.obj

  Compile the following code to create an application that uses Library.dll: extern &quot;C&quot; __declspec(dllimport) int GetVal;
 * 1) include 

int main {   printf(&quot;%d\n&quot;,GetVal); return 0; }                    Use the following command lines to build the DLL. This will create App.exe.

cl /c /clr /Zi app.cpp

link /debug app.obj library.lib

</li> Run App.exe. You will receive the following error message:

Application failed to initialize properly (0xc0000142). Click on OK to terminate the application.

</li></ol>

<div class="references_section">