我知道您知道 DLL 函数签名,但您没有标头。
对于具有已知签名的给定函数dll_function:
long dll_function(long, long, char*, char*);
您可以使用 Windows API 中的 LoadLibrary 和 GetProcAddress,如以下 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;
}