Visit our Sponsor   Visit our Sponsor
delphi3000.com - the free delphi knowledge platform
delphi3000.com - the free delphi knowledge platform
Have a look at your member-status

connecting people's knowledge


  - Recent ArticlesRSS feed for Recent Articles on delphi3000.com
  - List of All Articles
  - Top Viewed Articles
  - Articles (+Attachem.)
  - Articles Of Interest
  - Categories
  - Top Uploader
  - Search
  - Index

  - My Home
  - Submit an Article
  - My Articles
  - My Personal Data
  - My Bookmarks
  - Activities
  - Login/Logout

  - Sign Up
  - Why Sign Up
  - Newsletter

  - Press
  - Advertise

  - Contact
  - Feedback





Community
Borland
ClubeDelphi
Dr. Bob
UK-BUG
Delphi Meetings
Planeta Delphi







Startblatt.de






Share this article with friendsShare this article with friends
Rate this articleRate this article - to keep the quality of delphi3000.com !
Comment this article or read through previous comments (2)


Get a process by executable path and read it's memory spaceFormat this article printer-friendly!Bookmark function is only available for registered users!
uses mainly CreateToolhelp32Snapshot,OpenProcess,ReadProcessMemory
Product:
Delphi 6.x (or higher)
Category:
System
Skill Level:
Scoring:
Last Update:
01/06/2002
Search Keys:
delphi delphi3000 article borland vcl code-snippet Toolhelp32Snapshot OpenProcess ReadProcessMemory
Times Scored:
4
Visits:
5684
Uploader: pic sub
Company:
Reference: N/A
 
Question/Problem/Abstract:
Can I look in another process's address space ?
Answer:



{==========================================================
  Here's an example of ReadProcessMemory (guess what it
  does (:). The ReadProcessMem function searches for a
  process who's executable file path matches 'exepath'.
  If it finds it then gets the associated processID.
  OpenProcess returns the handle used by ReadProcessMemory.
  The function pushes at lpBuff the data read and ret :
      0 : all OK
      1 : executable not loaded
      2 : could not open process
      3 : error on read memory
===========================================================}

function ReadProcessMem(exepath : string;
                lpBuff : PByteArray; BytesToRead : LongWord;
                var BytesRead : LongWord) : Word;
var
   procinfo             : tagPROCESSENTRY32;
   snaph,proch          : cardinal;
   isover               : boolean;
begin
    result:=0;
    exepath:=uppercase(exepath);
    snaph:=CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS,0);
    procinfo.dwSize:=sizeof(PROCESSENTRY32);
    isover:=false;
    if not Process32First(snaph,procinfo) then
      isover:=true;
    while (uppercase(procinfo.szExeFile)<>exepath)
          and (not isover) do
    begin
      if not Process32Next(snaph,procinfo)
         then isover:=true;
    end;
    CloseHandle(snaph);
    if isover then
    begin
      result:=1;
      exit;
    end
    else
    begin
      proch:=OpenProcess(PROCESS_VM_READ,
                         false,procinfo.th32ProcessID);
      if proch=0 then
      begin
        result:=2;
        exit;
      end
      else
      begin
        if not ReadProcessMemory(proch,nil,
                        lpBuff,BytesToRead,
                        BytesRead) then
        begin
          result:=3;
          exit;
        end;
      end;
      closehandle(proch);
    end;
end;

{
  if you like to know if it reads anything try
  this next code : (include a button on your form)
}

procedure TForm1.Button1Click(Sender: TObject);
var
  buff : PByteArray;
  bytesread : LongWord;
  sresult : string;
begin
  buff:=allocmem(1024);
  Case ReadProcessMem('C:\Windows\explorer.exe',
                buff,1024,bytesread) of
    0 : sresult:='all OK';
    1 : sresult:='executable not loaded';
    2 : sresult:='could not open process';
    3 : sresult:='error on readmemory';
  end;
  MessageBox(0,PChar(sresult),'result is',0);
end;

{
  after getting the desired data into your buffer
  you my use it freely (no copyright :) )
}





Please rate this article!
Skill level:
BeginnerExpert

Useful:
No!Very!

Overall rating:
PoorExcellent



Comments to this article
Write a new comment
Problem with directory/name of exe
    Melmock mel (May 27 2005 11:34AM)

change line:
    while (uppercase(procinfo.szExeFile)<> exepath) and (not isover) do

with:
    while (uppercase(procinfo.szExeFile)<> ExtractFileName(exepath)) and (not isover) do

Respond

TlHelp32 !
    Peter Panino (Feb 29 2004 10:36PM)

You forgot to include the statement:

uses TlHelp32;
Respond














 
Sign up to consume product discounts for Bronze memberships !

read more


  Visit our Sponsor

 

  Community Ad of
M. Shkolnik
 
   














 







     
  Copyright © 2000 - 2007 delphi3000.com - All rights reserved. Terms of use. || Privacy
delphi3000.com is a service by bluestep.com IT-Services GmbH (Vienna)