All: I will be the first to admit that I have never been very good at developing macros so I was wondering if someone could assist me. What I would like to do is to take the following definition (from the NTDEF.H file) and make an equivalent macro:

#define FIELD_OFFSET(type, field)    ((LONG)&(((type *)0)->field))
an C example of it's use would be:

#define PCI_COMMON_HDR_LENGTH (FIELD_OFFSET (PCI_COMMON_CONFIG, DeviceSpecific))
Thank you.
Posted on 2001-05-01 18:55:00 by madprgmr
Inside my copy of NTDEF.H, I have the following:

// begin_winnt begin_ntminiport
// Calculate the byte offset of a field in a structure of type type.

#define FIELD_OFFSET(type, field)    ((LONG)(INT_PTR)&(((type *)0)->field))
Well... if I understand what this is doing, the answer is kinda simple. Seems to me it's computing the distance into a stucture that an element resides. For example, if you had

MyStruct 
   A DWORD 0
   B DWORD 0
ENDS
Then FIELD_OFFSET(MyStruct, B) would be 4. I'm basing this on the C cast of ZER0 to 'type', rather then a specific instance of 'type'. So....

FIELD_OFFSET MACRO type:REQ, field:REQ
   xor eax, eax
   lea .type.feild
ENDM
That's the easy answer. What I don't like is it's CODE... this should be do-able completely as a macro (compile time thing) without any registers so you could say use the return value to load into ANY register (or memory) or your choice. Oh, and I didn't try actually compiling this, but I suspect there is at least one reserved keyword (if not two) in there ('field' and 'type' may need be renamed) Anyone else wanna try? :-) This message was edited by Ernie, on 5/2/2001 12:12:39 AM
Posted on 2001-05-02 00:11:00 by Ernie
Ernie: I greatly appreciate your assistance.
Posted on 2001-05-02 12:36:00 by madprgmr