How to create .lib file when you only have .dll and .h files
来源:互联网 发布:java job quartz 编辑:程序博客网 时间:2024/05/16 11:29
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.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:
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 theLoadLibrary (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 todlopen,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 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;}
- How to create .lib file when you only have .dll and .h files
- How to create .lib file when you have .dll and .h files onle
- How to use Deep Learning when you have Limited Data
- How to Create and Use the DLL
- Configuration of DLL usage in VS2005--how to add additionl lib and head file
- how to solve the problem when you have chmod the directory to 777 but the apache still can not write file
- How to create EPS files...
- you have to know about how to create user defined control.
- SD-How to get delivery plant when you use va01 to create a new order
- how to create image file and format it
- How to make dll and lib using bakefile.
- Success and Motivation - You only have to be right once!(ZT)
- How to create PFX file
- When and How to Micromanage
- How To Create an IE-Only Stylesheet
- How To Create Patch Files for the MSDE 2000 Sample.msi File
- In C how do you redirect stdin/stdout/stderr to files when making an execvp() or similar call?
- dll's .h to dll's .lib
- Linux音频编程指南四
- 循环将一张表的数据添加到另一张表,重复数据不添加
- .NET程序员,我们应该更专业点
- sift特征向量
- 服务器端 要用tomacat,所以 安装eclipse,安装步骤如是
- How to create .lib file when you only have .dll and .h files
- Jackson 框架,轻易转换JSON
- 虚拟内存管理总结
- 2011-11-22
- C++ 之“友元类”学习笔记
- xmlHttpRequest
- shell IO
- android的文件系统结构及其引导过程的初步理解
- Android开发平台中,可通过TelephonyManager 获取本机号码