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

ID: Q98309


The information in this article applies to:
  • Microsoft Visual Basic Standard and Professional Editions for Windows, versions 2.0, 3.0
  • Microsoft Visual Basic programming system for Windows, version 1.0


SYMPTOMS

The "Error in loading DLL" 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 "Error in loading DLL" 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 "Error in loading DLL" 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).
  1. Create a C source code file that contains the following code and save the file as TEST.C.
    
        #include <windows.h>
    
        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;
        }
     


  2. 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
     


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


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


  5. Copy TEST.DLL to the Windows directory.


  6. 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.


  7. Add the following code to the general declarations section of Form1:
    
          Declare Sub Test Lib "TEST.DLL" ()
     


  8. Add the following code to the Form_Load event of Form1:
    
          Sub Form_Load ()
             Call TEST
          End Sub
     


  9. From the Run menu, choose Start (ALT, R, S) or press the F5 key to run the program.


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

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 "Error in loading DLL" error message.

Additional query words: 2.00 3.00

Keywords :
Version :
Platform :
Issue type :


Last Reviewed: June 1, 1999
© 2000 Microsoft Corporation. All rights reserved. Terms of Use.