C++ - 指向类方法的指针

zak*_*akk 0 c++ detours

我必须设置一个指向库函数(IHTMLDocument2::write)的指针,该函数是类的一个方法IHTMLDocument2.(对于好奇:我必须使用Detours挂钩该功能)

我不能直接这样做,因为类型不匹配,我也不能使用强制转换(reinterpret_cast<>这是"正确的"afaik不起作用)

这就是我在做的事情:

HRESULT (WINAPI *Real_IHTMLDocument2_write)(SAFEARRAY *) = &IHTMLDocument2::write
Run Code Online (Sandbox Code Playgroud)

谢谢你的帮助!

GMa*_*ckG 6

函数指针有以下类型:

HRESULT (WINAPI IHTMLDocument2::*)(SAFEARRAY*)
Run Code Online (Sandbox Code Playgroud)

如您所见,它的类名称合格.它需要一个类的实例来调用(因为它不是一个静态函数):

typedef HRESULT (WINAPI IHTMLDocument2::*DocumentWriter)(SAFEARRAY*);

DocumentWriter writeFunction = &IHTMLDocument2::write;

IHTMLDocument2 someDocument = /* Get an instance */;
IHTMLDocument2 *someDocumentPointer = /* Get an instance */;

(someDocument.*writefunction)(/* blah */);
(someDocumentPointer->*writefunction)(/* blah */);
Run Code Online (Sandbox Code Playgroud)