2012-07-17 43 views
1

什么是找到一个网络浏览器是否正在运行的最好方法?Delphi,Windows:找到网页浏览器是否正在运行的最佳方式?

用Delphi XE2和Windows,我需要找到是否有以下的网络浏览器正在运行:

A)是Mozilla Firefox B)苹果Safari浏览器 C)谷歌Chrome浏览器

如果找到了,因为需要通过修改Web浏览器的配置文件(这是要么不可能,要么可能会导致不可预测的结果的网页浏览器运行时,如果做的)编程改变了Web浏览器的主页的过程将被终止。

是否从EnumWindows的API函数的输出包含处理上述任务所需的足够的信息?如果是的话,那么上述每个Web浏览器的窗口类名都记录在任何地方?如果不是,那么哪种方法最可靠?

TIA。

+6

好,如果一些软件修改浏览器主页 – 2012-07-17 00:20:48

+2

同意@Antonio我会*非常*生气,我也会怀疑我的电脑被病毒/恶意软件感染,因为通常更改主页被进行病毒/恶意软件。 – Hendra 2012-07-17 03:05:15

+3

我会不快乐,以及如果我与我的浏览器工作,suddently它是由其他程序终止,如你的。 – Hendra 2012-07-17 03:24:50

回答

8

终止未经用户许可的过程是不是好的做法,而是如果他想终止应用程序(在这种情况下,网络浏览器),你必须问给用户。

回到您的问题,您可以使用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. 
+0

+1。 Code Factory的RRUZ再次发起罢工。 :-) – 2012-07-17 20:50:58

+0

+1。我不确定他不是一群程序员:) – Runner 2012-07-18 05:20:35

相关问题