Creating .lib from .dll

Posted on 1997-06-16
Last Modified: 2013-12-03
it seems that implib.exe is gone from msvc 4.x.  Is there still a way to create a .lib file from a .dll?
Question by:pjx826
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment

Accepted Solution

Shrif earned 50 total points
ID: 1398419

How to Create 32-bit Import Libraries Without .OBJs or Source

This article explains how to create an import library given a .DLL for which you have no source code or object modules. There is no 32-bit utility that can create an import library from a .DLL, as there was with 16-bit versions of Visual C++.
NOTE: This method may not work with DLLs generated with non-Microsoft development tools.

Normally, when building a .DLL or any target that exports functions or data items, an import library (and exports file) is generated as part of the linking process. But in the case of a third-party .DLL that does not ship with an import library, you may need to generate an import library in order to use the .DLL successfully using load-time dynamic linking. An import library is not needed for run-time dynamic linking.
There are two ways to create an import library given a .DLL:

(a) Create a .DEF file for use with the LIB /DEF: command.
(b) Stub out functions, and use the .OBJ files created to mimic the import/export relationships. Then use the LIB /DEF: command to create the import library.

Creating a .DEF file
The only time you can use a .DEF file to create an import library from a .DLL for which you do not have the source code or object modules is if the .DLL exports functions via a C interface. Specifically, the functions need to have been declared to use the C calling convention. This is specified by the _cdecl attribute, normally used in the prototype for the function. Note that if no attribute is specified, _cdecl is the default when /Gz (_stdcall is the default) or /Gr (_fastcall is the default) is not specified on the CL command line. The reason for this limitation is based on an assumption made by the LIB utility about the nature of the calling convention of exported functions.
Given a .DLL with functions exported via a C interface, you can create an import library by following these steps:

(a) Use DUMPBIN /EXPORTS <.DLL file name> to obtain the list of exported symbols for the .DLL in question. The symbols appear in the "name" column of the table whose headings are "ordinal hint name."

(b) Create a .DEF file that contains an EXPORTS section with the names of the functions listed in the "name" column of the DUMPBIN output.

(c) For _cdecl functions, the symbol appears just as it would when used in the calling program. Just place this symbol in the EXPORTS section of the .DEF file.

(d) Use LIB /DEF:<.DEF file name> to generate the import library and exports file. The base name of the import library will be the base name of the .DEF file. Use /OUT: to control the output library name.

Stubbing Out Functions
For exported functions that use calling conventions other than C, the situation is a little more complex. This is especially true when you consider C++ functions and the more complex name decoration schemes involved. To use this method, you must at least have the header file that describes the .DLL's interface.
To create stubbed functions from prototypes in a header file:

(a) When "__declspec(dllimport)" is used in a prototype or declaration, change it to "__declspec(dllexport)."

(b) For functions that do not return a value, for C functions in C source, and for C functions in C++ source code (used with the 'extern "C"' construct), replace the semicolon that terminates the function prototype with a matched pair of curly braces ("{}").

(c) For C++ functions (global or member) that return a value, you must create a dummy body for the function, and return a dummy value of the proper type. (Not having a return statement in the function is illegal.) This goes for class member functions, as well. Keep in mind that the purpose of this procedure is to trick the LIB utility into generating the correct import library, so these dummy bodies have no effect.

(d) For C++ classes, you can stub out the member functions by using the prototypes in the class declaration, as long as you disable function inlining when you compile.

(e) Function arguments are usually just specified by type in a header file. For example, Geta(int). A dummy argument identifier must be specified when adding the dummy function body Geta(int x). Otherwise the error C2055 is generated.

If the header file that describes MYDLL.DLL looks like:

   // mydll.H

   extern "C" __declspec(dllimport) void _stdcall Function(void);

   class __declspec(dllimport) CMyClass {
        int a;
        long b;
        int Geta(int);
        long Getb();

The dummy source file you use to build the import library should look like:

   // mydll.CPP

   extern "C" __declspec(dllexport) void _stdcall Function(void) {}

   class __declspec(dllexport) CMyClass {
        int a;
        long b;
        int Geta(int x) {return 111;}
        long Getb() {return 111;}
        CMyClass() {}

Once the functions are stubbed out, all you need to do is compile the source file into an .OBJ file:

   CL /c /Ob0 mydll.CPP

NOTE: Disabling function inlining is required to force generation of symbols for the functions defined in CMyClass. If function inlining were enabled, the compiler would notice that there are no references to the member functions in the translation unit, so it would discard the function bodies. See the discussion on inline function expansion under Optimizations in the Visual C++ CL Command line reference.Once you have .OBJ files, you can use LIB /DEF: to create the import library (.LIB) and exports file (.EXP):

   LIB /DEF: mydll.OBJ

For more information on the LIB command, consult the "LIB Reference" in the Visual C++ Books Online.Also, see the following article:

   Q140485 - Exporting PASCAL-Like Symbols in 32-bit DLLs


Featured Post

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This article shows how to make a Windows 7 gadget that accepts files dropped from the Windows Explorer.  It also illustrates how to give your gadget a non-rectangular shape and how to add some nifty visual effects to text displayed in a your gadget.…
If you have ever found yourself doing a repetitive action with the mouse and keyboard, and if you have even a little programming experience, there is a good chance that you can use a text editor to whip together a sort of macro to automate the proce…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA.…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

738 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question