您的位置:首页 > 其它

How to create .lib file when you only have .dll and .h files

2011-11-22 14:19 525 查看

1. 原文地址:http://www.codeproject.com/KB/cpp/libfromdll.aspx

这篇讲解了DEF文件地结构,但附件中的工程编译不了。

2. Microsoft Support: How To Create 32-bit Import Libraries Without .OBJs or Source

2.1 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 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 name of the .DEF file. Use /OUT: to control the output library name.

2.2 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:

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

Example

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;
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 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 in the Microsoft Knowledge Base:

140485
(http://support.microsoft.com/kb/140485/EN-US/ ) Exporting PASCAL-Like Symbols in 32-bit DLLs

3. Wiki: Dynamic-link library

Explicit run-time linking

DLL files may be explicitly loaded at run-time, a process referred to simply asrun-time dynamic linking by Microsoft, by using the
LoadLibrary (orLoadLibraryEx) API function. The GetProcAddress API function is used to look up exported symbols by name, andFreeLibrary — to unload the DLL. These functions are analogous to
dlopen,dlsym, and dlclose in the
POSIX standard API.

// LSPaper draw using OLE2 function if available on client

HINSTANCE hOle2Dll ;

hOle2Dll = LoadLibrary ( "OLE2.DLL" ) ;

if ( hOle2Dll != NULL )
{
FARPROC lpOleDraw ;

lpOleDraw = GetProcAddress ( hOle2Dll , "OleDraw" ) ;

if ( lpOleDraw != (FARPROC)NULL )
{
(*lpOleDraw) (pUnknown , dwAspect , hdcDraw , lprcBounds ) ;
}
FreeLibrary ( hOle2Dll ) ;
}


Another Example:


#include <windows.h>
#include <stdio.h>

// DLL function signature
typedef double (*importFunction)(double, double);

int main(int argc, char **argv)
{
importFunction addNumbers;
double result;

// Load DLL file
HINSTANCE hinstLib = LoadLibrary(TEXT("Example.dll"));
if (hinstLib == NULL) {
printf("ERROR: unable to load DLL\n");
return 1;
}

// Get function pointer
addNumbers = (importFunction)GetProcAddress(hinstLib, "AddNumbers");
if (addNumbers == NULL) {
printf("ERROR: unable to find DLL function\n");
FreeLibrary(hinstLib);
return 1;
}

// Call function.
result = addNumbers(1, 2);

// Unload DLL file
FreeLibrary(hinstLib);

// Display result
printf("The result was: %f\n", result);

return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐