【发布时间】:2009-05-28 17:10:12
【问题描述】:
我听说一些自定义组件作者使用 RTL 例程检查 Delphi 是否正在运行,以设置共享软件限制。有谁知道这个程序是什么?检查诸如“DelphiRunning”或“IsDelphiRunning”之类的明显名称并没有发现任何有用的信息。
【问题讨论】:
标签: delphi
我听说一些自定义组件作者使用 RTL 例程检查 Delphi 是否正在运行,以设置共享软件限制。有谁知道这个程序是什么?检查诸如“DelphiRunning”或“IsDelphiRunning”之类的明显名称并没有发现任何有用的信息。
【问题讨论】:
标签: delphi
这里有 2 种不同的想法:
- Delphi 已启动并运行
- 应用程序在调试器下运行
测试Delphi 是否正在运行 的常用方法是检查是否存在具有特定类名(如 TAppBuilder 或 TPropertyInspector)的已知 IDE Windows。
这两个适用于所有版本的 Delphi IIRC。
如果您想知道您的应用程序是否在调试器下运行,即使用“运行”(F9) 从 IDE 正常启动或在已经运行时附加到调试器,您只需测试 DebugHook 全局变量。
请注意,“从程序中分离”不会删除 DebugHook 值,但“附加到进程”会设置它。
function IsDelphiRunning: Boolean;
begin
Result := (FindWindow('TAppBuilder', nil) > 0) and
(FindWindow('TPropertyInspector', 'Object Inspector') > 0);
end;
function IsOrWasUnderDebugger: Boolean;
begin
Result := DebugHook <> 0;
end;
如果目标是将组件的试用版的使用限制在开发应用程序时,两者都有缺陷:
- 应用程序中可以包含具有正确类名/标题的隐藏窗口
- 可以在代码中手动设置DebugHook
【讨论】:
- DebugHook can be manually set in the code 简而言之,这两个缺陷,有什么解决方法吗?
DebugBreak 或 asm int 3 end 和没有真正的调试器...
您可以在组件代码中使用 DebugHook 0。 DebugHook 是一个由 Delphi/RAD Studio IDE 设置的全局变量(IIRC,它位于系统单元中),无法从其他任何地方设置。
还有其他技术(例如,用于 TAppBuilder 的 FindWindow()),但 DebugHook 可以完成所有工作。
【讨论】:
这是来自www.delphitricks.com/source-code/misc/check_if_delphi_is_running.html的代码sn-p。
function WindowExists(AppWindowName, AppClassName: string): Boolean;
var
hwd: LongWord;
begin
hwd := 0;
hwd := FindWindow(PChar(AppWindowName), PChar(AppClassName));
Result := False;
if not (Hwd = 0) then {window was found if not nil}
Result := True;
end;
function DelphiLoaded: Boolean;
begin
DelphiLoaded := False;
if WindowExists('TPropertyInspector', 'Object Inspector') then
if WindowExists('TMenuBuilder', 'Menu Designer') then
if WindowExists('TAppBuilder', '(AnyName)') then
if WindowExists('TApplication', 'Delphi') then
if WindowExists('TAlignPalette', 'Align') then
DelphiLoaded := True;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
if DelphiLoaded then
begin
ShowMessage('Delphi is running');
end;
end;
function DelphiIsRunning: Boolean;
begin
Result := DebugHook <> 0;
end;
【讨论】: