delphi - 如何检测特定的 Delphi IDE 是否正在运行?

标签 delphi ide delphi-xe2 version-detection

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

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

最佳答案

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

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

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

关于delphi - 如何检测特定的 Delphi IDE 是否正在运行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9142590/

相关文章:

delphi - 当防火墙拒绝互联网访问时,ShellExecute 会阻止(永不返回)

java - 隐藏功能 IntelliJ IDEA

Android Studio 无法在 OSX 上通过 ADB 连接

delphi - 为什么 SetString 内在函数可能会导致 PChar 参数出现 "Incompatible types"错误?

delphi - 如何在不将扫描图像保存到磁盘的情况下将扫描图像从 WIA ImageFile 传输到 TBitmap?

delphi - 为什么包含过程时 Delphi 记录的大小没有增加?

delphi - 如何滚动 TFlowPanel 的内容?

multithreading - Delphi 中增加和返回整数的线程安全方法

c# - 如何在我的程序的开始菜单中创建一个菜单?

ruby-on-rails - 使用自动完成功能编写功能的 Cucumber IDE?