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

发布于 2024-08-08 16:57:09 字数 369 浏览 6 评论 0原文

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

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

这就是我正在做的:

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

感谢您的帮助!

I have to set up a pointer to a library function (IHTMLDocument2::write) which is a method of the class IHTMLDocument2. (for the curious: i have to hook that function with Detours)

I can't do this directly, because of type mismatch, neither can I use a cast (reinterpret_cast<> which is the "right one" afaik doesn't work)

Here's what I am doing:

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

Thanks for your help!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

满意归宿 2024-08-15 16:57:09

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

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

The pointer to function has the following type:

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

As you can see, it's qualified with it's class name. It requires an instance of a class to call on (because it is not a static function):

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 */);
月棠 2024-08-15 16:57:09

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

You need to use a member function pointer. A normal function pointer won't work, because when you call a (non-static) class member function there is an implicit this pointer referring to an instance of the class.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文