How to create 32-bit import libraries without .objs or source
View Products That Article Applies To.
This Article Was Previously Published Under Q131313
SUMMARYThis 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.
MORE INFORMATIONNormally, 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 is the a. Ways to create an import library given a .dll:
Create a .DEF file for use with the LIB / DEF:. Command 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 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:
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." Create a .DEF file that contains an EXPORTS section with the names of the functions listed in the "name" column of the DUMPBIN output. 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. 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 of the .def file. Use / out: to control the Output Library Name.
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:
When "__declspec (dllimport)" is used in a prototype or declaration, change it to "__declspec (dllexport)." 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 ( "{}"). 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. 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 Function Arguments Are Usually Just Specified by Type in AH Eader file. for example. A Dummy Argument Identifier Must Be Specified When Adding The Dummy Function Body Geta (INT X). OtherWise The Error C2055 is generated.
Exampleif 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;
PUBLIC:
INT Geta (int);
Long getb ();
CMYCLASS ();
}
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;
PUBLIC:
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 Online 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 In The Microsoft Knowledge Base:
140485 Exporting Pascal-Like Symbols in 32-bit DLLS
The Information in this Article Applies TO:
The Microsoft Library Manager (LIB.exe), WHEN USED with:
Microsoft Visual C , 32-Bit Editions 2.0
Microsoft Visual C , 32-Bit Editions 2.1
Microsoft Visual C , 32-Bit Editions 4.0
Microsoft Visual C , 32-Bit Editions 5.0