whats new ¦  programming tips ¦  indy articles ¦  intraweb articles ¦  informations ¦  links ¦  interviews
 misc ¦  tutorials ¦  Add&Win Game

Tips (1548)

Database (90)
Files (137)
Forms (107)
Graphic (116)
IDE (21)
Indy (5)
Internet / LAN (131)
IntraWeb (0)
Math (76)
Misc (127)
Multimedia (45)
Objects/
ActiveX (51)

OpenTools API (3)
Printing (35)
Strings (83)
System (268)
VCL (243)

Top15

Tips sort by
component


Search Tip

Add new Tip

Add&Win Game

Advertising

36 Visitors Online


 
... get info on memory usage from a given process?
Autor: David
[ Print tip ]  

Tip Rating (10):  
     


//...

uses
  
psAPI;


//...

function GetProcessMemorySize(_sProcessName: stringvar _nMemSize: Cardinal): Boolean;
var
  
l_nWndHandle, l_nProcID, l_nTmpHandle: HWND;
  l_pPMC: PPROCESS_MEMORY_COUNTERS;
  l_pPMCSize: Cardinal;
begin
  
l_nWndHandle := FindWindow(nil, PChar(_sProcessName));

  if l_nWndHandle = 0 then 
  begin
    
Result := False;
    Exit;
  end;

  l_pPMCSize := SizeOf(PROCESS_MEMORY_COUNTERS);

  GetMem(l_pPMC, l_pPMCSize);
  l_pPMC^.cb := l_pPMCSize;

  GetWindowThreadProcessId(l_nWndHandle, @l_nProcID);
  l_nTmpHandle := OpenProcess(PROCESS_ALL_ACCESS, False, l_nProcID);

  if (GetProcessMemoryInfo(l_nTmpHandle, l_pPMC, l_pPMCSize)) then
    
_nMemSize := l_pPMC^.WorkingSetSize
  else
    
_nMemSize := 0;

  FreeMem(l_pPMC);

  Result := True;
end;

//Beispiel

procedure TForm1.Button1Click(Sender: TObject);
var
  
l_nSize: Cardinal;
begin
  if 
(GetProcessMemorySize('Unbenannt - Editor', l_nSize)) then
    
ShowMessage('Size: ' + IntToStr(l_nSize) + ' byte')
  else
    
ShowMessage('Error');
end;


 

Rate this tip:

poor
very good


Copyright © by SwissDelphiCenter.ch
All trademarks are the sole property of their respective owners