简体   繁体   English

从Dll调用具有偏移地址的成员函数

[英]Call a member function with offset address from Dll

I have non-exported member function in a dll. 我在dll中有非导出的成员函数。
I'm trying to call this function with base + offset address I extracted before. 我试图用之前提取的基本+偏移地址调用此函数。
I have a problem to do the actual calling from the class instance. 我有一个问题是从类实例中进行实际调用。

II was able to get the base address of the module and the offset of the function from the dll. II能够从dll获取模块的基址和函数的偏移量。
I got an error calling the function because it not a member of my class. 调用该函数时出错,因为它不是我班级的成员。

HMODULE hDll = GetModuleHandleA("myModule.dll");

void *(funcPtr)() = (void(*)())((char *)&hDll + 0x101E07); //this is the non exported function address
myClassInstance->funcPtr();

I'm getting an error: 我收到一个错误:

'funcPtr' is not a member of 'MyClass'

How do I make the call of funcPtr from myClassInstance ? 如何让我的电话funcPtrmyClassInstance

This is not an simple thing to do. 这不是一件简单的事情。 First, you need to declare your pointer as a member function pointer: 首先,您需要将指针声明为成员函数指针:

void (MYCLASS::*funcPtr)();

And you'd call it via 你会通过它来称呼它

(myClassInstance->*funcPtr)();

Then you need some way to store a value in there. 然后你需要一些方法来存储一个值。 You can't cast a char * (or void * ) pointer to a member function pointer. 您不能将char * (或void * )指针强制转换为成员函数指针。 You'll have to use some form of type punning. 你必须使用某种形式的打字。

The typical way is to use memcpy : 典型的方法是使用memcpy

void *addr = (char *)&hDll + 0x101E07;
memcpy(&funcPtr, &addr, sizeof(void *));

Another possibility, since getting the address of the member function from the DLL is already over the line for Undefined Behavior, might be using a union: 另一种可能性,因为从DLL获取成员函数的地址已经超出了Undefined Behavior的范围,可能正在使用union:

union MemFuncPtr {
    void *addr;
    void (MYCLASS::*memfun)();
    // Can be expanded to include other member function pointer types if necessary
};

Then you can just 那你就可以了

MemFuncPtr mfp;
mfp.addr = (char *)&hDll + 0x101E07;
funcPtr = mfp.memfun;

Or you can try the very ugly reference cast on the left side of the assignment. 或者你可以在作业的左侧尝试非常丑陋的参考演员。

There are additional possible complications if the function you're getting the address of is virtual and myClassInstance points to a derived class, or if multiple inheritance is involved. 如果您获取地址的函数是virtual并且myClassInstance指向派生类,或者涉及多个继承,则还有其他可能的复杂情况。

Proceed at your own risk. 继续需要您自担风险。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM