C++ 使用 Visual Studio 导入 lib 文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19384157/
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
importing a lib file using visual studio
提问by MelMed
I am trying to import a file as below:
我正在尝试导入如下文件:
#import "francais.dll"
It says to me that it cannot open source file "C:/xxxx/Proj/Release/francais.tlh"
它告诉我它无法打开源文件“C:/xxxx/Proj/Release/francais.tlh”
The Library file is existing in Proj.
库文件存在于 Proj 中。
How to solve that?
如何解决?
回答by Hans Passant
It is not a "library file", it is a COM server. The #import directive auto-generates a .tli and a .tlh file from the type library that's embedded in francais.dll. There's no obvious reason why it wouldn't be able to load that .tlh file, there's probably something wrong with that DLL. Hard to see from here. Do make sure that you are not ignoring earlier errors, start at the top of the Error List window.
它不是“库文件”,而是 COM 服务器。#import 指令从嵌入在 francais.dll 中的类型库自动生成 .tli 和 .tlh 文件。没有明显的原因为什么它无法加载该 .tlh 文件,该 DLL 可能有问题。从这里很难看到。请确保您没有忽略较早的错误,从错误列表窗口的顶部开始。
As a basic check, you can look at that type library yourself. Run OleView.exe from the Visual Studio Command Prompt and use File + View Typelib, select that DLL. You need to see the content of the type library, decompiled into IDL.
作为基本检查,您可以自己查看该类型库。从 Visual Studio 命令提示符运行 OleView.exe 并使用 File + View Typelib,选择该 DLL。您需要查看类型库的内容,反编译为 IDL。
You can also see it in VS itself, use File + Open + File and select the DLL. You'll see the resources that are embedded in the DLL, there needs to be a node labeled "TYPELIB" with one resource with ID 1 that's the actual type library. If anything goes wrong with these two verifications then the #import directive isn't likely to work either.
您也可以在 VS 本身中看到它,使用 File + Open + File 并选择 DLL。您将看到嵌入在 DLL 中的资源,需要有一个标记为“TYPELIB”的节点,其中一个 ID 为 1 的资源是实际类型库。如果这两个验证出现任何问题,那么 #import 指令也不太可能起作用。
And do note that it is odd that it tries to find the file in the Release directory. You'd normally always start with the Debug configuration.
请注意,它试图在 Release 目录中查找文件是很奇怪的。您通常总是从调试配置开始。
回答by zero298
You need to add the .lib to the project properties. Do you have a .lib to go along with the DLL? Another question dealing with a similar issue can be found here: How do I use a third-party DLL file in Visual Studio C++?
您需要将 .lib 添加到项目属性中。你有 .lib 与 DLL 一起使用吗?可以在此处找到处理类似问题的另一个问题:如何在 Visual Studio C++ 中使用第三方 DLL 文件?
Main thing to note is whether or not you have a .lib
to go along with your DLL. Another thing that I have done before but I wouldn't completely recommend is to copy the DLL into the same directory as your executable. Whenever loading DLLs, Windows automatically checks the path that the executable is in.
需要注意的主要事项是您是否需要.lib
与您的 DLL 一起使用。我之前做过但我不完全推荐的另一件事是将 DLL 复制到与可执行文件相同的目录中。每当加载 DLL 时,Windows 都会自动检查可执行文件所在的路径。
1)On the menu bar (top of window) click project
->properties
(or alt+f7).
1)在菜单栏(窗口顶部)点击project
-> properties
(或 alt+f7)。
2)Open configuration properties
->linker
. Then in the general
tab, add the directory to your "Additional Library Directories"
.
2)打开配置properties
-> linker
. 然后在general
选项卡中,将目录添加到您的"Additional Library Directories"
.
3)Then click on input
right below general
and add the name of the lib to the "Additional Dependencies"
3)然后点击input
右下角general
,将lib的名称添加到"Additional Dependencies"
回答by RonenKr
if you insist on coupling your Dll from code you can use (not all compiler support that, VS does) :
如果您坚持将您的 Dll 与您可以使用的代码耦合(并非所有编译器都支持,VS 支持):
#pragma comment(lib, "path_to_lib\libname.lib")
use #import
for COM libraries not for standard Dlls
使用#import
的COM库不是标准的Dll
回答by CarpeDiemKopi
I had similiar trouble with COM server MSO.DLL (MS Office 14 / 2010) in Visual Studio 2015.
我在 Visual Studio 2015 中遇到了 COM 服务器 MSO.DLL(MS Office 14 / 2010)的类似问题。
The (red) IntelliSense warning goes away after first building/compiling! My real problem was the exact path. I had to write it manually. Copy/paste vom Explorer didn't work.
第一次构建/编译后(红色)IntelliSense 警告消失!我真正的问题是确切的路径。我不得不手动编写它。复制/粘贴 vom Explorer 不起作用。
#import "C:\Program Files\Common Files\microsoft shared\OFFICE14\MSO.DLL" no_implementation rename("RGB", "ExclRGB") rename("DocumentProperties", "ExclDocumentProperties") rename("SearchPath", "ExclSearchPath")