简体   繁体   English

如何获取类中成员函数的地址

[英]how to get the address of a member function within a class

I'm trying to get the address of a member function. 我正在尝试获取成员函数的地址。 Basically I'm trying to get the offset between a windows api function loaded in memory and my function. 基本上,我试图获取内存中加载的Windows api函数与我的函数之间的偏移量。

This is what I've tried to get the address: 这是我尝试获取的地址:

   NTSTATUS (NTAPI myntquerydirectoryfile)(
        HANDLE hFile,
        HANDLE hEvent,
        PVOID pApcRoutine,
        PVOID pApcContext,
        IO_STATUS_BLOCK* ioStatus,
        PVOID pBuffer,
        ULONG bufferSize,
        FILE_INFORMATION_CLASS infoClass,
        BOOLEAN singleEntry,
        PUNICODE_STRING pFileName,
        BOOLEAN restart
    )
    {

        cout << "my func" << endl;

    };


    typedef  NTSTATUS (*pmyhook)(HANDLE hFile,
        HANDLE hEvent,
        PVOID pApcRoutine,
        PVOID pApcContext,
        IO_STATUS_BLOCK* ioStatus,
        PVOID pBuffer,
        ULONG bufferSize,
        FILE_INFORMATION_CLASS infoClass,
        BOOLEAN singleEntry,
        PUNICODE_STRING pFileName,
        BOOLEAN restart);

    pmyhook = &myntquerydirectoryfile;

This code snap shows how to get address of class member functions: 此代码快照展示了如何获取类成员函数的地址:

#include <stdio.h>
class A 
{
public:
    void test() 
    {
        // Address of member function test(), is the value of &A::test
        printf("Address of function is: 0x%X\n", &A::test);
    }
};

int main()
{
    A a;
    a.test();
}

In general, it's not allowed and not recommended. 通常,不允许这样做,不建议这样做。

void (Class_Name::* FuncPointer)() = &Class_Name::Func_Name;

FuncPointer will point to the class method. FuncPointer将指向类方法。

Better use static function or the lambda feature in C++ 11. 最好在C ++ 11中使用静态函数或lambda功能。

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

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