Foros Club Delphi

Foros Club Delphi (https://www.clubdelphi.com/foros/index.php)
-   Varios (https://www.clubdelphi.com/foros/forumdisplay.php?f=11)
-   -   Detectar si un proceso esta activo. (https://www.clubdelphi.com/foros/showthread.php?t=89061)

deliriun 20-09-2015 17:27:33

Detectar si un proceso esta activo.
 
Hola qué tal?
Quisiera saber si hay la posibilidad de verificar si un proceso esta activo o corriendo...

Por ejemplo si el Bloc de Notas esta activo que de un mensaje.

He buscado otros ejemplos pero ninguno me funciona...

De antemano gracias.

ecfisa 21-09-2015 00:17:33

Hola deliriun.

Proba de este modo,
Código Delphi [-]
uses tlhelp32;

function IsAppRunning(const ExeFileName: string): Boolean;
var
  Entry: PROCESSENTRY32;
  hSnapShot: THandle;
begin
  hSnapShot:= CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
  Entry.dwSize:= SizeOf(PROCESSENTRY32);

  Result:= False;
  if Process32First(hSnapShot, Entry) then
    while Process32Next(hSnapShot, Entry) and not Result do
      if Entry.szExeFile = ExeFileName then
        Result:= True;
  CloseHandle(hSnapSHot);
end;

Ejemplo de uso:
Código Delphi [-]
procedure TForm1.Button1Click(Sender: TObject);
begin
  if IsAppRunning('notepad.exe') then
    ShowMessage('Se está ejecutando')
  else
    ShowMessage('No se está ejecutando');
end;

Saludos :)

escafandra 21-09-2015 02:23:43

Si te interesa saber si un proceso de 16 bits se ejecuta, sigue este hilo en C/C++.

En delphi:
Código Delphi [-]
function VDMEnumTaskWOWEx(dwProcessId: DWORD; fp: pointer; lparam: integer): integer; stdcall; external 'VdmDbg.dll' name 'VDMEnumTaskWOWEx';
function VDMEnumProcessWOW(fp: pointer; lparam: integer): integer; stdcall; external 'VdmDbg.dll' name 'VDMEnumProcessWOW';

type
TRuningProcess16 = record
  Name: Pchar;
  Running: boolean;
end;
PTRuningProcess16 = ^TRuningProcess16;


// Enumera procesos
function ProcessTasks(dwThreadId: DWORD; hMod16, hTask16: WORD; pszModName, pszFileName: PCHAR; PE: integer): boolean; stdcall;
begin
   if AnsiContainsText(pszFileName, PTRuningProcess16(PE).Name) then
   begin
      PTRuningProcess16(PE).Running:= true;
      Result:= true;
      exit;
   end;
   Result:= false;
end;

// Enumera Máquinas Virtuales
function ProcessVDMs(dwProcessId, dwAttrib: DWORD; PE: integer): boolean; stdcall;
begin
   VDMEnumTaskWOWEx(dwProcessId, @ProcessTasks, PE);
   Result:= PTRuningProcess16(PE).Running;
end;

function IsRunning16_(Name: string): boolean;
var
  PE: TRuningProcess16;
begin
  PE.Name:= PCHAR(Name);
  PE.Running:= false;

  // Buscando...
  VDMEnumProcessWOW(@ProcessVDMs, integer(@PE));
  Result:= PE.Running;
end;

Combinar una llamada al código de ecfisa, con otra a sRunning16_:

Código Delphi [-]
IsRunin16_32(ExeFileName: String): BOOL;
begin
  Result:= IsAppRunning(ExeFileName) or IsRunning16_(ExeFileName);
end;

Saludos.

Neftali [Germán.Estévez] 22-09-2015 12:25:54

Se supone que también podrías hacerlo utilizando la función FindWindows (si buscas en el clubdelphpi encontrarás muchos hilos al respecto) o también mediante WMI (link).


La franja horaria es GMT +2. Ahora son las 09:14:31.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Traducción al castellano por el equipo de moderadores del Club Delphi