C++ __declspec(dllimport) 如何加载库

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17935113/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 21:35:18  来源:igfitidea点击:

__declspec(dllimport) how to load library

c++dlldllexport

提问by deepspace

http://msdn.microsoft.com/en-us/library/9h658af8.aspx

http://msdn.microsoft.com/en-us/library/9h658af8.aspx

MSDN says I can export function from the library with __declspec(dllexport)but how can I load this library in my executable?

MSDN 说我可以从库中导出函数,__declspec(dllexport)但是如何在我的可执行文件中加载这个库?

I've got an exported function in DLL:

我在 DLL 中有一个导出函数:

 __declspec(dllexport) void myfunc(){}

And now I would like to use it in my executable:

现在我想在我的可执行文件中使用它:

 __declspec(dllimport) void myfunc(void);

But how my program will know where to find this function?

但是我的程序如何知道在哪里可以找到这个函数呢?

采纳答案by CharlesB

This is the compiler/linker job, it's done automatically as long as you

这是编译器/链接器的工作,只要你

  1. include the .lib in the Linker options
  2. provide the DLL at runtime so that it's found by the exe
  1. 在链接器选项中包含 .lib
  2. 在运行时提供 DLL,以便 exe 找到它

The .lib file is generated when you compile the DLL, or is shipped with it if it's not your code. In this case the code is compiled with __declspec(dllexport).

.lib 文件在您编译 DLL 时生成,或者如果它不是您的代码,则随它一起提供。在这种情况下,代码是用__declspec(dllexport).

When compiling your exe, the compiler sees that the included function is to be found in DLL. In this case the code is compiled with __declspec(dllimpport).

编译您的 exe 时,编译器会看到包含的函数将在 DLL 中找到。在这种情况下,代码是用__declspec(dllimpport).

The linker is provided with the .lib file, and generates appropriate instructions in the exe.

链接器随 .lib 文件一起提供,并在 exe 中生成适当的指令。

These instructions will make the Exe find the DLL and load the exported function at runtime. The DLL just has to be next to the Exe (there are other possible places, however).

这些指令将使 Exe 找到 DLL 并在运行时加载导出的函数。DLL 只需要在 Exe 旁边(但是,还有其他可能的位置)。

Switching between __declspec(dllimpport)and __declspec(dllexport)is done by a macro, provided by Visual C++ when creating a DLL project.

在创建 DLL 项目时由 Visual C++ 提供的宏完成__declspec(dllimpport)和之间的切换。__declspec(dllexport)

回答by Kirk Backus

If you are using a DLL, you can to use the LoadLibraryand GetProcAddresscombination.

如果您使用的是 DLL,则可以使用LoadLibraryGetProcAddress组合。

//Load the DLL
HMODULE lib = LoadLibrary("testing.dll");

//Create the function
typedef void (*FNPTR)();
FNPTR myfunc = (FNPTR)GetProcAddress(lib, "myfunc");

//EDIT: For additional safety, check to see if it loaded
if (!myfunc) {
    //ERROR.  Handle it.
}

//Call it!
myfunc();

回答by Forest Kunecke

Your operating system will be able to find it based on the linking process. If your library is linked properly to your program, it will recognize that there is an external function being used, and look for it in the dll paths. If it can't find it, your linker will throw an error.

您的操作系统将能够根据链接过程找到它。如果您的库正确链接到您的程序,它将识别出正在使用的外部函数,并在 dll 路径中查找它。如果找不到,您的链接器将抛出错误。

I recommend doing some reading into the linking process; it can be confusing at times but understanding it may help you grasp some key concepts in C/C++.

我建议阅读链接过程;它有时可能会令人困惑,但理解它可能会帮助您掌握 C/C++ 中的一些关键概念。