| |
 |
| The following information applies to: |
|
I was thinking one day at the office that we needed to have a
program to sort of automate our process. So I began to write some code
to run our software in steps. As I was writing the code I ran into a
small problem. I was planning on using WinExec to run my software from
Delphi. The problem was is that the program I was writing calls WinExec
but the program keeps on running. So I ended up having a bunch of
program fail on me because certain files were not created at the time.
My problem was that I would have to wait for each program to finish its
execution. I ended up diving into the windows help file and I found the
CreateProcess function which is the recommended for running 32-bit
applications. This function also returns the Process ID (or PID number)
of the application that I ran. So now all I needed to do is to make the
program wait until the Process ID did not exsist any more. So I looked
more into the help files and found a function called
CreateToolHelp32Snapshot. This function creates a "snapshot" of the
system and all the processes running. The system takes a "snapshot" so
when you loop through the processes you don't have processes and thread
stopping and disappearing on you. According to the help files, reading
directly from this area of the memory could cause GPs. Now that I have
taken the snapshot of the system, I have to read through each of the
processes and determine if my process was still there. This can be done
with the Process32First and Process32Next functions. Now that I had all
the data I needed I constructed my own unit to house the procedures I
needed for easy of portability. I created a RunProgram function which
returns the exit code of the program and a PIDExists function which
returns a boolean of True if it does and False if it doesn't. Below is the
source for these functions.
|
| Code:
|
unit PIDProcessing;
interface
function PIDExists(PID:Integer):boolean;
function RunProgram(ExeProgram:String):integer;
implimentation
uses TLHelp32, Windows;
function PIDExists(PID:Integer):boolean;
var hSnap:Cardinal;
ProcessEntry:TProcessEntry32;
Finding:LongBool;
Found:Boolean;
begin
hSnap:=CreateToolHelp32Snapshot(TH32CS_SNAPPROCESS,0);
if hSnap=0 then
begin
Result:=False;
Exit;
end;
Found:=False;
ProcessEntry.dwSize:=SizeOf(ProcessEntry);
Finding:=Process32First(hSnap,ProcessEntry);
While Finding do
begin
if PID=ProcessEntry.th32ProcessID then Found:=True;
Finding:=Process32Next(hSnap,ProcessEntry);
end;
CloseHandle(hSnap);
Result:=Found;
end;
function RunProgram(EXEProgram:String):Boolean;
var si:TStartupInfo;
pi:TProcessInformation;
begin
FillMemory(@si,sizeof(si),0);
si.cb:=Sizeof(si);
if EXEProgram<>'' then
begin
if CreateProcess(nil,PChar(EXEProgram),
nil,nil,false,NORMAL_PRIORITY_CLASS,nil,nil,si,pi) then
begin
While PIDExists(pi.dwProcessId) do Application.ProcessMessages;
CloseHandle(pi.hProcess);
CloseHandle(pi.hThread);
Result:=True;
end
else
Result:=False;
end;
Result:=True;
end;
end.
|
| Reminder: I've converted the PID and the PPID for Hexadecimal for easy viewing.
|
Submission Information:
Submitted by: Jeremy Walton
|
| E-Mail: jeremyw@dicecorp.com
|
|
|
| Hits/month |
2,500,000+ |
Downloads (Since May 2000) |
7,393,709 |
| Total Files |
6,023 |
| Forum msgs |
7,670 |
| Articles/FAQs |
70+/900+ |
Top Selling Software at Amazon
|