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

Tips (1541)

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

OpenTools API (3)
Printing (35)
Strings (83)
System (266)
VCL (242)

Top15

Tips sort by
component


Search Tip

Add new Tip

Add&Win Game

Advertising

39 Visitors Online


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

Tip Rating (12):  
     


//...

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