从 C++ 调用 dll 函数

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

Calling a dll function from C++

c++dll

提问by Casper_2211

I have a function inside a dll that I would like to call from my C++ application. The dll is also made in C++ and has a def file which shows the functions present in the dll. I am using visual studio 2010 and I configured it to use the dll file by adding the directory of the dll in the linker "Additional Library directories" and then adding the DLLname.lib in the "input" of the linker. Now all the namespace inside the dll are available however the Functions that I need are not available because they are not under any namespace. How do I go about accessing those functions ? Those functions were declared as such in the dll

我在 dll 中有一个函数,我想从我的 C++ 应用程序中调用它。dll 也是用 C++ 制作的,并且有一个 def 文件,它显示了 dll 中存在的函数。我正在使用 Visual Studio 2010,我通过在链接器“附加库目录”中添加 dll 目录,然后在链接器的“输入”中添加 DLLname.lib 将其配置为使用 dll 文件。现在 dll 中的所有命名空间都可用,但是我需要的函数不可用,因为它们不在任何命名空间下。我如何去访问这些功能?这些函数在 dll 中是这样声明的

#include "stdafx.h"
#include <stdio.h>
__declspec(dllexport) int somefunction()
{
......
            return SomeValue
}

My question is how do I access somefunction in my C++ application through its dll.

我的问题是如何通过它的 dll 访问我的 C++ 应用程序中的某些函数。

回答by Matt Kline

There seems to be some confusion here. Adding a file to the linker input is for statically-linked libraries (.lib on Windows). With statically-linked libraries, the code is just copied into your program at compile time. Dynamically-linked libraries (.dll in Windows) reside in different files (the DLLs) and are loaded by your program when you run it. To access a function in a dll, there's two main methods:

这里似乎有些混乱。将文件添加到链接器输入用于静态链接库(Windows 上的 .lib)。使用静态链接库,代码只是在编译时复制到您的程序中。动态链接库(Windows 中的 .dll)驻留在不同的文件(DLL)中,并在您的程序运行时加载。要访问 dll 中的函数,有两种主要方法:

  • Use dllimport, similarly to how you exported the functions with dllexport

  • Load the DLL using LoadLibrary, then get a pointer to your function with GetProcAddress. If you're using this method, another thing you should note is that you should likely use extern "C"on functions you're exporting to avoid name mangling. If you're having issues finding the function with GetProcAddress, you can use Dependency Walkerto examine the function names inside the DLL - they may be modified slightly depending on the calling conventionused.

  • 使用dllimport,类似于您使用 dllexport 导出函数的方式

  • 使用LoadLibrary加载 DLL ,然后使用GetProcAddress获取指向您的函数的指针。如果您正在使用此方法,您应该注意的另一件事是您可能应该extern "C"在导出的函数上使用以避免名称修改。如果您在使用 查找函数时遇到问题GetProcAddress,您可以使用Dependency Walker检查 DLL 中的函数名称 - 它们可能会根据所使用的调用约定稍作修改。

回答by stefan

I think the poster is asking for help on implicit linking. MSDN Linking Implicitlyand Wikipedia Dynamic-link library Using DLL imports

我认为海报是在寻求有关隐式链接的帮助。 MSDN 隐式链接维基百科动态链接库使用 DLL 导入