C++ GetProcAddress的用法不执行
我可能错过了一些很容易的东西,但我仍然面对着这个我无法解决的问题。我用这些函数创建了一个dllC++ GetProcAddress的用法不执行,c++,winapi,dll,dllexport,C++,Winapi,Dll,Dllexport,我可能错过了一些很容易的东西,但我仍然面对着这个我无法解决的问题。我用这些函数创建了一个dll extern "C"{ __declspec(dllexport) void some(); __declspec(dllexport) void printer(); } void printer() { printf("printing...\n"); } 我使用cmake编译它 cmake .. -G "Visual Stud
extern "C"{
__declspec(dllexport) void some();
__declspec(dllexport) void printer();
}
void printer()
{
printf("printing...\n");
}
我使用cmake编译它
cmake .. -G "Visual Studio 15 2017" -A x64 -DBUILD_SHARED_LIBS=TRUE
cmake --build . --config Release
我从dllloader.cpp加载它
int main() {
HINSTANCE hGetProcIDDLL = LoadLibrary("mydll.dll");
if (hGetProcIDDLL == NULL) {
std::cout << "cannot locate the .dll file" << std::endl;
} else {
std::cout << "it has been called" << std::endl;
}
if(GetProcAddress(hGetProcIDDLL, "printer") == NULL){
}else{
std::cout <<"not null" << std::endl;
}
std::cout << GetLastError() << " err" << std::endl;
getchar();
return 0;
}
intmain(){
HINSTANCE hGetProcIDDLL=LoadLibrary(“mydll.dll”);
如果(hGetProcIDDLL==NULL){
std::couttypedef void(*my_dll_print)(;//指向dll的打印方法的函数指针
int main()
{
HINSTANCE hGetProcIDDLL=LoadLibrary(“mydll.dll”);
如果(hGetProcIDDLL==nullptr)
std::您是否必须保存返回值并使用它来调用函数。我可以将void
函数的值存储在哪种变量类型中?在该链接中,该值似乎是内置于我的@RetiredInjaals中的@RetiredInjaals是否有一种更简单的方法可以从地址加载它?@RetiredInjaw在您的代码中,您认为您正在尝试o调用printer()
?如果我只是阅读您的代码并推断其含义,它会说“如果过程'printer'在hGetProcIDDLL
中没有地址,则不执行任何操作,否则将“not null”
发送到标准输出。”(或者我应该按字面理解“nothing is printed”并假定“not null”
或“它被称为”
已打印”?)我刚刚了解到,我需要在JaMiT处调用它,以了解如何调用。在我记得getprocaddress直接运行函数之前,我使用了这种方法,我可能记错了,或者它可能是一个具有不同名称的函数。1
函数指针类型my_dll_print
缺少调用约定。2
GetProcAddress
要求您传递修饰符号,除非导出已重命名(例如使用.def文件)。3
调用太晚。在调用时,其返回值已变得毫无意义。LoadLibrary返回HMODULE
typedef void(*my_dll_print)(); // function pointer to print method of dll
int main()
{
HINSTANCE hGetProcIDDLL = LoadLibrary("mydll.dll");
if (hGetProcIDDLL == nullptr)
std::cout << "cannot locate the .dll file" << std::endl;
else
std::cout << "it has been called" << std::endl;
my_dll_print print_method = reinterpret_cast<my_dll_print>(GetProcAddress(hGetProcIDDLL, "printer")); // Extract method address and create pointer
if (print_method == nullptr)
std::cout << "is null" << std::endl;
else
print_method(); // Call dll method
std::cout << GetLastError() << " err" << std::endl;
getchar();
return 0;
}