PRB: DLL Fails to Initialize If User-Defined Entry Point Is Used (314657)



The information in this article applies to:

  • Microsoft Visual C++ .NET (2002)

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 <your_entrypoint_name>(LPVOID hinstDLL,
                                   DWORD  dwReason,
                                   LPVOID lpvReserved)
				
-or-
BOOL WINAPI <your_entrypoint_name>(void* hinstDLL,
                                   DWORD dwReason,
                                   void* lpvReserved)
				

MORE INFORMATION

Steps to Reproduce the Behavior

The following steps demonstrate the problem:
  1. Compile the following code to create a DLL:
    #include <windows.h>
    
    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 "C" __declspec(dllexport) int GetVal()
    {
    return g_x;
    }
    					
  2. 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

  3. Compile the following code to create an application that uses Library.dll:
    #include <stdio.h>
    extern "C" __declspec(dllimport) int GetVal();
    
    int main()
    {
    	printf("%d\n",GetVal());
    	return 0;
    }
    					
  4. 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

  5. Run App.exe. You will receive the following error message:
    Application failed to initialize properly (0xc0000142). Click on OK to terminate the application.

REFERENCES

For other top-hit Visual C++ .NET Microsoft Knowledge Base articles, visit the following Microsoft Web site: For more information about the Visual C++ Compiler options, visit the following Microsoft Web sites:

Modification Type:MajorLast Reviewed:7/23/2002
Keywords:kbCompiler kbCRT kbDSupport kbIJW kbManaged kbprb kbProd2Web KB314657