Delphi,Windows:确定web浏览器是否正在运行的最佳方法?

Delphi,Windows:确定web浏览器是否正在运行的最佳方法?,windows,delphi,process,Windows,Delphi,Process,确定web浏览器是否正在运行的最佳方法是什么 使用Delphi XE2和Windows,我需要确定以下web浏览器当前是否正在运行: A) Mozilla Firefox B) 苹果野生动物园 C) 谷歌浏览器 如果找到,该过程将终止,因为需要通过修改web浏览器配置文件以编程方式更改web浏览器的主页(这是不可能的,或者如果在web浏览器运行时这样做,可能会导致不可预测的结果) EnumWindows API函数的输出是否包含处理上述任务所需的足够信息?如果是,那么是否在任何地方记录了上述每个

确定web浏览器是否正在运行的最佳方法是什么

使用Delphi XE2和Windows,我需要确定以下web浏览器当前是否正在运行:

A) Mozilla Firefox B) 苹果野生动物园 C) 谷歌浏览器

如果找到,该过程将终止,因为需要通过修改web浏览器配置文件以编程方式更改web浏览器的主页(这是不可能的,或者如果在web浏览器运行时这样做,可能会导致不可预测的结果)

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


TIA。

未经用户许可终止进程不是好做法,相反,您必须询问用户是否希望终止应用程序(在本例中为web浏览器)

现在回到您的问题,您可以使用该方法检测应用程序(webbroser)是否正在运行进程名称检查(firefox.exe、chrome.exe、safari.exe)

现在,如果需要更可靠的方法,可以搜索窗口的类名(使用方法),然后搜索句柄的进程所有者的PID(使用),从这里可以使用进程的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.
{$APPTYPE控制台}
使用
窗户,
TL32,
SysUtils;
函数GetProcessName(const th32ProcessID:DWORD):字符串;
变量
hSnapshot:THandle;
lppe:tprocesentry32;
开始
结果:='';
hSnapshot:=CreateToolhelp32Snapshot(TH32CS\u SNAPPROCESS,0);
如果hSnapshot的句柄值无效,则
尝试
lppe.dwSize:=SizeOf(lppe);
如果先处理32(hSnapshot,lppe),则
重复
如果lppe.th32ProcessID=th32ProcessID,则
退出(lppe.szExeFile);
直到下一步(hSnapshot、lppe);
最后
闭合手柄(hSnapshot);
结束;
结束;
函数IsWebBrowserRunning(const ClassName,ExeName:string):布尔;
变量
hWindow:THandle;
dwProcessId:DWORD;
开始
结果:=假;
hWindow:=FindWindowEx(0,0,PChar(ClassName),nil);
如果hWindow0那么
开始
dwProcessId:=0;
GetWindowThreadProcessId(hWindow、dwProcessId);
如果dwProcessId>0,则
退出(Sametext(GetProcessName(dwProcessId),ExeName));
结束;
结束;
开始
尝试
如果IsWebBrowserRunning('MozillaWindowClass','firefox.exe'),那么
Writeln(“Firefox正在运行”);
如果IsWebBrowserRunning({1C03B488-D53B-4a81-97F8-754559640193},'safari.exe'),则
Writeln(“Safari正在运行”);
如果IsWebBrowserRunning('Chrome\u WidgetWin\u 1','Chrome.exe'),则
Writeln(“Chrome正在运行”);
除了
关于E:Exception-do
Writeln(E.ClassName,“:”,E.Message);
结束;
readln;
结束。

在没有用户许可的情况下终止进程不是一种好做法,相反,您必须询问用户是否希望终止应用程序(在本例中为web浏览器)

现在回到您的问题,您可以使用该方法检测应用程序(webbroser)是否正在运行进程名称检查(firefox.exe、chrome.exe、safari.exe)

现在,如果需要更可靠的方法,可以搜索窗口的类名(使用方法),然后搜索句柄的进程所有者的PID(使用),从这里可以使用进程的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.
{$APPTYPE控制台}
使用
窗户,
TL32,
SysUtils;
函数GetProcessName(const th32ProcessID:DWORD):字符串;
变量
hSnapshot:THandle;
lppe:tprocesentry32;
开始
结果:='';
hSnapshot:=CreateToolhelp32Snapshot(TH32CS\u SNAPPROCESS,0);
如果hSnapshot的句柄值无效,则
尝试
lppe.dwSize:=SizeOf(lppe);
如果先处理32(hSnapshot,lppe),则
重复
如果lppe.th32ProcessID=th32ProcessID,则
退出(lppe.szExeFile);
直到下一步(hSnapshot、lppe);
最后
闭合手柄(hSnapshot);
结束;
结束;
函数IsWebBrowserRunning(const ClassName,ExeName:string):布尔;
变量
hWindow:THandle;
dwProcessId:DWORD;
开始
结果:=假;
hWindow:=FindWindowEx(0,0,PChar(ClassName),nil);
如果hWindow0那么
开始
dwProcessId:=0;
GetWindowThreadProcessId(hWindow、dwProcessId);
如果dwProcessId>0,则
退出(Sametext(GetProcessName(dwProcessId),ExeName));
结束;
结束;
开始
尝试
如果IsWebBrowserRunning('MozillaWindowClass','firefox.exe'),那么
Writeln(“Firefox正在运行”);
如果IsWebBrowserRunning({1C03B488-D53B-4a81-97F8-754559640193},'safari.exe'),则
Writeln(“Safari正在运行”);
如果IsWebBrowserRunning('Chrome\u WidgetWin\u 1','Chrome.exe'),则
Writeln(“Chrome正在运行”);
除了
关于E:Exception-do
Writeln(E.ClassName,“:”,E.Message);
结束;
readln;
结束。

如果某些软件更改了我的浏览器主页,我会非常生气。如果@Antonio同意,我也会怀疑我的电脑感染了病毒/恶意软件,因为通常更改主页是由病毒/恶意软件完成的。如果我正在使用我的浏览器,并且它突然被其他程序(如您的程序)终止,我会很不高兴。如果某些软件更改了我的浏览器主页,我会非常生气同意@Antonio,我也会怀疑我的计算机感染了病毒/恶意软件,因为通常更改主页是由病毒/恶意软件完成的。如果我正在使用我的浏览器,并且它突然被其他程序(如您的程序)终止,我也不会高兴。
{$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.