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

时间:2009-10-25 21:02:28

标签: c++ detours

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

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

这就是我在做的事情:

HRESULT (WINAPI *Real_IHTMLDocument2_write)(SAFEARRAY *) = &IHTMLDocument2::write

感谢您的帮助!

2 个答案:

答案 0 :(得分:6)

指向函数的指针具有以下类型:

HRESULT (WINAPI IHTMLDocument2::*)(SAFEARRAY*)

正如您所看到的,它的等级名称是合格的。它需要一个类的实例来调用(因为它不是一个静态函数):

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 */);

答案 1 :(得分:4)

您需要使用member function pointer。普通函数指针不起作用,因为当您调用(非静态)类成员函数时,会有一个隐式this指针指向该类的实例。

相关问题