We help IT Professionals succeed at work.

How can I know if an application is running ?

frdep
frdep asked
on
Medium Priority
163 Views
Last Modified: 2010-04-06
How can I know if an application is running ?
ie is MSWord is running, or Excel, or NotePad, etc.
I'm using Delphi 6 and Windows NT.
Thanks
Comment
Watch Question

Commented:
...get a list of all running Exe-Files/ Check if a Exe-File is running

Uses
 Psapi, tlhelp32;

procedure CreateWin9xProcessList(List : TStringList) ;
var
 hSnapShot : THandle;
 ProcInfo : TProcessEntry32;
begin
 if List = nil then Exit ;
 hSnapShot := CreateToolHelp32Snapshot(TH32CS_SNAPPROCESS, 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.szExeFile) ;
   end ;
   CloseHandle(hSnapShot) ;
 end ;
end ;

procedure CreateWinNTProcessList(List : 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_INFORMATION or
   PROCESS_VM_READ,
   false,
   PIDArray[I]) ;
   if (hProcess <> 0) then
   begin
     EnumProcessModules(hProcess, @hMod, sizeof(hMod), cb) ;
     GetModuleFilenameEx(hProcess,hMod,ModuleName,sizeof(ModuleName));
     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(List) ;
   VER_PLATFORM_WIN32_NT : CreateWinNTProcessList(List) ;
 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(ExtractFileName(MyProcList.Strings[i]),FileName) = 0 then result := true
     end else
      if CompareText(MyProcList.Strings[i],FileName) = 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(MyProcList.Strings[i]);
finally
  MyProcList.Free;
end;
end;

Commented:
...get a list of all running Exe-Files/ Check if a Exe-File is running

Uses
 Psapi, tlhelp32;

procedure CreateWin9xProcessList(List : TStringList) ;
var
 hSnapShot : THandle;
 ProcInfo : TProcessEntry32;
begin
 if List = nil then Exit ;
 hSnapShot := CreateToolHelp32Snapshot(TH32CS_SNAPPROCESS, 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.szExeFile) ;
   end ;
   CloseHandle(hSnapShot) ;
 end ;
end ;

procedure CreateWinNTProcessList(List : 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_INFORMATION or
   PROCESS_VM_READ,
   false,
   PIDArray[I]) ;
   if (hProcess <> 0) then
   begin
     EnumProcessModules(hProcess, @hMod, sizeof(hMod), cb) ;
     GetModuleFilenameEx(hProcess,hMod,ModuleName,sizeof(ModuleName));
     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(List) ;
   VER_PLATFORM_WIN32_NT : CreateWinNTProcessList(List) ;
 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(ExtractFileName(MyProcList.Strings[i]),FileName) = 0 then result := true
     end else
      if CompareText(MyProcList.Strings[i],FileName) = 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(MyProcList.Strings[i]);
finally
  MyProcList.Free;
end;
end;

Commented:
Sorry ...isn't me :(

...

Or can use a Madshi stuff :

http://www.madshi.net/enumStuff.zip

Commented:
Listening...

Commented:
:-)  Thanx ginsonic, for linking to me... The code posted by ginsonic should work fine. There's one little thing to mention: In NT/2k/XP it does need Microsoft's psApi.dll, which is not included in every NT installation. So you need to distribute this dll with your application. In contrast my code (see ginsonic's link) works fine without needing psApi.dll.

Both codes are looking for running exe file names. If you now the main window class, you can also use FindWindow. That is much easier, if that is good enough for you...

Regards, Madshi.

Explore More ContentExplore courses, solutions, and other research materials related to this topic.