eclipse 如何在 C++ 中使用没有头文件的 DLL
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6997372/
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
How to use DLL without header in C++
提问by Country
Help, please. A have a dll-file. I know it's functions and parameters. How can I use it in Eclipse with MinGW?
请帮忙。A 有一个 dll 文件。我知道它的功能和参数。如何在带有 MinGW 的 Eclipse 中使用它?
回答by Armen Tsirunyan
I am assuming you are using windows. In WINAPI you have LoadLibraryand GetProcAddressfunctions. Here's an exampleof usage
我假设您正在使用 Windows。在 WINAPI 中,您有LoadLibrary和GetProcAddress函数。这是一个使用示例
回答by Saullo G. P. Castro
I understood you know the DLL function signature and you don't have the header.
我知道您知道 DLL 函数签名并且您没有标题。
For a given function dll_function
with the known signature:
对于dll_function
具有已知签名的给定函数:
long dll_function(long, long, char*, char*);
You can use LoadLibrary
and GetProcAddress
from Windows API like examplified in the following C++ code:
您可以使用LoadLibrary
和GetProcAddress
来自 Windows API,例如以下 C++ 代码中的示例:
#include <windows.h>
#include <iostream>
typedef long(__stdcall *f_funci)(long, long, char*, char*);
struct dll_func_args {
long arg1;
long arg2;
std::string arg3;
std::string arg4;
};
// Borrowing from https://stackoverflow.com/a/27296/832621
std::wstring s2ws(const std::string& s)
{
int len;
int slength = (int)s.length() + 1;
len = MultiByteToWideChar(CP_ACP, 0, s.c_str(), slength, 0, 0);
wchar_t* buf = new wchar_t[len];
MultiByteToWideChar(CP_ACP, 0, s.c_str(), slength, buf, len);
std::wstring r(buf);
delete[] buf;
return r;
}
int main()
{
std::string filename = "C:\...\mydllfile.dll";
dll_func_args args;
args.arg1 = 1;
args.arg2 = 2;
args.arg3 = "arg3";
args.arg4 = "arg4";
std::wstring tmp = s2ws(filename);
HINSTANCE hGetProcIDDLL = LoadLibrary(tmp.c_str());
if (!hGetProcIDDLL)
{
std::cerr << "Failed to load DLL" << std::endl;
return EXIT_FAILURE;
}
// resolve function address here
dll_func_ptr func = (dll_func_ptr)GetProcAddress(hGetProcIDDLL, "dll_function");
if (!func)
{
std::cout << "Failed to load function inside DLL" << std::endl;
return EXIT_FAILURE;
}
std::cout << "Return value " << func(args.arg1, args.arg2, (char *)args.arg3.c_str(), (char *)args.arg4.c_str()) << std::endl;
return EXIT_SUCCESS;
}
回答by Ferruccio
I created a wrapperto simplify this sort of thing a while back.
Update:I completely forgot about this post and deleted the blog post and associated source code. I wound up with a dangling pointer here ;-)
更新:我完全忘记了这篇文章并删除了博客文章和相关的源代码。我在这里得到了一个悬空指针;-)
Luckily, someone did a much better job than I did: Boost.DLL
幸运的是,有人做得比我好得多:Boost.DLL
回答by Ajay
If you DO have appropriate .LIB file, and you have exact function prototype, you don't need header. Just declare the functions youself (possibly in your own custom header). Call those functions directly. Link with .LIB file. The DLL would get loaded by OS, and functions would be called.
如果您确实有合适的 .LIB 文件,并且您有确切的函数原型,则不需要标题。只需自己声明函数(可能在您自己的自定义标头中)。直接调用这些函数。与 .LIB 文件链接。DLL 将被操作系统加载,并且函数将被调用。
If you don't have .LIB file to link to DLL, you need to use LoadLibrary
and GetProcAddress
as others have suggested.
如果您没有 .LIB 文件链接到 DLL,则需要使用LoadLibrary
和GetProcAddress
其他人建议的那样。