本文共 6366 字,大约阅读时间需要 21 分钟。
这篇讲解了DEF文件地结构,但附件中的工程编译不了。
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: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:If the header file that describes MYDLL.DLL looks like:
// mydll.Hextern "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:
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):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:
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 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#include // DLL function signaturetypedef 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;}
转载地址:http://lthai.baihongyu.com/