CONTAINING_RECORD
MSDN描述:
CONTAINING_RECORD returns the base address of an instance of a structure given the type of the structure and the address of a field within the containing structure.
Syntax
PCHAR CONTAINING_RECORD(IN PCHAR Address, IN TYPE Type, 
IN PCHAR Field);
它的功能为已知结构体或类的某一成员、对象中该成员的地址和这一结构体名或类名,从而得到该对象的基地址。
其宏可以如此写:
#define CONTAINING_RECORD(address,type,field) \
((type*)((PCHAR)(address)-(ULONG_PTR)(&((type*)0)->field)))
实例:
#include <windows.h>  
#include <stdio.h>  
typedef struct
{
	int a;
	int b;
	int c;
	int d;
}DATA, *PDATA;
int main()
{
	DATA d = { 1, 2, 3, 4 };
	PDATA d2 = CONTAINING_RECORD(&d.b, DATA, b);
	printf("%d %d %d %d\n", d2->a, d2->b, d2->c, d2->d);  //Input:1 2 3 4
	int *d3 = (int*)(&((PDATA)0)->b);
	printf("%p\r\n", d3);  //Input:00000004 即b的相对地址
	ULONG_PTR Offset = (ULONG_PTR)d3;  //转成数字
	PDATA fin = (PDATA)((char*)(&d.b) - Offset);
	printf("%d %d %d %d\n", fin->a, fin->b, fin->c, fin->d);  //Input:1 2 3 4
	return 0;
}
                    
                
                
            
        
浙公网安备 33010602011771号