将 Windows Dll 转换为 C++ Visual Studio 2008 的 .lib
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1536557/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Converting a Windows Dll to .lib for C++ Visual Studio 2008
提问by Mike Trader
I know there is a tool called Dll to lib but the developer is asking $1000. I only need to convert one library, once, so its not easy to justify that price.
我知道有一个工具叫 Dll to lib,但开发人员要价 1000 美元。我只需要转换一个库,一次,所以很难证明这个价格是合理的。
I tried IMPLIB32.EXE, but I just get empty .lib files. How can I accomplish this? Perhaps I can write a simple conversion app?
我试过 IMPLIB32.EXE,但我只得到空的 .lib 文件。我怎样才能做到这一点?也许我可以编写一个简单的转换应用程序?
Added1:
添加1:
The Dll's are typically stdcall not cdecl and written in older C like languages NOT C# or .NET or C++. I now need to call them from C++ apps. An example would be the SQLite.dll or zlib.dll. I do not have access to the .lib files for these dll's.
Dll 通常是 stdcall 而不是 cdecl 并且是用较旧的 C 语言编写的,例如不是 C# 或 .NET 或 C++ 的语言。我现在需要从 C++ 应用程序中调用它们。一个例子是 SQLite.dll 或 zlib.dll。我无权访问这些 dll 的 .lib 文件。
Added2:
添加2:
I re-wrote this code for VS2008 http://floodyberry.wordpress.com/2008/09/08/generating-dll-wrappers/and included the example Dll etc downloadable here: http://www.transferbigfiles.com/Get.aspx?id=7d86fa0b-6ddc-4f6f-8d31-2c20824aae9aThis in turn makes a project that creates a Dll. When I try to compile the Dll I get the linker error: AddShow.dll : fatal error LNK1107: invalid or corrupt file: cannot read at 0x300 Described here: http://list.isis.vanderbilt.edu/pipermail/udm-users/2006-March/000664.htmlNot sure how to proceed. So close yet so far
我为 VS2008 重新编写了这段代码 http://floodyberry.wordpress.com/2008/09/08/generating-dll-wrappers/并包含了可在此处下载的示例 Dll 等:http: //www.transferbigfiles.com/Get .aspx?id=7d86fa0b-6ddc-4f6f-8d31-2c20824aae9a这反过来又创建了一个创建 Dll 的项目。当我尝试编译 Dll 时,出现链接器错误:AddShow.dll:致命错误 LNK1107:无效或损坏的文件:无法在 0x300 处读取 此处描述:http://list.isis.vanderbilt.edu/pipermail/udm-users /2006-March/000664.html不知道如何继续。那么近又那么远
Next we move to this method
接下来我们转到这个方法
http://www.coderetard.com/2009/01/21/generate-a-lib-from-a-dll-with-visual-studio/
http://www.coderetard.com/2009/01/21/generate-a-lib-from-a-dll-with-visual-studio/
Running dumpbin with the argument /exports C:\path\to\AddShow.dll does absolutly nothing After some research
使用参数 /exports C:\path\to\AddShow.dll 运行 dumpbin 完全没有经过一些研究
http://msdn.microsoft.com/en-us/library/aa446532.aspxit seems that mspdb71.dll (now mspdb80.dll) is needed from the common/ide folder dumpbin.exe now runs with error:
http://msdn.microsoft.com/en-us/library/aa446532.aspx似乎需要来自 common/ide 文件夹 dumpbin.exe 的 mspdb71.dll(现在是 mspdb80.dll)现在运行时出现错误:
fatal error LNK1106: invalid file or disk full: cannot seek to 0x6179A These threads suggests the version of dumpbin.exe might be the issue
致命错误 LNK1106:无效文件或磁盘已满:无法寻找 0x6179A 这些线程表明 dumpbin.exe 的版本可能是问题所在
I have Microsoft (R) COFF/PE Dumper Version 9.00.30729.01
我有 Microsoft (R) COFF/PE Dumper 版本 9.00.30729.01
So I tried Microsoft (R) COFF Binary File Dumper Version 5.12.8078 with no success. After much reading I am no closer
所以我尝试了 Microsoft (R) COFF Binary File Dumper Version 5.12.8078 没有成功。经过大量阅读后,我不再靠近
http://support.microsoft.com/kb/815645http://support.microsoft.com/kb/839286http://markmail.org/message/p5vwzyfyv3bs6z34http://fixunix.com/programmer/94825-fatal-error-lnk1106-invalid-file-disk-full.html
http://support.microsoft.com/kb/815645 http://support.microsoft.com/kb/839286 http://markmail.org/message/p5vwzyfyv3bs6z34 http://fixunix.com/programmer/94825-fatal -error-lnk1106-invalid-file-disk-full.html
When I run ProcMon I see the first occurance of queryopen and sqlite3.dll when svchost.exe tries to open it and fails with the error PATH NOT FOUND. the path is C:\Program Files\Microsoft Visual Studio 9.0\VC\bin\SQLITE3.DLL and is correct. If I put it at the root of the C drive then I get NAME NOT FOUND errors:
当我运行 ProcMon 时,当 svchost.exe 尝试打开它时,我看到第一次出现 queryopen 和 sqlite3.dll,但失败并显示错误 PATH NOT FOUND。路径是 C:\Program Files\Microsoft Visual Studio 9.0\VC\bin\SQLITE3.DLL 并且是正确的。如果我将它放在 C 驱动器的根目录,那么我会收到 NAME NOT FOUND 错误:
C:\Program Files\Microsoft Visual Studio 9.0\VC\bin\link.exe.Local
C:\Program Files\Microsoft Visual Studio 9.0\VC\bin\link.exe.Local
C:\Program Files\Microsoft Visual Studio 9.0\VC\bin\dumpbin.exe.Local
C:\Program Files\Microsoft Visual Studio 9.0\VC\bin\dumpbin.exe.Local
from link.exe and dumpbin.exe respectivly. Im using XPSP3 not Vista and this is about the limit of my knowledge of sysinternals. what are these .local files?
分别来自 link.exe 和 dumpbin.exe。我使用的是 XPSP3 而不是 Vista,这是我对 sysinternals 知识的限制。这些 .local 文件是什么?
(csrss.exe is also not able to find a few manifest files.)
(csrss.exe 也无法找到一些清单文件。)
So no success yet, just more mystery
所以还没有成功,只是更加神秘
Added 3:
添加 3:
I tried to run dumpbin.exe from its installed location, \Program Files\Microsoft Visual Studio 8\VC\bin, but the OS said it couldn't find mspdb80.dll. I copied mspdb80.dll from \Program Files\Microsoft Visual Studio 8\Common7\IDE to try to get dumpbin.exe to run.
我尝试从其安装位置 \Program Files\Microsoft Visual Studio 8\VC\bin 运行 dumpbin.exe,但操作系统表示找不到 mspdb80.dll。我从 \Program Files\Microsoft Visual Studio 8\Common7\IDE 复制了 mspdb80.dll 以尝试运行 dumpbin.exe。
now I get the error: "c1902 program database manager mismatch please check your installation"
现在我收到错误消息:“c1902 程序数据库管理器不匹配,请检查您的安装”
If I remove mspdb80.dll from \Program Files\Microsoft Visual Studio 8\VC\bin the error goes away! but I can't run dumpbin.exe.
如果我从 \Program Files\Microsoft Visual Studio 8\VC\bin 中删除 mspdb80.dll,错误就会消失!但我无法运行 dumpbin.exe。
Added 4:
添加4:
I was finally able to get dumpbin to run by copying the following files to a folder:
我终于能够通过将以下文件复制到一个文件夹来运行 dumpbin:
dumpbin.exe link.exe lib.exe mspdb80.dll
dumpbin.exe link.exe lib.exe mspdb80.dll
I did get the error:
我确实得到了错误:
fatal error LNK1248: image size (FFFFFXXX) exceeds maximum allowable size (80000000)
致命错误 LNK1248:图像大小 (FFFFFXXX) 超过最大允许大小 (80000000)
once, but replacing the dll fixed that. Presumably it got corrupted?
一次,但更换 dll 解决了这个问题。估计是坏了?
I then moved onto the next step in the instructions: http://www.coderetard.com/2009/01/21/generate-a-lib-from-a-dll-with-visual-studio/http://support.microsoft.com/kb/131313and got the error: warning lnk4017 statement not supported for the target platform ignored This turns out to be because I specified the .dll instead of the .def file.
然后我按照说明进行下一步: http: //www.coderetard.com/2009/01/21/generate-a-lib-from-a-dll-with-visual-studio/ http://support .microsoft.com/kb/131313并得到错误:目标平台不支持警告 lnk4017 语句被忽略 这原来是因为我指定了 .dll 而不是 .def 文件。
The resulting .Lib and .exp files are then added to the VS2008 project and compile and run. The debugger then reported an error: Run-Time Check Failure #0 - The value of ESP was not properly saved across a function call...
然后将生成的 .Lib 和 .exp 文件添加到 VS2008 项目并编译和运行。调试器随后报告错误:运行时检查失败 #0 - ESP 的值未在函数调用中正确保存...
As mentioned here "Run-Time Check Failure #0 - The value of ESP was not properly saved across a function call" after successful C# callback from the C++ code of GameSpy libthis is because I used stdcall in my dll and declared __cdecl in my app.
正如此处提到的 “运行时检查失败 #0 - 在从 GameSpy lib 的 C++ 代码成功进行 C# 回调之后,ESP 的值没有在函数调用中正确保存”这是因为我在我的 dll 中使用了 stdcall 并在我的声明中声明了 __cdecl应用程序。
extern "C" { // put Dll C function prototypes here
int __cdecl AddTwoNum(int n, double f); // __stdcall
}
So changing this to __stdcall should fix it you would think.. but alas no.
因此,将其更改为 __stdcall 应该可以解决您认为的问题……但可惜没有。
now I get a linking error: error LNK2001: unresolved external symbol _AddTwoNum@12
现在我收到一个链接错误:错误 LNK2001:未解析的外部符号 _AddTwoNum@12
This is a decorated function name for some reason. Why?
由于某种原因,这是一个修饰的函数名称。为什么?
Added n:
添加 n:
Well this turned out to be because the .lib file was made using a dll that with STDCALL functions. STDCALL requiers the caller to clean up the stack so the number of bytes of the arguments is appended to the function name with an @ sign. IN this case I had three 4Byte iintegers for a total of 12 bytes.
好吧,事实证明这是因为 .lib 文件是使用带有 STDCALL 函数的 dll 制作的。STDCALL 要求调用者清理堆栈,以便将参数的字节数附加到带有 @ 符号的函数名称。在这种情况下,我有三个 4Byte 整数,总共 12 个字节。
Once I remade the .lib file from a dll created with the CDECL calling convention then all was good.
一旦我从使用 CDECL 调用约定创建的 dll 重新制作了 .lib 文件,那么一切都很好。
回答by floodyberry
I can't seem to comment on his answer (I need more reputation?), but you can try using my .dll wrapper generatorto create the stub .dll iain suggested.
我似乎无法评论他的回答(我需要更多声誉?),但您可以尝试使用我的.dll 包装器生成器来创建建议的存根 .dll iain。
回答by iain
I am assuming you want to call some function/methods in a dll in C/C++ without having access to the lib file to link with it.
我假设您想在 C/C++ 中调用 dll 中的某些函数/方法,而无需访问 lib 文件以与之链接。
If you know the signatures of the functions you require, you can:
如果您知道所需函数的签名,您可以:
- create your own stub version of the dll
- create a dll project, with the same name as the dll you want to use
- add stub implementations of the functions you want to call
- the signatures are very important use depends.exe or dumpbin.exe to check they match.
- then write/build your program to link to the stub dll
- to run the program replace the stub dll with the real one
- 创建您自己的 dll 存根版本
- 创建一个与你要使用的dll同名的dll项目
- 添加要调用的函数的存根实现
- 签名非常重要,请使用depends.exe 或dumpbin.exe 来检查它们是否匹配。
- 然后编写/构建您的程序以链接到存根 dll
- 要运行该程序,请用真实的存根 dll 替换存根 dll
回答by Rapha?l Saint-Pierre
You could also go the dynamic way, using LoadLibrary()and GetProcAddress().
您也可以采用动态方式,使用LoadLibrary()和GetProcAddress()。
(See second link for an example).
(有关示例,请参见第二个链接)。
回答by Rapha?l Saint-Pierre
I found this example rather quickly: http://www.coderetard.com/2009/01/21/generate-a-lib-from-a-dll-with-visual-studio/
我很快就找到了这个例子:http: //www.coderetard.com/2009/01/21/generate-a-lib-from-a-dll-with-visual-studio/
回答by Joshua
In cases like zlib or sqlite you can download the source directly and compile your own lib file. In fact, I just did this the other day for zlib, I downloaded the source from www.zlib.net(direct zlib 1.2.3 download) opened the provided visual studio project file and compiled both a debug and release version of the lib. I haven't tried sqlite but seeing that they have the sources, it shouldn't be difficult to do.
在像 zlib 或 sqlite 这样的情况下,您可以直接下载源代码并编译您自己的 lib 文件。事实上,我前几天刚刚为 zlib 做了这个,我从www.zlib.net下载了源代码(直接 zlib 1.2.3 下载)打开提供的 Visual Studio 项目文件并编译了 lib 的调试和发布版本。我没有尝试过 sqlite 但看到他们有来源,应该不难做到。
If it's a proprietary dll, you could try asking the original developer or company for a lib file, or write your own stub dll that iain suggested or go the dynamic route that RaphaelSP.
如果它是专有的 dll,您可以尝试向原始开发人员或公司索要 lib 文件,或者编写您自己的 iain 建议的存根 dll,或者走 RaphaelSP 的动态路线。
If they are exported in C (no C++ name mangling) I would go the dynamic route myself.
如果它们在 C 中导出(没有 C++ 名称修改),我会自己走动态路线。
回答by hackworks
I have had success in getting a lib from DLL using details in http://wyw.dcweb.cn/dllfaq.htm. Your mileage might vary. I have used this to get a lib from python DLL to build some python extensions that are written in 'C'.
我已经成功地使用http://wyw.dcweb.cn/dllfaq.htm 中的详细信息从 DLL 中获取了一个库。您的里程可能会有所不同。我已经使用它从 python DLL 中获取一个库来构建一些用“C”编写的 python 扩展。
回答by OCTAGRAM
Open source edllhas its own internal COFF loader. edll is written for quite different purposes, but since it has independent COFF loader within, maybe it's possible to incorporate your .dll into binary stream or resource inside your .exe, then in run-time use edll's COFF loader to load built-in dll.
开源edll有自己的内部 COFF 加载器。edll 是为完全不同的目的而编写的,但由于它有独立的 COFF 加载器,也许可以将 .dll 合并到二进制流或 .exe 中的资源中,然后在运行时使用 edll 的 COFF 加载器加载内置 dll .
回答by Dana Holt
If you contact the developer of the Dll to lib tool they might be willing to give you a discount.
如果您联系 Dll to lib 工具的开发人员,他们可能愿意给您打折。
I've been in these situations before (where I needed an expensive tool just one time), and sometimes I have had success in getting a large discount.
我以前遇到过这些情况(我只需要一次昂贵的工具),有时我还成功地获得了很大的折扣。
It never hurts to try.
尝试从来没有坏处。
Good luck.
祝你好运。
回答by unwind
Thisis supposedly a free tool that makes the conversion. I'm not sure if it's "free" as in "limited trial version" or not, the information is not very clear. If you've already tested this, edit your question and I'll delete this answer.
这应该是一个进行转换的免费工具。我不确定它是否像“有限试用版”那样“免费”,信息不是很清楚。如果您已经对此进行了测试,请编辑您的问题,我将删除此答案。