21,459
社区成员
发帖
与我相关
我的任务
分享
#define INITGUID
#include <Windows.h> // Define HRESULT
#include <iostream>
#include <objbase.h> // Define interface
#include <iomanip>
using namespace std;
interface IDictionary : public IUnknown
{
virtual void _stdcall Initialize() = 0;
virtual void _stdcall Uninitialize() = 0;
};
interface ISpellCheck : public IUnknown
{
virtual void _stdcall CheckWord() = 0;
};
class CDictionary : public IDictionary, public ISpellCheck
{
public:
virtual HRESULT _stdcall QueryInterface(const IID&, void** ppv)
{
static int i;
cout << "QueryInterface " << i++ << endl;
return 0;
};
virtual ULONG _stdcall AddRef() { cout << "AddRef" << endl; return 0; };
virtual ULONG _stdcall Release() { cout << "Release" << endl; return 0; };
virtual void _stdcall Initialize() { cout << "Initialize" << endl; };
virtual void _stdcall Uninitialize() { cout << "Uninitialize" << endl; };
virtual void _stdcall CheckWord() { cout << "CheckWord" << endl; };
int a;
};
// {3340DDDC-490B-4a3c-9CC9-0E3DB217D36A}
DEFINE_GUID(MyIID, 0x3340dddc, 0x490b, 0x4a3c, 0x9c, 0xc9, 0xe, 0x3d, 0xb2, 0x17, 0xd3, 0x6a);
int main(void)
{
CDictionary *pA = new CDictionary;
cout << "对象地址:" << (int*)pA << endl;
cout << "第1个ppv地址:" << (int*)pA << endl;
cout << "第1个ppv地址内容:" << setiosflags(ios::uppercase) << setw(8) << setfill('0') << hex << *(int*)pA << endl;
cout << "第2个ppv地址:" << (int*)pA+1 << endl;
cout << "第2个ppv地址内容:" << setiosflags(ios::uppercase) << setw(8) << setfill('0') << hex << *((int*)pA+1) << endl;
cout << "虚函数表 — 第1个函数指针:" << setiosflags(ios::uppercase) << setw(8) << setfill('0') << hex << ((int*)*(int*)pA+0) << endl;
cout << "虚函数表 — 第1个函数地址:" << setiosflags(ios::uppercase) << setw(8) << setfill('0') << hex << *((int*)*(int*)pA+0) << endl;
cout << "虚函数表 — 第2个函数地址:" << setiosflags(ios::uppercase) << setw(8) << setfill('0') << hex << *((int*)*(int*)pA+1) << endl; // 为啥地址和下面的不一样
cout << "虚函数表 — 第3个函数地址:" << setiosflags(ios::uppercase) << setw(8) << setfill('0') << hex << *((int*)*(int*)pA+2) << endl;
cout << "虚函数表 — 第4个函数地址:" << setiosflags(ios::uppercase) << setw(8) << setfill('0') << hex << *((int*)*(int*)pA+3) << endl;
cout << "虚函数表 — 第5个函数地址:" << setiosflags(ios::uppercase) << setw(8) << setfill('0') << hex << *((int*)*(int*)pA+4) << endl;
cout << "虚函数表 — 第6个函数地址:" << setiosflags(ios::uppercase) << setw(8) << setfill('0') << hex << *((int*)*(int*)pA+5) << endl;
cout << "虚函数表 — 第2个函数指针:" << setiosflags(ios::uppercase) << setw(8) << setfill('0') << hex << ((int*)*((int*)pA+1)) << endl;
cout << "虚函数表 — 第1个函数地址:" << setiosflags(ios::uppercase) << setw(8) << setfill('0') << hex << *((int*)*((int*)pA+1)+0) << endl; // 为啥地址和上面的不一样
cout << "虚函数表 — 第2个函数地址:" << setiosflags(ios::uppercase) << setw(8) << setfill('0') << hex << *((int*)*((int*)pA+1)+1) << endl;
cout << "虚函数表 — 第3个函数地址:" << setiosflags(ios::uppercase) << setw(8) << setfill('0') << hex << *((int*)*((int*)pA+1)+2) << endl;
cout << "虚函数表 — 第4个函数地址:" << setiosflags(ios::uppercase) << setw(8) << setfill('0') << hex << *((int*)*((int*)pA+1)+3) << endl;
cout << "虚函数表 — 第5个函数地址:" << setiosflags(ios::uppercase) << setw(8) << setfill('0') << hex << *((int*)*((int*)pA+1)+4) << endl;
cout << "数据成员地址: " << &pA->a << endl;
IDictionary *e = pA;
ISpellCheck *f = pA;
e->QueryInterface(MyIID, NULL); // 验证是否是调用相同函数
f->QueryInterface(MyIID, NULL);
delete pA;
return 0;
}
对象地址:00831DC0
第1个ppv地址:00831DC0
第1个ppv地址内容:004712CC
第2个ppv地址:00831DC4
第2个ppv地址内容:004712B8
虚函数表 — 第1个函数指针:004712CC
虚函数表 — 第1个函数地址:0040125D
虚函数表 — 第2个函数地址:00401078
虚函数表 — 第3个函数地址:004012DA
虚函数表 — 第4个函数地址:004012C6
虚函数表 — 第5个函数地址:0040105F
虚函数表 — 第6个函数地址:00000000
虚函数表 — 第2个函数指针:004712B8
虚函数表 — 第1个函数地址:0040109B
虚函数表 — 第2个函数地址:00401087
虚函数表 — 第3个函数地址:00401096
虚函数表 — 第4个函数地址:00401091
虚函数表 — 第5个函数地址:00000000
数据成员地址: 00831DC8
QueryInterface 0
QueryInterface 1
Press any key to continue