首页 > 代码库 > msvc下从dll文件创建lib

msvc下从dll文件创建lib

1.进入msvc命令控制台。
2.生成.def文件
通过pexports或微软编译环境自带的dumpbin.exe导出DLL对应的def文件
方法一: pexports .dll > .def
方法二: dumpbin /exports .dll > .def
其中***代表你的dll的文件名以及要生成的def的文件名。
我推荐使用pexports,因为它导出的def是标准的def格式,可在http://sourceforge.net/projects/mingw/files/MinGW/Extension/pexports/下载;而微软的dumpbin导出的def需要我们稍微修改为标准的格式。下图是pexports和dumpbin生成的def文件,如果你用dumpbin,则需要将其生成的def文件修改为pexports生成的def的格式。

图一、pexports生成的def文件格式

图二、dumpbin生成的def文件格式(它携带了更多的信息,我们需要修改成图一的格式)

A DEF file can be written manually extremely easy if you don‘t have one: You open the DLL in DependencyWalker (http://www.dependencywalker.com), select “Save As” -> “Text with Import/Export lists” and have the names and ordinals of all exported functions in a txt file like this:

Export Ordinal Hint Function Entry Point


[C ] 2 (0x0002) 1 (0x0001) gsasl_base64_from 0x000024F0
[C ] 3 (0x0003) 2 (0x0002) gsasl_base64_to 0x000024A0
[C ] 4 (0x0004) 3 (0x0003) gsasl_callback 0x000018B0
[C ] 5 (0x0005) 4 (0x0004) gsasl_callback_hook_get 0x00001900
[C ] 6 (0x0006) 5 (0x0005) gsasl_callback_hook_set 0x000018F0
[C ] 7 (0x0007) 6 (0x0006) gsasl_callback_set 0x000018A0
[C ] 8 (0x0008) 7 (0x0007) gsasl_check_version 0x00001870
[C ] 9 (0x0009) 8 (0x0008) gsasl_client_mechlist 0x00001E20
[C ] 10 (0x000A) 9 (0x0009) gsasl_client_start 0x00001F40
The rest is quickly done. Create a DEF file which has this format:

EXPORTS
gsasl_base64_from @2
gsasl_base64_to @3
gsasl_callback @4
gsasl_callback_hook_get @5
gsasl_callback_hook_set @6
gsasl_callback_set @7
gsasl_check_version @8
gsasl_client_mechlist @9
gsasl_client_start @10
gsasl_client_suggest_mechanism @11
The number behind the @ is the ordinal.

P.D: DependencyWalker can even undecorate cryptic names of C++ exports like

Foo@@YGHHH@Z –> int Foo(int, int).
3.编辑.def为标准格式
4.生成.lib文件

lib /def:\you_path\DLL_Name.def  /machine:i386 /OUT:\you_path\DLL_Name.lib