windows VC++/Dev-C++:如何包含一个 DLL?

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

VC++ / Dev-C++: How to include an DLL?

windowsvisual-c++dlldev-c++

提问by Daniel Marschall

I have written an DLL in Delphi which exports functions. I would like to use these functions in a C++ program without using dynamic Linking (LoadLibrary() API-Call).

我在 Delphi 中编写了一个导出函数的 DLL。我想在 C++ 程序中使用这些函数而不使用动态链接(LoadLibrary() API-Call)。

The "import" declaration would be

“进口”声明将是

extern "C" int __stdcall getVersionNumber();

I mainly use Bloodshed Dev-C++ which creates Windows Executables. But I do not know how to tell the compiler that it should import the function "getVersionNumber" from "STATMONDLL32.dll".

我主要使用 Bloodshed Dev-C++ 来创建 Windows 可执行文件。但我不知道如何告诉编译器它应该从“STATMONDLL32.dll”导入函数“getVersionNumber”。

After I spent many hours by googling the problem, without any result (there was only weird stuff written about .a files and .lib files, which I do not have compiled by Delphi...) I have also installed VC++, but even there, I could not find a way to tell the compiler to use a specific DLL.

在我用谷歌搜索问题花了很多时间后,没有任何结果(只有关于 .a 文件和 .lib 文件的奇怪的东西,我没有用 Delphi 编译......)我也安装了 VC++,但即使有,我找不到告诉编译器使用特定 DLL 的方法。

I have created a DEF file for this DLL - how can I tell Dev-C++ and/or VC++ to use it? (Dev-C++ prefered)

我已经为这个 DLL 创建了一个 DEF 文件 - 我如何告诉 Dev-C++ 和/或 VC++ 使用它?(Dev-C++ 首选)

// Edit: Delphi is creating UNDECORATED symbols. The symbol is exactly "getVersionNumber".

// 编辑:Delphi 正在创建 UNDECORATED 符号。该符号正是“getVersionNumber”。

I have created following DEF file with an alias for decoration:

我创建了以下带有装饰别名的 DEF 文件:

LIBRARY   STATMONDLL32
EXPORTS
  getVersionNumberA = _getVersionNumberA@0

I have created a *.lib file with VC++ "lib.exe":

我用 VC++“lib.exe”创建了一个 *.lib 文件:

lib.exe /DEF:StatMonDll32.def /OUT:StatMonDll32.lib

I have included the lib in VC++ linker settings.

我已将 lib 包含在 VC++ 链接器设置中。

But VC++ tells me that it cannot resolve the external symbol _getVersionNumberA@0 ! Please help!

但是 VC++ 告诉我它无法解析外部符号 _getVersionNumberA@0 !请帮忙!

// Edit: I have uploaded the DLL here: http://www.viathinksoft.de/temp/StatMonDll32.dll. Can you access the symbol getVersionNumberA with VC++ ? I am searching for a solution since 6 days now :'-(

// 编辑:我在这里上传了 DLL:http: //www.viathinksoft.de/temp/StatMonDll32.dll。你能用 VC++ 访问符号 getVersionNumberA 吗?自 6 天以来,我一直在寻找解决方案 :'-(

Best regards

此致

Daniel Marschall

丹尼尔·马歇尔

回答by Ben Voigt

You can use dynamic linking, it should work something along the lines of:

您可以使用动态链接,它应该按照以下方式工作:

extern "C" typedef int (__stdcall *pfnadd)(int a, int b);
extern "C" typedef int (__stdcall *pfngetversion)(void);
HMODULE mod = LoadLibraryA("mydll.dll");
pfnadd add = (pfnadd)GetProcAddress(mod, "Add");
pfngetversion getVersionNumberA =
       (pfngetversion)GetProcAddress(mod, "getVersionNumberA");

and then you can just call using the function pointer:

然后你可以使用函数指针调用:

add(1, 2);
std::cout << getVersionNumberA();

Although, it seems like your question has bits and pieces of two different functions!

虽然,您的问题似乎有两个不同功能的点点滴滴!