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

时间:2017-03-17 02:24:09

标签: c++ hook code-injection malware

我试图获取成员函数的地址。基本上我试图获取内存中加载的windows api函数和我的函数之间的偏移量。

这就是我试图获取地址的原因:

   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;

2 个答案:

答案 0 :(得分:1)

此代码快照显示了如何获取类成员函数的地址:

#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();
}

答案 1 :(得分:0)

一般情况下,不允许这样做,也不建议使用。

do{
//All logic goes here

//At the end of the game take input from user in the form of 'Y|y' or 'N|n'

System.out.println("Do you want to continue : press 'Y' for yes and 'N' for no");  
Scanner sc=new Scanner(System.in);
String choice=sc.nextLine();   
} while(choice.equalsIgnoreCase("Y"))

void (Class_Name::* FuncPointer)() = &Class_Name::Func_Name; 将指向类方法。

最好在C ++ 11中使用静态函数或lambda特性。