Microsoft KB Archive/98309

= PRB: Error in loading DLL When LIBRARY & File Names Different =

Article ID: 98309

Article Last Modified on 12/12/2003

-

APPLIES TO


 * Microsoft Visual Basic 2.0 Standard Edition
 * Microsoft Visual Basic 3.0 Professional Edition
 * Microsoft Visual Basic 2.0 Professional Edition
 * Microsoft Visual Basic 3.0 Professional Edition
 * Microsoft Visual Basic 1.0 Standard Edition

-



This article was previously published under Q98309



SYMPTOMS
The &quot;Error in loading DLL&quot; error message occurs if you call a DLL and the LIBRARY name of the DLL is different from the filename.



STATUS
This behavior is by design. Visual Basic ensures that the LIBRARY name and filename of a DLL match. If they don't match, Visual Basic generates the &quot;Error in loading DLL&quot; error.

Visual Basic version 3.0 does not require that the LIBRARY name and the filename be the same for a DLL. However, unless you are designing a DLL specifically to be called from Visual Basic version 3.0 or some other application not written using Visual Basic, we recommend that you use the same name for both the LIBRARY name and filename of a DLL.



MORE INFORMATION
When creating a Windows DLL, you must specify the LIBRARY name of the DLL in the module-definition (.DEF) file for the DLL. In order to call any procedure contained within the DLL from Visual Basic, the LIBRARY name given in the module-definition file must be the same as the filename for the DLL.

Steps to Reproduce Behavior
Perform the following steps to build a DLL that will lead to a &quot;Error in loading DLL&quot; error when called from Visual Basic. To build the following application, you will need to use a C compiler capable of creating Windows Dynamic Link Libraries (DLLs).

  Create a C source code file that contains the following code and save the file as TEST.C.    #include 

VOID FAR PASCAL test ( VOID ); VOID FAR PASCAL test ( VOID ) {      //The contents of any procedure in the DLL is not important //Define this procedure to be called from Visual Basic return; }

//   // Initialize library. This routine is called when the first // client loads // the DLL. //   int FAR PASCAL LibMain (      HANDLE hModule,       WORD   wDataSeg,       WORD   cbHeapSize,       LPSTR  lpszCmdLine    ) {      // Avoid warnings on unused (but required) formal parameters wDataSeg = wDataSeg; cbHeapSize = cbHeapSize; lpszCmdLine = lpszCmdLine;

return 1; }

//   // WEP //   int FAR PASCAL WEP(int fSystemExit);

//   // Performs cleanup tasks when the DLL is unloaded. WEP is   // called automatically by Windows when the DLL is unloaded (no    // remaining tasks still have the DLL loaded). It is strongly // recommended that a DLL have a WEP function, even if it does // nothing but returns success (1), as in this example. //   int FAR PASCAL WEP (       int fSystemExit    ) {       // Avoid warnings on unused (but required) formal parameters fSystemExit = fSystemExit;

return 1; }

  Create a module-definition file (DEF) file that contains the following code and save the file as TEST.DEF.   LIBRARY DIFFNAME

DESCRIPTION 'Sample DLL where LIBRARY name != filename'

EXETYPE WINDOWS

CODE PRELOAD MOVEABLE DISCARDABLE DATA PRELOAD SINGLE MOVEABLE

EXPORTS WEP @1 RESIDENTNAME TEST @2

  Compile TEST.C from the command line as follows:      CL /c /ASw /W3 TEST.C

  Link the resulting TEST.OBJ file as follows:      LINK /NOE /NOD TEST.OBJ+LIBENTRY.OBJ,TEST.DLL,,LIBW+SDLLCEW,TEST.DEF;

</li> Copy TEST.DLL to the Windows directory.</li> Start Visual Basic or from the File menu, choose New Project (ALT, F, N) if Visual Basic is already running. Form1 is created by default.</li>  Add the following code to the general declarations section of Form1: Declare Sub Test Lib &quot;TEST.DLL&quot;

</li>  Add the following code to the Form_Load event of Form1: Sub Form_Load Call TEST End Sub

</li> From the Run menu, choose Start (ALT, R, S) or press the F5 key to run the program.</li></ol>

Execution will break on the Call statement in the Form_Load event, and you will receive the error &quot;Error in loading DLL.&quot;

To avoid this error, change the LIBRARY name in TEST.DEF, under step 2, from DIFFNAME to TEST. Then do step 4 to link in the new module-definition file. Follow steps 5 through 8 again and you should no longer see the &quot;Error in loading DLL&quot; error message.

Additional query words: 2.00 3.00

Keywords: kbprb KB98309

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.