flat assembler
Message board for the users of flat assembler.

Index > Main > Pascal translation (pointer to struct)

Author
Thread Post new topic Reply to topic
TimK



Joined: 14 Feb 2010
Posts: 20
TimK
I need to translate the following pascal code:
Code:
type
  PMyStruct = ^TMyStruct;
  TMyStruct = packed record
    A: LongWord;
    B: LongWord;
  end;

procedure WriteMapData;
var
  MapFile: LongWord;
  MapData: PMyStruct;
begin
  MapFile := OpenFileMapping(FILE_MAP_ALL_ACCESS, False, 'MyMapFileName');

  if (MapFile <> 0) then
  begin
    MapData := MapViewOfFile(MapFile, FILE_MAP_ALL_ACCESS, 0, 0, 0);

    if Assigned(MapData) then
    begin
      MapData^.B := 123;

      UnmapViewOfFile(MapData);
    end;

    CloseHandle(MapFile);
  end;
end;    


My translation:
Code:
struct MyStruct
  A dd ?
  B dd ?
ends

MapFile dd 0
MapData MyStruct ; ???

invoke  OpenFileMapping, FILE_MAP_ALL_ACCESS,FALSE,'MyMapFileName'
mov     [MapFile],eax

invoke  MapViewOfFile, [MapFile],FILE_MAP_ALL_ACCESS,0,0,0
mov     [MapData],eax

;mov     [MapData^.B],123
mov     [MapData.B],123 ; ???

invoke  UnmapViewOfFile, [MapData]
invoke  CloseHandle, [MapFile]
    


But something is wrong and MapData.B value is always 0.

Test code:
Code:
procedure CreateMapFile;
var
  MapFile: LongWord;
  MapData: PMyStruct;
begin
  MapFile := CreateFileMapping(INVALID_HANDLE_VALUE, nil,
    PAGE_READWRITE, 0, SizeOf(LongWord), 'MyMapFileName');

  if (MapFile <> 0) then
  begin
    MapData := MapViewOfFile(MapFile, FILE_MAP_ALL_ACCESS, 0, 0, 0);

    if Assigned(MapData) then
    begin
      WriteMapData;

      ShowMessage(IntToStr(MapData^.B));

      UnmapViewOfFile(MapData);
    end;

    CloseHandle(MapFile);
  end;
end;    


How to declare a pointer to the structure and fill the structure fields with a pointer to it? Sorry for my english Embarassed
Post 14 Feb 2010, 12:37
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 17474
Location: In your JS exploiting you and your system
revolution
Try this:
Code:
MapData DD ? ;this is a pointer
...
mov [eax+MyStruct.B],123    
Post 14 Feb 2010, 12:49
View user's profile Send private message Visit poster's website Reply with quote
TimK



Joined: 14 Feb 2010
Posts: 20
TimK
Thank you! It works.

This assumes that next code (filling dword variable by pointer),
Code:
var
  A: PLongWord;
begin
  A^ := 123;
end;
    


translates as:
Code:
A dd ?

mov eax,[A]
mov [eax+4],123 
    


Right?
Post 14 Feb 2010, 13:28
View user's profile Send private message Reply with quote
vid
Verbosity in development


Joined: 05 Sep 2003
Posts: 7105
Location: Slovakia
vid
No, like this:

Code:
A dd ? 

mov eax,[A] 
mov [eax],123
    
Post 14 Feb 2010, 13:55
View user's profile Send private message Visit poster's website AIM Address MSN Messenger ICQ Number Reply with quote
TimK



Joined: 14 Feb 2010
Posts: 20
TimK
Thanks vid, but my code still does not work Smile

Code:
MapFile dd 0
MapData dd 0

invoke  MapViewOfFile, [MapFile],FILE_MAP_ALL_ACCESS,0,0,0
mov     [MapData],eax
test    eax,eax
jz      error

mov     eax,[MapData]
mov     eax,123
    


like it changes the address, but not the value...
Post 14 Feb 2010, 14:42
View user's profile Send private message Reply with quote
Plue



Joined: 15 Dec 2005
Posts: 151
Plue
You didn't do as he told you.
Code:
mov     [eax],123    
Post 14 Feb 2010, 15:16
View user's profile Send private message Reply with quote
TimK



Joined: 14 Feb 2010
Posts: 20
TimK
Oh, sorry, im idiot, missed brackets... All works as expected. Many thanks!
Post 14 Feb 2010, 15:53
View user's profile Send private message Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2020, Tomasz Grysztar. Also on YouTube, Twitter.

Website powered by rwasa.