DelRunner
asked on
Check if a program is running
Hi there !
Could anyone tell me how to check if a particular program (.exe) is running, and if so how to kill / close it ?
DelRunner
Could anyone tell me how to check if a particular program (.exe) is running, and if so how to kill / close it ?
DelRunner
Hello
Use FindWindow and WM_CLose to to close the application
procedure TForm1.Button1Click(Sender : TObject);
var
H : THandle;
begin
H := FindWindow('notepad',nil);
PostMessage(H,WM_Close,0,0 );
end;
if you want to force the application to close without saving use WM_Quit
also you can find the application by it's titlebar caption
like
H := FindWindow(nil,'untitled - notepad');
Use FindWindow and WM_CLose to to close the application
procedure TForm1.Button1Click(Sender
var
H : THandle;
begin
H := FindWindow('notepad',nil);
PostMessage(H,WM_Close,0,0
end;
if you want to force the application to close without saving use WM_Quit
also you can find the application by it's titlebar caption
like
H := FindWindow(nil,'untitled - notepad');
But waht if the application have no windows and what if the application stacked
Secondly you can use exitprocess instead of terminate process function which is the same that you told but saves the changes.
I think this came from another question here at EE so I cant take the credit really. Cut n paste this and save it to ProcessViewer.pas and save it in your program directory.
unit ProcessViewer;
interface
uses
Windows, Dialogs, SysUtils, Classes, ShellAPI, TLHelp32, Forms;
const
SleepForReCheck=5000;
type TProcessInfo=record
FileName: string;
Caption: string;
Visible: boolean;
Handle: DWord;
PClass: string;
ThreadID: DWord;
PID: DWord;
end;
var
DateiList,CaptionList,Visi bleList,Ha ndleList,C lassList,T hreadIdLis t,PIDList: TStringList;
ProcessInfo: array of TProcessInfo;
function EnumWindowsProc(hWnd: HWND; lParam: LPARAM): Bool; stdcall;
function KillProcessByPID(PID: DWord): boolean;
function KillProcessByFileName(File Name: string; KillAll: boolean): boolean;
procedure GetProcessList;
function GetFileNameFromHandle(Hand le: hwnd):string;
function IsFileActive(FileName: String): boolean;
implementation
procedure GetProcessList;
var
i,Laenge: integer;
begin
DateiList.Clear;
HandleList.Clear;
ClassList.Clear;
CaptionList.Clear;
VisibleList.Clear;
ThreadIdList.Clear;
PIDList.Clear;
EnumWindows(@EnumWindowsPr oc, 0);
Laenge:=DateiList.Count;
SetLength(ProcessInfo,Laen ge);
for i:=0 to Laenge-1 do
begin
DateiList[i]:=UpperCase(Da teiList[i] );
with ProcessInfo[i] do
begin
FileName:=DateiList[i];
Caption:=CaptionList[i];
Visible:=VisibleList[i]='1 ';
Handle:=StrToInt64(HandleL ist[i]);
PClass:=ClassList[i];
ThreadID:=StrToInt64(Threa dIdList[i] );
PID:=StrToInt64(PIDList[i] );
end;
end;
end;
function IsFileActive(FileName: String): boolean;
var
i: integer;
begin
result:=false;
if FileName='' then exit;
GetProcessList;
FileName:=UpperCase(Extrac tFileName( FileName)) ;
for i:=0 to Length(ProcessInfo)-1 do
begin
if Pos(FileName,ProcessInfo[i ].FileName )>0 then
begin
result:=true;
break;
end;
end;
end;
function GetFileNameFromHandle(Hand le: hwnd):string;
var
PID: DWord;
aSnapShotHandle: THandle;
ContinueLoop: Boolean;
aProcessEntry32: TProcessEntry32;
begin
GetWindowThreadProcessID(H andle, @PID);
aSnapShotHandle := CreateToolHelp32SnapShot(T H32CS_SNAP PROCESS, 0);
aProcessEntry32.dwSize := SizeOf(aProcessEntry32);
ContinueLoop := Process32First(aSnapShotHa ndle, aProcessEntry32);
while Integer(ContinueLoop) <> 0 do
begin
if aProcessEntry32.th32Proces sID = PID then
begin
result:=aProcessEntry32.sz ExeFile;
break;
end;
ContinueLoop := Process32Next(aSnapShotHan dle, aProcessEntry32);
end;
CloseHandle(aSnapShotHandl e);
end;
function EnumWindowsProc(hWnd: HWND; lParam: LPARAM): Bool;
var
Capt,Cla: array[0..255] of char;
Datei: string;
ident: dword;
begin
GetWindowText(hWnd, Capt, 255);
GetClassName(hwnd,Cla,255) ;
ThreadIdList.Add(IntToStr( GetWindowT hreadProce ssId(hwnd, nil)));
Datei:=GetFileNameFromhand le(hwnd);
DateiList.Add(Datei);
HandleList.Add(IntToStr(HW nd));
if IsWindowVisible(HWnd) then VisibleList.Add('1') else VisibleList.Add('0');
ClassList.Add(Cla);
CaptionList.Add(Capt);
GetWindowThreadProcessId(S trToInt(Ha ndleList[H andleList. Count-1]), @ident);
PIDList.Add(IntToStr(ident ));
Result:=true;
end;
function KillProcessByPID(PID : DWord): boolean;
var
myhandle : THandle;
i: integer;
begin
myhandle := OpenProcess(PROCESS_TERMIN ATE, False, PID);
TerminateProcess(myhandle, 0);
for i:=0 to SleepForReCheck do Application.ProcessMessage s; //Genug Zeit geben
GetProcessList;
Result:=PIDList.IndexOf(In tToStr(PID ))=-1;
end;
function KillProcessByFileName(File Name: string; KillAll: boolean): boolean;
var
i: integer;
FileFound: boolean;
begin
result:=false;
if FileName='' then exit;
FileName:=UpperCase(Extrac tFileName( FileName)) ;
result:=true;
GetProcessList;
if KillAll then
begin
//Kill all
FileFound:=false;
repeat
GetProcessList;
FileFound:=false;
for i:=0 to DateiList.Count-1 do
begin
if Pos(Filename,DateiList[i]) >0 then
begin
FileFound:=true;
break;
end;
end;
if i<DateiList.Count then
begin
if not KillProcessByPID(StrToInt6 4(PIDList[ i])) then
begin
result:=false;
exit;
end;
end;
until not FileFound;
end else
begin
//Kill one
for i:=0 to DateiList.Count-1 do
begin
if Pos(Filename,DateiList[i]) >0 then break;
end;
if i<DateiList.Count then
begin
if not KillProcessByPID(StrToInt6 4(PIDList[ i])) then
begin
result:=false;
exit;
end;
end;
end;
end;
initialization
DateiList:=TStringList.Cre ate;
HandleList:=TStringList.Cr eate;
ClassList:=TStringList.Cre ate;
CaptionList:=TStringList.C reate;
VisibleList:=TStringList.C reate;
ThreadIdList:=TStringList. Create;
PIDList:=TStringList.Creat e;
finalization
DateiList.Free;
HandleList.Free;
ClassList.Free;
CaptionList.Free;
VisibleList.Free;
ThreadIdList.Free;
PIDList.Free;
end.
Then to use it just add ProcessViewer to your uses and an example is like this:
procedure Torm1.Button1Click(Sender: TObject);
Begin
If isFileActive('Notepad.exe' ) then
Begin
ShowMessage('Notepad is found, im about to kill it!);
killProcessByFilename('Not epad.exe', True);
end;
end;
Regards
Smurff
unit ProcessViewer;
interface
uses
Windows, Dialogs, SysUtils, Classes, ShellAPI, TLHelp32, Forms;
const
SleepForReCheck=5000;
type TProcessInfo=record
FileName: string;
Caption: string;
Visible: boolean;
Handle: DWord;
PClass: string;
ThreadID: DWord;
PID: DWord;
end;
var
DateiList,CaptionList,Visi
ProcessInfo: array of TProcessInfo;
function EnumWindowsProc(hWnd: HWND; lParam: LPARAM): Bool; stdcall;
function KillProcessByPID(PID: DWord): boolean;
function KillProcessByFileName(File
procedure GetProcessList;
function GetFileNameFromHandle(Hand
function IsFileActive(FileName: String): boolean;
implementation
procedure GetProcessList;
var
i,Laenge: integer;
begin
DateiList.Clear;
HandleList.Clear;
ClassList.Clear;
CaptionList.Clear;
VisibleList.Clear;
ThreadIdList.Clear;
PIDList.Clear;
EnumWindows(@EnumWindowsPr
Laenge:=DateiList.Count;
SetLength(ProcessInfo,Laen
for i:=0 to Laenge-1 do
begin
DateiList[i]:=UpperCase(Da
with ProcessInfo[i] do
begin
FileName:=DateiList[i];
Caption:=CaptionList[i];
Visible:=VisibleList[i]='1
Handle:=StrToInt64(HandleL
PClass:=ClassList[i];
ThreadID:=StrToInt64(Threa
PID:=StrToInt64(PIDList[i]
end;
end;
end;
function IsFileActive(FileName: String): boolean;
var
i: integer;
begin
result:=false;
if FileName='' then exit;
GetProcessList;
FileName:=UpperCase(Extrac
for i:=0 to Length(ProcessInfo)-1 do
begin
if Pos(FileName,ProcessInfo[i
begin
result:=true;
break;
end;
end;
end;
function GetFileNameFromHandle(Hand
var
PID: DWord;
aSnapShotHandle: THandle;
ContinueLoop: Boolean;
aProcessEntry32: TProcessEntry32;
begin
GetWindowThreadProcessID(H
aSnapShotHandle := CreateToolHelp32SnapShot(T
aProcessEntry32.dwSize := SizeOf(aProcessEntry32);
ContinueLoop := Process32First(aSnapShotHa
while Integer(ContinueLoop) <> 0 do
begin
if aProcessEntry32.th32Proces
begin
result:=aProcessEntry32.sz
break;
end;
ContinueLoop := Process32Next(aSnapShotHan
end;
CloseHandle(aSnapShotHandl
end;
function EnumWindowsProc(hWnd: HWND; lParam: LPARAM): Bool;
var
Capt,Cla: array[0..255] of char;
Datei: string;
ident: dword;
begin
GetWindowText(hWnd, Capt, 255);
GetClassName(hwnd,Cla,255)
ThreadIdList.Add(IntToStr(
Datei:=GetFileNameFromhand
DateiList.Add(Datei);
HandleList.Add(IntToStr(HW
if IsWindowVisible(HWnd) then VisibleList.Add('1') else VisibleList.Add('0');
ClassList.Add(Cla);
CaptionList.Add(Capt);
GetWindowThreadProcessId(S
PIDList.Add(IntToStr(ident
Result:=true;
end;
function KillProcessByPID(PID : DWord): boolean;
var
myhandle : THandle;
i: integer;
begin
myhandle := OpenProcess(PROCESS_TERMIN
TerminateProcess(myhandle,
for i:=0 to SleepForReCheck do Application.ProcessMessage
GetProcessList;
Result:=PIDList.IndexOf(In
end;
function KillProcessByFileName(File
var
i: integer;
FileFound: boolean;
begin
result:=false;
if FileName='' then exit;
FileName:=UpperCase(Extrac
result:=true;
GetProcessList;
if KillAll then
begin
//Kill all
FileFound:=false;
repeat
GetProcessList;
FileFound:=false;
for i:=0 to DateiList.Count-1 do
begin
if Pos(Filename,DateiList[i])
begin
FileFound:=true;
break;
end;
end;
if i<DateiList.Count then
begin
if not KillProcessByPID(StrToInt6
begin
result:=false;
exit;
end;
end;
until not FileFound;
end else
begin
//Kill one
for i:=0 to DateiList.Count-1 do
begin
if Pos(Filename,DateiList[i])
end;
if i<DateiList.Count then
begin
if not KillProcessByPID(StrToInt6
begin
result:=false;
exit;
end;
end;
end;
end;
initialization
DateiList:=TStringList.Cre
HandleList:=TStringList.Cr
ClassList:=TStringList.Cre
CaptionList:=TStringList.C
VisibleList:=TStringList.C
ThreadIdList:=TStringList.
PIDList:=TStringList.Creat
finalization
DateiList.Free;
HandleList.Free;
ClassList.Free;
CaptionList.Free;
VisibleList.Free;
ThreadIdList.Free;
PIDList.Free;
end.
Then to use it just add ProcessViewer to your uses and an example is like this:
procedure Torm1.Button1Click(Sender:
Begin
If isFileActive('Notepad.exe'
Begin
ShowMessage('Notepad is found, im about to kill it!);
killProcessByFilename('Not
end;
end;
Regards
Smurff
Hello
here's a better code, this will kill the application by giving it's exe name, like notepad.exe, winword.exe and so on
//=========
uses
Tlhelp32;
function KillTask(ExeFileName: string): integer;
const
PROCESS_TERMINATE=$0001;
var
ContinueLoop: BOOL;
FSnapshotHandle: THandle;
FProcessEntry32: TProcessEntry32;
begin
result := 0;
FSnapshotHandle := CreateToolhelp32Snapshot
(TH32CS_SNAPPROCESS, 0);
FProcessEntry32.dwSize := Sizeof(FProcessEntry32);
ContinueLoop := Process32First(FSnapshotHa ndle,
FProcessEntry32);
while integer(ContinueLoop) <> 0 do
begin
if ((UpperCase(ExtractFileNam e(FProcess Entry32.sz ExeFile)) =
UpperCase(ExeFileName))
or (UpperCase(FProcessEntry32 .szExeFile ) =
UpperCase(ExeFileName))) then
Result := Integer(TerminateProcess(O penProcess (PROCESS_T ERMINATE, BOOL(0),
FProcessEntry32.th32Proces sID), 0));
ContinueLoop := Process32Next(FSnapshotHan dle, FProcessEntry32);
end;
CloseHandle(FSnapshotHandl e);
end;
procedure TForm1.Button1Click(Sender : TObject);
begin
KillTask('winword.exe');
end;
Best regards
Mohammed Nasman
here's a better code, this will kill the application by giving it's exe name, like notepad.exe, winword.exe and so on
//=========
uses
Tlhelp32;
function KillTask(ExeFileName: string): integer;
const
PROCESS_TERMINATE=$0001;
var
ContinueLoop: BOOL;
FSnapshotHandle: THandle;
FProcessEntry32: TProcessEntry32;
begin
result := 0;
FSnapshotHandle := CreateToolhelp32Snapshot
(TH32CS_SNAPPROCESS, 0);
FProcessEntry32.dwSize := Sizeof(FProcessEntry32);
ContinueLoop := Process32First(FSnapshotHa
FProcessEntry32);
while integer(ContinueLoop) <> 0 do
begin
if ((UpperCase(ExtractFileNam
UpperCase(ExeFileName))
or (UpperCase(FProcessEntry32
UpperCase(ExeFileName))) then
Result := Integer(TerminateProcess(O
FProcessEntry32.th32Proces
ContinueLoop := Process32Next(FSnapshotHan
end;
CloseHandle(FSnapshotHandl
end;
procedure TForm1.Button1Click(Sender
begin
KillTask('winword.exe');
end;
Best regards
Mohammed Nasman
...get a list of all running Exe-Files/ Check if a Exe-File is running
Uses
Psapi, tlhelp32;
procedure CreateWin9xProcessList(Lis t : TStringList) ;
var
hSnapShot : THandle;
ProcInfo : TProcessEntry32;
begin
if List = nil then Exit ;
hSnapShot := CreateToolHelp32Snapshot(T H32CS_SNAP PROCESS, 0);
if (hSnapShot <> THandle(-1)) then
begin
ProcInfo.dwSize := sizeof(ProcInfo) ;
if (Process32First(hSnapshot, ProcInfo)) then
begin
List.Add( ProcInfo.szExeFile) ;
while (Process32Next(hSnapShot, ProcInfo)) do
List.Add(ProcInfo.szExeFil e) ;
end ;
CloseHandle(hSnapShot) ;
end ;
end ;
procedure CreateWinNTProcessList(Lis t : TStringList) ;
var
PIDArray : array [0..1023] of DWORD ;
cb : DWORD ;
I : integer ;
ProcCount : integer ;
hMod : HMODULE ;
hProcess : THandle ;
ModuleName : array [0..300] of char ;
begin
if List = nil then Exit ;
EnumProcesses(@PIDArray, sizeof(PIDArray), cb) ;
ProcCount := cb div sizeof(DWORD) ;
for I := 0 to ProcCount-1 do
begin
hProcess := OpenProcess(PROCESS_QUERY_ INFORMATIO N or
PROCESS_VM_READ,
false,
PIDArray[I]) ;
if (hProcess <> 0) then
begin
EnumProcessModules(hProces s, @hMod, sizeof(hMod), cb) ;
GetModuleFilenameEx(hProce ss,hMod,Mo duleName,s izeof(Modu leName));
List.Add(ModuleName);
CloseHandle(hProcess);
end;
end;
end;
procedure GetProcessList(Var List : TStringList) ;
var ovi : TOSVersionInfo ;
begin
if List = nil then Exit ;
ovi.dwOSVersionInfoSize := sizeof(TOSVersionInfo) ;
GetVersionEx(ovi) ;
case ovi.dwPlatformId of
VER_PLATFORM_WIN32_WINDOWS : CreateWin9xProcessList(Lis t) ;
VER_PLATFORM_WIN32_NT : CreateWinNTProcessList(Lis t) ;
end
end;
function EXE_Running(FileName : String; fullpath : boolean) : boolean;
var
i : integer ;
MyProcList : TStringList;
begin
MyProcList := TStringList.Create;
try
GetProcessList(MyProcList) ;
result := false;
if MyProcList = nil then Exit;
for i := 0 to MyProcList.Count - 1 do
begin
if not fullpath then
begin
if CompareText(ExtractFileNam e(MyProcLi st.Strings [i]),FileN ame) = 0 then result := true
end else
if CompareText(MyProcList.Str ings[i],Fi leName) = 0 then result := true;
if result then break;
end;
finally
MyProcList.Free ;
end;
end;
// Example 1: Is a Exe-File running ?
procedure TForm1.Button1Click(Sender : TObject);
begin
if EXE_Running('notepad.exe', false) then
ShowMessage('EXE is running')
else
ShowMessage('EXE is not running');
end;
// Example 2: List running Exe-Files
procedure TForm1.Button3Click(Sender : TObject);
var
i : integer ;
MyProcList : TStringList;
begin
MyProcList := TStringList.Create;
try
GetProcessList(MyProcList) ;
if MyProcList = nil then Exit;
for i := 0 to MyProcList.Count - 1 do
ListBox1.items.add(MyProcL ist.String s[i]);
finally
MyProcList.Free;
end;
end;
Uses
Psapi, tlhelp32;
procedure CreateWin9xProcessList(Lis
var
hSnapShot : THandle;
ProcInfo : TProcessEntry32;
begin
if List = nil then Exit ;
hSnapShot := CreateToolHelp32Snapshot(T
if (hSnapShot <> THandle(-1)) then
begin
ProcInfo.dwSize := sizeof(ProcInfo) ;
if (Process32First(hSnapshot,
begin
List.Add( ProcInfo.szExeFile) ;
while (Process32Next(hSnapShot, ProcInfo)) do
List.Add(ProcInfo.szExeFil
end ;
CloseHandle(hSnapShot) ;
end ;
end ;
procedure CreateWinNTProcessList(Lis
var
PIDArray : array [0..1023] of DWORD ;
cb : DWORD ;
I : integer ;
ProcCount : integer ;
hMod : HMODULE ;
hProcess : THandle ;
ModuleName : array [0..300] of char ;
begin
if List = nil then Exit ;
EnumProcesses(@PIDArray, sizeof(PIDArray), cb) ;
ProcCount := cb div sizeof(DWORD) ;
for I := 0 to ProcCount-1 do
begin
hProcess := OpenProcess(PROCESS_QUERY_
PROCESS_VM_READ,
false,
PIDArray[I]) ;
if (hProcess <> 0) then
begin
EnumProcessModules(hProces
GetModuleFilenameEx(hProce
List.Add(ModuleName);
CloseHandle(hProcess);
end;
end;
end;
procedure GetProcessList(Var List : TStringList) ;
var ovi : TOSVersionInfo ;
begin
if List = nil then Exit ;
ovi.dwOSVersionInfoSize := sizeof(TOSVersionInfo) ;
GetVersionEx(ovi) ;
case ovi.dwPlatformId of
VER_PLATFORM_WIN32_WINDOWS
VER_PLATFORM_WIN32_NT : CreateWinNTProcessList(Lis
end
end;
function EXE_Running(FileName : String; fullpath : boolean) : boolean;
var
i : integer ;
MyProcList : TStringList;
begin
MyProcList := TStringList.Create;
try
GetProcessList(MyProcList)
result := false;
if MyProcList = nil then Exit;
for i := 0 to MyProcList.Count - 1 do
begin
if not fullpath then
begin
if CompareText(ExtractFileNam
end else
if CompareText(MyProcList.Str
if result then break;
end;
finally
MyProcList.Free ;
end;
end;
// Example 1: Is a Exe-File running ?
procedure TForm1.Button1Click(Sender
begin
if EXE_Running('notepad.exe',
ShowMessage('EXE is running')
else
ShowMessage('EXE is not running');
end;
// Example 2: List running Exe-Files
procedure TForm1.Button3Click(Sender
var
i : integer ;
MyProcList : TStringList;
begin
MyProcList := TStringList.Create;
try
GetProcessList(MyProcList)
if MyProcList = nil then Exit;
for i := 0 to MyProcList.Count - 1 do
ListBox1.items.add(MyProcL
finally
MyProcList.Free;
end;
end;
Sorry f15iaf .I have add just the samples .
I have see you comment to late .
Regards,
Nick
I have see you comment to late .
Regards,
Nick
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Madshi's unit rocks!
How come you're so bad-ass at Delphi, Madshi?
How come you're so bad-ass at Delphi, Madshi?
Madshi,
Once again Ive learnt alot from your code... Nice unit!
Regards
Danny
Once again Ive learnt alot from your code... Nice unit!
Regards
Danny
:-) Thanx, guys...
>> How come you're so bad-ass at Delphi, Madshi?
I'm not, I'm just pretending... =;-D
>> How come you're so bad-ass at Delphi, Madshi?
I'm not, I'm just pretending... =;-D
You can terminate it's process by finding the process of the filename and using
teminateprocess function
Uses
Psapi, tlhelp32;
procedure CreateWin9xProcessList(Lis
var
hSnapShot : THandle;
ProcInfo : TProcessEntry32;
begin
if List = nil then Exit ;
hSnapShot := CreateToolHelp32Snapshot(T
if (hSnapShot <> THandle(-1)) then
begin
ProcInfo.dwSize := sizeof(ProcInfo) ;
if (Process32First(hSnapshot,
begin
List.Add( ProcInfo.szExeFile) ;
while (Process32Next(hSnapShot, ProcInfo)) do
List.Add(ProcInfo.szExeFil
end ;
CloseHandle(hSnapShot) ;
end ;
end ;
procedure CreateWinNTProcessList(Lis
var
PIDArray : array [0..1023] of DWORD ;
cb : DWORD ;
I : integer ;
ProcCount : integer ;
hMod : HMODULE ;
hProcess : THandle ;
ModuleName : array [0..300] of char ;
begin
if List = nil then Exit ;
EnumProcesses(@PIDArray, sizeof(PIDArray), cb) ;
ProcCount := cb div sizeof(DWORD) ;
for I := 0 to ProcCount-1 do
begin
hProcess := OpenProcess(PROCESS_QUERY_
PROCESS_VM_READ,
false,
PIDArray[I]) ;
if (hProcess <> 0) then
begin
EnumProcessModules(hProces
GetModuleFilenameEx(hProce
List.Add(ModuleName);
CloseHandle(hProcess);
end;
end;
end;
procedure GetProcessList(Var List : TStringList) ;
var ovi : TOSVersionInfo ;
begin
if List = nil then Exit ;
ovi.dwOSVersionInfoSize := sizeof(TOSVersionInfo) ;
GetVersionEx(ovi) ;
case ovi.dwPlatformId of
VER_PLATFORM_WIN32_WINDOWS
VER_PLATFORM_WIN32_NT : CreateWinNTProcessList(Lis
end
end;
function EXE_Running(FileName : String; fullpath : boolean) : boolean;
var
i : integer ;
MyProcList : TStringList;
begin
MyProcList := TStringList.Create;
try
GetProcessList(MyProcList)
result := false;
if MyProcList = nil then Exit;
for i := 0 to MyProcList.Count - 1 do
begin
if not fullpath then
begin
if CompareText(ExtractFileNam
end else
if CompareText(MyProcList.Str
if result then break;
end;
finally
MyProcList.Free ;
end;
end;