What does C # Marshal.ReadInt32 (IntPtr) mean in Delphi?

I am translating C # code to Delphi, I understand that (msg = TMsg):

(int)msg.LParam

      

- just the cast (please correct me if i'm wrong) however the following puzzles my mind:

Marshal.ReadInt32( (IntPtr)msg.WParam, 4 )

      

can someone please clarify this?

+3


source to share


1 answer


This just reads a 4 byte integer from the pointer. In managed .net code, you don't have pointers (unless you're using unsafe code), so the framework provides tools for communication between the native and managed worlds. The MSDN documentation for .net libraries is comprehensive and of course, describes Marshal.ReadInt32

.

An additional complication here is that there is an additional 4 byte offset. In fact, the pointer is probably pointing to a structure, and this code allocates an integer value at offset 4 of the structure. This is overwhelmingly the most likely explanation for the code as it is.

Now, literal translation:

PInteger(msg.WParam+4)^

      



but you can also write it in Delphi like this:

type
  TMyRecord = record
    i1: Integer;
    i2: Integer;
  end;
  PMyrecord = ^TMyRecord;
....
value := PMyRecord(msg.WParam)^.i2;

      

If you know which message this one refers to WParam

, then you will also know what will use true here record

. And so you will not need to define the highlighted entry, as Windows.pas

it will already do that.

+8


source







All Articles