【问题标题】:How do I detect if the specific Delphi IDE is running?如何检测特定的 Delphi IDE 是否正在运行?
【发布时间】:2012-02-04 16:56:24
【问题描述】:

我正在开发一个组件安装程序(仅适用于 Delphi XE2),我想检测 Delphi XE2 IDE 是否正在运行。您将如何检测它?

附:我知道 TAppBuilder 窗口类名,但我还需要检测 IDE 版本。

【问题讨论】:

  • 如果你能找到主窗口的窗口句柄你可以使用GetWindowThreadProcessId来获取进程id。然后调用 OpenProcess 获取进程句柄。然后调用 GetModuleFileNameEx 来获取 exe 文件名。然后使用 GetFileVersionInfo 等读取 exe 文件的版本资源。呸!
  • @DavidHeffernan: :-) 深呼吸,一次又一次。那里应该感觉更好。
  • 我希望上面的方法可以完成这项工作,但如果有人能找到更简单的方法,我一点也不感到惊讶。

标签: delphi ide delphi-xe2 version-detection


【解决方案1】:

这些是确定 Delphi XE2 是否正在运行的步骤

1) 首先从\Software\Embarcadero\BDS\9.0 注册表键中的App 条目中读取bds.exe 文件的位置,该注册表键可以位于HKEY_CURRENT_USER 或HKEY_LOCAL_MACHINE 根键中。 p>

2) 然后使用CreateToolhelp32Snapshot 函数可以检查是否存在同名的exe 正在运行。

3) 最后使用最后处理的条目的 PID,您可以解析 Exe 的完整文件路径(使用 GetModuleFileNameEx 函数),然后再次比较名称。

查看此示例代码

{$APPTYPE CONSOLE}

{$R *.res}

uses

  Registry,
  PsAPI,
  TlHelp32,
  Windows,
  SysUtils;

function ProcessFileName(dwProcessId: DWORD): string;
var
  hModule: Cardinal;
begin
  Result := '';
  hModule := OpenProcess(PROCESS_QUERY_INFORMATION or PROCESS_VM_READ, False, dwProcessId);
  if hModule <> 0 then
    try
      SetLength(Result, MAX_PATH);
      if GetModuleFileNameEx(hModule, 0, PChar(Result), MAX_PATH) > 0 then
        SetLength(Result, StrLen(PChar(Result)))
      else
        Result := '';
    finally
      CloseHandle(hModule);
    end;
end;

function IsAppRunning(const FileName: string): boolean;
var
  hSnapshot      : Cardinal;
  EntryParentProc: TProcessEntry32;
begin
  Result := False;
  hSnapshot := CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
  if hSnapshot = INVALID_HANDLE_VALUE then
    exit;
  try
    EntryParentProc.dwSize := SizeOf(EntryParentProc);
    if Process32First(hSnapshot, EntryParentProc) then
      repeat
        if CompareText(ExtractFileName(FileName), EntryParentProc.szExeFile) = 0 then
          if CompareText(ProcessFileName(EntryParentProc.th32ProcessID),  FileName) = 0 then
          begin
            Result := True;
            break;
          end;
      until not Process32Next(hSnapshot, EntryParentProc);
  finally
    CloseHandle(hSnapshot);
  end;
end;

function RegReadStr(const RegPath, RegValue: string; var Str: string;
  const RootKey: HKEY): boolean;
var
  Reg: TRegistry;
begin
  try
    Reg := TRegistry.Create;
    try
      Reg.RootKey := RootKey;
      Result      := Reg.OpenKey(RegPath, True);
      if Result then
        Str := Reg.ReadString(RegValue);
    finally
      Reg.Free;
    end;
  except
    Result := False;
  end;
end;

function RegKeyExists(const RegPath: string; const RootKey: HKEY): boolean;
var
  Reg: TRegistry;
begin
  try
    Reg := TRegistry.Create;
    try
      Reg.RootKey := RootKey;
      Result      := Reg.KeyExists(RegPath);
    finally
      Reg.Free;
    end;
  except
    Result := False;
  end;
end;


function GetDelphiXE2LocationExeName: string;
Const
 Key = '\Software\Embarcadero\BDS\9.0';
begin
  Result:='';
    if RegKeyExists(Key, HKEY_CURRENT_USER) then
    begin
      RegReadStr(Key, 'App', Result, HKEY_CURRENT_USER);
      exit;
    end;

    if RegKeyExists(Key, HKEY_LOCAL_MACHINE) then
      RegReadStr(Key, 'App', Result, HKEY_LOCAL_MACHINE);
end;


Var
 Bds : String;

begin
  try
     Bds:=GetDelphiXE2LocationExeName;
     if Bds<>'' then
     begin
       if  IsAppRunning(Bds) then
        Writeln('The Delphi XE2 IDE Is running')
       else
        Writeln('The Delphi XE2 IDE Is not running')
     end
     else
     Writeln('The Delphi XE2 IDE Is was not found');
  except
    on E: Exception do
      Writeln(E.ClassName, ': ', E.Message);
  end;
  Readln;
end.

其他资源。 Detecting installed delphi versions

【讨论】:

  • 对我来说,在 win 10 x64 中,我认为在以下 reg 路径上检查它更安全:“\HKEY_LOCAL_MACHINE\SOFTWARE\WOW6432Node\Embarcadero\BDS\xx.xx”是 delphi 的数量版本,例如:“\HKEY_LOCAL_MACHINE\SOFTWARE\WOW6432Node\Embarcadero\BDS\20.0”,如果是 Rio 版本。
【解决方案2】:

检查 DebugHook 0。 不利的一面是,目前如果您的应用程序是使用包构建的,DebugHook 将返回 0。 但通常这将是一个非常优雅和简单的测试。在 D2009 中运行良好,我只是注意到它在 XE2 中存在包依赖错误 (http://qc.embarcadero.com/wc/qcmain.aspx?d=105365)。

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-10-28
相关资源
最近更新 更多