我在 dll 中有非导出的成员函数。
我正在尝试使用 调用此函数基数 + 偏移量 我之前提取的地址。
我在从类实例进行实际调用时遇到问题。
我能够从 dll 中获取模块的基地址和函数的偏移量。
我调用该函数时出错,因为它不是我类(class)的成员。
HMODULE hDll = GetModuleHandleA("myModule.dll");
void *(funcPtr)() = (void(*)())((char *)&hDll + 0x101E07); //this is the non exported function address
myClassInstance->funcPtr();
我收到一个错误:
'funcPtr' is not a member of 'MyClass'
如何调用
funcPtr
来自 myClassInstance
?
最佳答案
这不是一件简单的事情。首先,您需要将指针声明为成员函数指针:
void (MYCLASS::*funcPtr)();
你可以通过
(myClassInstance->*funcPtr)();
然后你需要一些方法来在那里存储一个值。您不能转换
char *
(或 void *
)指向成员函数指针的指针。你必须使用某种形式的双关语。典型的方式是使用
memcpy
:void *addr = (char *)&hDll + 0x101E07;
memcpy(&funcPtr, &addr, sizeof(void *));
另一种可能性,因为从 DLL 获取成员函数的地址已经超出了未定义行为的范围,可能是使用 union :
union MemFuncPtr {
void *addr;
void (MYCLASS::*memfun)();
// Can be expanded to include other member function pointer types if necessary
};
然后你可以
MemFuncPtr mfp;
mfp.addr = (char *)&hDll + 0x101E07;
funcPtr = mfp.memfun;
或者您可以尝试在作业左侧使用非常难看的引用。
如果您获得地址的函数是
virtual
,则可能会出现其他复杂情况。和 myClassInstance
指向派生类,或者是否涉及多重继承。继续需要您自担风险。
关于c++ - 从 Dll 调用具有偏移地址的成员函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56416728/