Đây là những bước để xác định xem Delphi XE2 đang chạy
1) Trước tiên đọc vị trí của các tập tin bds.exe từ App
entry trong khóa \Software\Embarcadero\BDS\9.0
registry có thể được đặt tại Phím HKEY_CURRENT_USER hoặc HKEY_LOCAL_MACHINE gốc.
2) Sau đó, sử dụng chức năng CreateToolhelp32Snapshot bạn có thể kiểm tra xem có tồn tại một exe có cùng tên đang chạy hay không.
3) Cuối cùng sử dụng PID của mục nhập được xử lý cuối cùng, bạn có thể giải quyết đường dẫn tệp đầy đủ của Exe (sử dụng hàm GetModuleFileNameEx
) và sau đó so sánh lại tên.
Kiểm tra này mẫu mã
{$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.
nguồn addtional. Detecting installed delphi versions
Nếu bạn có thể tìm thấy tay cầm cửa sổ của cửa sổ chính, bạn có thể sử dụng GetWindowThreadProcessId để lấy id tiến trình. Sau đó, gọi OpenProcess để có được một quy trình xử lý. Sau đó gọi GetModuleFileNameEx để giữ tên tệp exe. Sau đó sử dụng GetFileVersionInfo vv để đọc tài nguyên phiên bản của tệp exe. Phew! –
@DavidHeffernan: :-) Hãy hít thở sâu, một lần nữa, và một lần nữa. Điều đó sẽ cảm thấy tốt hơn. –
Tôi hy vọng ở trên sẽ thực hiện công việc nhưng tôi sẽ không ngạc nhiên nếu ai đó có thể tìm thấy một cách dễ dàng hơn. –