Forum > Operating Systems

MapViewOfFile identifier not found [windows]

(1/1)

safa:
Hi, in order to create a map file in lazarus (installed on windows) , I need to use the function MapViewOfFile, but I get this compilation error:MapViewOfFile identifier not found, it worked fine when I was using delphi, but it doesn't in Lazarus, so do I need to add any unit in uses? what's the problem?
PS: I get the same error for "hfile" and "createfilemapping".
Thanks for answering me.

molly:
Yes, you are sometimes required to include some other units in order to call the windows api functions.

It would simply be a too much of a job to maintain all supported platforms otherwise. The jedi windows units are included with fpc/lazarus.

With regards to MapViewOfFile, it is located inside unit jwawinbase.pas

I know you asked for examples in the other thread, but when doing a search on this function i came across some hits that might be able to help you.
The fp debugger units adds an implementation of mapped files as well as turbo's ipro (the latter is even a stream implementation inside ipstrms.pas).

Maybe they can be of help to you.

balazsszekely:
Here is a basic example how to use map files in lazarus. For the sake of this demo, I did the read/write within the same process, but the idea is the same for inter process communication:

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---uses Windows; const  FileMapName = '_ThisIsJustAUniqueString91231_'; type  PData = ^TData;  TData = packed record    FUniqueID : DWORD;    FName: string;    /add other data if you like  end;var  hFileMap: HANDLE; function WriteData(const Data: TData): Boolean;var  pPData: PData;begin  Result := False;  pPData := MapViewOfFile(hFileMap, FILE_MAP_ALL_ACCESS, 0, 0, SizeOf(TData));  if (pPData <>  nil) then  begin    pPData^.FUniqueID := Data.FUniqueID;    pPData^.FName := Data.FName;    UnmapViewOfFile(pPData);    Result := True;  end;end; function ReadData(var Data: TData): Boolean;var  hOpenFileMap: HANDLE;  pPData: PData;begin  Result := False;  hOpenFileMap := OpenFileMapping(FILE_MAP_ALL_ACCESS, False, PChar(FileMapName));  if (hOpenFileMap <> 0) then  begin    pPData := MapViewOfFile(hFileMap, FILE_MAP_ALL_ACCESS, 0, 0, 0);    if (pPData <>  nil) then    begin      Data.FUniqueID :=  pPData^.FUniqueID;      Data.FName :=  pPData^.FName;      Result := True;      UnmapViewOfFile(pPData);    end;    CloseHandle(hOpenFileMap);  end;end; procedure TForm1.FormCreate(Sender: TObject);begin  hFileMap := CreateFileMapping($FFFFFFFF, nil, PAGE_READWRITE, 0, SizeOf(TData), PChar(FileMapName));  if hFileMap = 0 then    ShowMessage('Cannot create FileMap' + sLineBreak + SysErrorMessage(GetLastOSError));end; procedure TForm1.FormDestroy(Sender: TObject);begin  CloseHandle(hFileMap);end; procedure TForm1.bWriteClick(Sender: TObject);var  Data: TData;begin  if hFileMap <> 0 then  begin    Data.FUniqueID := 15;    Data.FName := 'test test test';    if WriteData(Data) then      ShowMessage('Data successfully written to filemap!');  end;end; procedure TForm1.bReadClick(Sender: TObject);var  Data: TData;begin  if ReadData(Data) then    ShowMessage('UniqeID: ' + IntToStr(Data.FUniqueID) + sLineBreak +                'Name   : ' +  Data.FName);end;                     


PS: Feel free to ask questions if you like, shortly I will return with another example.

balazsszekely:
As I promised earlier, I will post another example. Sometimes it's useful to know if a PE(Portable Executable - exe, dll) is 32 or 64 bit, without actually running it. The task is trivial in case of a running exe(see TaskManger-->Processes tab) but non-trivial otherwise.
For this purpose I used the MapViewOfFile api:

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---uses JwaWindows; function GetPEType(const APath: WideString): Byte;const  PE_UNKNOWN = 0;  PE_16BIT   = 1;  PE_32BIT   = 2;  PE_64BIT   = 3;var  hFile, hFileMap: THandle;  PMapView: Pointer;  PIDH: PImageDosHeader;  PINTH: PImageNtHeaders;  Base: LongWord;begin  Result := PE_UNKNOWN;   hFile := CreateFileW(PWideChar(APath), GENERIC_READ, FILE_SHARE_READ, nil, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);  if hFile = INVALID_HANDLE_VALUE then  begin    CloseHandle(hFile);    Exit;  end;   hFileMap  := CreateFileMapping(hFile, nil, PAGE_READONLY, 0, 0, nil);  if hFileMap = 0 then  begin    CloseHandle(hFile);    CloseHandle(hFileMap);    Exit;  end;   PMapView := MapViewOfFile(hFileMap, FILE_MAP_READ, 0, 0, 0);  if PMapView = nil then  begin    CloseHandle(hFile);    CloseHandle(hFileMap);    Exit;  end;   PIDH := PImageDosHeader(PMapView);  if PIDH^.e_magic <> IMAGE_DOS_SIGNATURE then  begin    CloseHandle(hFile);    CloseHandle(hFileMap);    UnmapViewOfFile(PMapView);    Exit;  end;   Base := LongWord(PIDH);  PINTH := PIMAGENTHEADERS(Base + LongWord(PIDH^.e_lfanew));  if PINTH^.Signature <> IMAGE_NT_SIGNATURE then  begin    CloseHandle(hFile);    CloseHandle(hFileMap);    UnmapViewOfFile(PMapView);    Exit;  end;   if PINTH^.Signature = $4550 then  begin    case PINTH^.OptionalHeader.Magic of      $10b: Result := PE_32BIT;      $20b: Result := PE_64BIT    end;  end  else    Result := PE_16BIT;   CloseHandle(hFile);  CloseHandle(hFileMap);  UnmapViewOfFile(PMapView);end;  procedure TForm1.Button1Click(Sender: TObject);var  PEType: Byte;begin  PEType := GetPEType(WideString('c:\windows\notepad.exe'));  case PEType of    0: ShowMessage('unknown PE');    1: ShowMessage('16 bit PE');    2: ShowMessage('32 bit PE');    3: ShowMessage('64 bit PE');  end;end;
It should return 64 in case of 64 bit windows, 32 otherwise.

Navigation

[0] Message Index

Go to full version