终止未经用户许可的过程是不是好的做法,而是如果他想终止应用程序(在这种情况下,网络浏览器),你必须问给用户。
回到您的问题,您可以使用CreateToolhelp32Snapshot
方法检测应用程序(webbroser)是否正在运行检查进程名称(firefox.exe,chrome.exe,safari.exe)。
uses
Windows,
tlhelp32,
SysUtils;
function IsProcessRunning(const ListProcess: Array of string): boolean;
var
hSnapshot : THandle;
lppe : TProcessEntry32;
I : Integer;
begin
result:=false;
hSnapshot := CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
if hSnapshot <> INVALID_HANDLE_VALUE then
try
lppe.dwSize := SizeOf(lppe);
if Process32First(hSnapshot, lppe) then
repeat
for I := Low(ListProcess) to High(ListProcess) do
if SameText(lppe.szExeFile, ListProcess[i]) then
Exit(True);
until not Process32Next(hSnapshot, lppe);
finally
CloseHandle(hSnapshot);
end;
end;
,如果你愿意,你可以搜索窗口(使用FindWindowEx
方法)的类名称的更可靠的方式,然后进程所有者的PID使用像这样
IsProcessRunning(['firefox.exe','chrome.exe','safari.exe'])
现在手柄(使用GetWindowThreadProcessId
),从这里你可以使用进程的PID解决exe文件的名称。
{$APPTYPE CONSOLE}
uses
Windows,
tlhelp32,
SysUtils;
function GetProcessName(const th32ProcessID: DWORD): string;
var
hSnapshot : THandle;
lppe : TProcessEntry32;
begin
result:='';
hSnapshot := CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
if hSnapshot <> INVALID_HANDLE_VALUE then
try
lppe.dwSize := SizeOf(lppe);
if Process32First(hSnapshot, lppe) then
repeat
if lppe.th32ProcessID=th32ProcessID then
Exit(lppe.szExeFile);
until not Process32Next(hSnapshot, lppe);
finally
CloseHandle(hSnapshot);
end;
end;
function IsWebBrowserRunning(const ClassName, ExeName :string) : Boolean;
var
hWindow : THandle;
dwProcessId: DWORD;
begin
result:=False;
hWindow:= FindWindowEx(0, 0, PChar(ClassName), nil);
if hWindow<>0 then
begin
dwProcessId:=0;
GetWindowThreadProcessId(hWindow, dwProcessId);
if dwProcessId>0 then
exit(Sametext(GetProcessName(dwProcessId),ExeName));
end;
end;
begin
try
if IsWebBrowserRunning('MozillaWindowClass','firefox.exe') then
Writeln('Firefox is Running');
if IsWebBrowserRunning('{1C03B488-D53B-4a81-97F8-754559640193}','safari.exe') then
Writeln('Safari is Running');
if IsWebBrowserRunning('Chrome_WidgetWin_1','chrome.exe') then
Writeln('Chrome is Running');
except
on E: Exception do
Writeln(E.ClassName, ': ', E.Message);
end;
readln;
end.
好,如果一些软件修改浏览器主页 – 2012-07-17 00:20:48
同意@Antonio我会*非常*生气,我也会怀疑我的电脑被病毒/恶意软件感染,因为通常更改主页被进行病毒/恶意软件。 – Hendra 2012-07-17 03:05:15
我会不快乐,以及如果我与我的浏览器工作,suddently它是由其他程序终止,如你的。 – Hendra 2012-07-17 03:24:50