Criamos uma aplicação e nela incluímos um TButton, apenas para testar a função.
Vamos agora precisar dar uses em Tlhelp32. Em seguida vamos declarar a function.
type
TForm1 = class(TForm)
Button1: TButton;
private
function ValidarSoftwareAberto(ExeFileName: String): Boolean;
{ Private declarations }
public
{ Public declarations }
end;
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, Tlhelp32;
type
TForm1 = class(TForm)
Button1: TButton;
private
function ValidarSoftwareAberto(ExeFileName: String): Boolean;
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
function TForm1.ValidarSoftwareAberto(ExeFileName: String): Boolean;
begin
end;
end.
function TForm1.ValidarSoftwareAberto(ExeFileName: String): Boolean;
const
PROCESS_TERMINATE = $0001;
var
ContinueLoop: BOOL;
FSnapshotHandle: THandle;
FProcessEntry32: TProcessEntry32;
begin
Result := False;
FSnapshotHandle := CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
FProcessEntry32.dwSize := sizeof(FProcessEntry32);
ContinueLoop := Process32First(FSnapshotHandle, FProcessEntry32);
while Integer(ContinueLoop) <> 0 do
begin
if ((UpperCase(ExtractFileName(FProcessEntry32.szExeFile)) = UpperCase
(ExeFileName)) or (UpperCase(FProcessEntry32.szExeFile) = UpperCase
(ExeFileName))) then
begin
Result := True;
Exit;
end;
ContinueLoop := Process32Next(FSnapshotHandle, FProcessEntry32);
end;
CloseHandle(FSnapshotHandle);
end;
procedure TForm1.Button1Click(Sender: TObject);
Var
xExeFileName: String;
begin
xExeFileName := 'chrome.exe';
if ValidarSoftwareAberto(xExeFileName) then
begin
ShowMessage(xExeFileName + ' esta Aberto !!!');
end
else
begin
ShowMessage(xExeFileName + ' não esta Aberto !!!');
end;
end;
Fico por aqui e até o próximo artigo
Um abraço
Wesley Y
www.lithic.com.br