can somebody tell me where I am making mistake in the converting:
C:
typedef struct _REGISTRY_EVENT {
REG_NOTIFY_CLASS eventType;
TIME_FIELDS time;
HANDLE processId;
ULONG dataType;
ULONG dataLengthB;
ULONG registryPathLengthB;
/* Contains path and optionally data */
UCHAR registryData[];
} REGISTRY_EVENT, * PREGISTRY_EVENT;
Delphi:
_Registry_Event = record
EventType: REG_NOTIFY_CLASS;
Time: TIME_FIELDS;
processID: THandle;
DataType: ULONG;
DataLength: ULONG;
开发者_高级运维 registryPathLength: ULONG;
registryData: array of UCHAR;
end;
from the c code sizeof(REGISTRY_EVENT) = 36
from the delphi code sizeof(REGISTRY_EVENT) = 40
Thanks in advance
Bojan
The error is here
registryData: array of UCHAR;
You don't need a dynamic array here.
Updated:
I assume that
UCHAR registryData[];
is a C hack to create a zero-length field at the end of structure. An equivalent Delphi hack is
registryData: record end;
to access this dummy field as an array in Delphi, you should typecast:
type
PByteArr = ^TByteArr;
TByteArr = array[0..$FFFF] of Byte;
PRec = ^TRec;
TRec = packed record
Data: Integer;
MoreData: record end;
end;
procedure TForm1.Button1Click(Sender: TObject);
var
P: PRec;
PMoreData: PByteArr;
begin
P:= AllocMem(SizeOf(TRec) + 4);
PMoreData:= @P^.MoreData;
PMoreData^[2]:= 3;
ShowMessage(IntToStr(PMoreData^[2]));
FreeMem(P);
end;
but probably you need to typecast this dummy field to something else since the field definition is just a hack.
And finally: Never use Delphi dynamic arrays (like registryData: array of UCHAR;) while converting C structures to Delphi. Dynamic array in Delphi is a lifetime-managed reference type that has no exact equivalent in C.
I'm not sure it's a mistake -- it may well be the compiler inserting some padding between fields. Try adding {$Align off}
before the record definition, or change = Record
to = Packed Record
and see if that helps.
Edit: while this may also be a problem, @Serg's answer strikes me as a lot more likely.
UCHAR registryData[];
is
registryData: array[0..0] of UCHAR;
With packed records and packed array for the types the size of _Registry_Event equals 34.
This is probably due to either different sizes of the data types of each field, or due to different padding. For the field sizes, print the sizeof() or size() for each and compare. If they are all the same, then it's padding, and you may need to find a compiler option to adjust that.
That is, if you care. Why do you care if the sizes are the same?
精彩评论