Article ID: 131313 - View products that this article applies to.
This article was previously published under Q131313
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:
Creating a .DEF fileThe 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 that all names are automatically exported without a leading underscore. This is only true for _cdecl function names.
Given a .DLL with functions exported via a C interface, you can create an import library by following these steps:
Stubbing Out FunctionsFor 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:
ExampleIf the header file that describes MYDLL.DLL looks like:
The dummy source file you use to build the import library should look like:
Once the functions are stubbed out, all you need to do is compile the source file into an .OBJ file:
CL /c /Ob0 mydll.CPPNOTE: 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.OBJFor more information on the LIB command, consult the "LIB Reference" in the Visual C++ Books Online.
Also, see the following article in the Microsoft Knowledge Base:
(http://support.microsoft.com/kb/140485/EN-US/ )Exporting PASCAL-Like Symbols in 32-bit DLLs
Article ID: 131313 - Last Review: June 29, 2004 - Revision: 2.1
Contact us for more help
Connect with Answer Desk for expert help.