what uses to use tagPROCESSENTRY32 type? ----- Original Message ----- From: Claudiu Savu To: [email protected] Sent: Saturday, February 28, 2009 8:59 PM Subject: Re: [delphi-en] Process run check
You say you want to know if the app is not responding anymore, here is a code for detection: got it from: http://www.delphi3000.com/articles/article_3295.asp?SK=word // For Win9x/ME function IsAppRespondig9x(dwThreadId: DWORD): Boolean; type TIsHungThread = function(dwThreadId: DWORD): BOOL; stdcall; var hUser32: THandle; IsHungThread: TIsHungThread; begin Result := True; hUser32 := GetModuleHandle('user32.dll'); if (hUser32 > 0) then begin @IsHungThread := GetProcAddress(hUser32, 'IsHungThread'); if Assigned(IsHungThread) then begin Result := not IsHungThread(dwThreadId); end; end; end; // For Win NT/2000/XP function IsAppRespondigNT(wnd: HWND): Boolean; type TIsHungAppWindow = function(wnd:hWnd): BOOL; stdcall; var hUser32: THandle; IsHungAppWindow: TIsHungAppWindow; begin Result := True; hUser32 := GetModuleHandle('user32.dll'); if (hKernel > 0) then begin @IsHungAppWindow := GetProcAddress(hUser32, 'IsHungAppWindow'); if Assigned(IsHungAppWindow) then begin Result := not IsHungAppWindow(wnd); end; end; end; function IsAppRespondig(Wnd: HWND): Boolean; begin if not IsWindow(Wnd) then begin ShowMessage('Incorrect window handle'); Exit; end; if Win32Platform = VER_PLATFORM_WIN32_NT then Result := IsAppRespondigNT(wnd) else Result := IsAppRespondig9X(GetWindowThreadProcessId(wnd,nil)); end; // Example: Check if Word is hung/responing procedure TForm1.Button3Click(Sender: TObject); var Res: DWORD; h: HWND; begin // Find Word by classname h := FindWindow(PChar('OpusApp'), nil); if h <> 0 then begin if IsAppRespondig(h) then ShowMessage('Word is responding') else ShowMessage('Word is not responding'); end else ShowMessage('Word is not open'); end; also this is a function to kill the not responding app: // uses Tlhelp32 function KillTask(ExeFileName: string): integer; var ContinueLoop: BOOL; FSnapshotHandle: THandle; FProcessEntry32: TProcessEntry32; begin result := 0; 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 Result := Integer(TerminateProcess(OpenProcess( PROCESS_TERMINATE, BOOL(0), FProcessEntry32.th32ProcessID), 0)); ContinueLoop := Process32Next(FSnapshotHandle, FProcessEntry32); end; CloseHandle(FSnapshotHandle); end; Cheers, Claudiu ----- Original Message ----- From: "mauro russo" <[email protected]> To: "delphi-en" <[email protected]> Sent: Saturday, February 28, 2009 8:17 PM Subject: [delphi-en] Process run check Dear, how to check if an .exe is in execution? In practice I would like to be sure that, if a my application A1 faults and is closed, it is automatically restarted. I thought to use a secondary very simple deamon application A2 which checks if A1 is in execution and, if not, launches it by ShelExecute. Moreover A1 does the same with A2. Really I would like to use a S.O. resource to make A1 and A2 periodically "speaking" each other. So, if A1 is running, but hanging, A2 can choose to Terminate it and restart it. I believe there is some simplier way to obtain my goal, but I am not expert about windows API. Some help? Regards, Mauro. ---------- Questa email è stata verificata dal sistema centralizzato antivirus della UniPlan Software [Non-text portions of this message have been removed] ------------------------------------ ----------------------------------------------------- Home page: http://groups.yahoo.com/group/delphi-en/ To unsubscribe: [email protected]! Groups Links __________ Information from ESET NOD32 Antivirus, version of virus signature database 3895 (20090227) __________ The message was checked by ESET NOD32 Antivirus. http://www.eset.com ---------- Questa email è stata verificata dal sistema centralizzato antivirus della UniPlan Software [Non-text portions of this message have been removed]

