如何确定一个单元是否被编译成Delphi程序? [英] How do I determine if a unit has been compiled into a Delphi program?

查看:148
本文介绍了如何确定一个单元是否被编译成Delphi程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要确定一个特定单元是否被编译成Delphi程序,例如单位SomeUnitName是我的一些程序的一部分,但不是其他程序的一部分。我想要一个函数

I want to be able to determine if a particular unit has been compiled into a Delphi program, e.g. the unit SomeUnitName is part of some of my programs but not of others. I would like to have a function

function IsSomeUnitNameInProgram: boolean;

(这当然不是在SomeUnitName中声明,因为在这种情况下,它总是被包含)运行时返回true,如果单元已经被编译到程序中,那么假如果没有,则返回false。

(which is of course not declared in SomeUnitName because in that case it would always be included) that at runtime returns true, if the unit has been compiled into the program, and false, if not.

我的想法到目前为止已经沿用了jcl调试信息(从一个详细的地图文件编译),我基本上添加到我的所有程序来确定这个信息,但我宁愿,如果jcl不需要。

My thoughts so far have gone along the lines of using the jcl debug information (compiled from a detailed map file) which I basically add to all my programs to determine this information, but I would prefer it, if jcl were not required.

添加代码对于SomeUnitName不是一个选项。

Adding code to SomeUnitName is not an option.

这是目前Delphi 2007,但最好也适用于Delphi XE2。

This is currently for Delphi 2007 but preferably should also work for Delphi XE2.

任何想法?

自@DavidHeffernan以来的一些背景问题:

some background on this since @DavidHeffernan asked:

这不仅仅是一个程序,但是对于100多个不同的程序。他们大多数是在内部使用,但有些也交付给客户。由于我们使用了相当多的图书馆,有些人以各种开源许可证的方式购买了其他的图书,我希望能够在相关框中添加一个学分选项卡,该框只显示实际编译到程序中而不是所有程序库的库。感谢TOndrej的答案,这个工作现在正好像我想要的那样工作:
代码检查一个单元总是链接的,如果一个库被程序使用,如果它在那里,它添加了库名,

This is not only for one program but for more than 100 different ones. Most of them are used internally but some also get delivered to customers. Since we use quite a few libraries, some bought others under various open source licenses, I wanted to be able to add a "credits" tab to the about box which displays only those libraries actually compiled into the program rather than all of them. Thanks to the answer from TOndrej this works now exactly as I wanted it to: The code checks for a unit which is always linked if a library is used by the program and if it is there, it adds the library name, the copyright and a link to it to the about box.

推荐答案

单位名称被编译成PACKAGEINFO资源,您可以在其中查找:

Unit names are compiled into the 'PACKAGEINFO' resource where you can look it up:

uses
  SysUtils;

type
  PUnitInfo = ^TUnitInfo;
  TUnitInfo = record
    UnitName: string;
    Found: PBoolean;
  end;

procedure HasUnitProc(const Name: string; NameType: TNameType; Flags: Byte; Param: Pointer);
begin
  case NameType of
    ntContainsUnit:
      with PUnitInfo(Param)^ do
        if SameText(Name, UnitName) then
          Found^ := True;
  end;
end;

function IsUnitCompiledIn(Module: HMODULE; const UnitName: string): Boolean;
var
  Info: TUnitInfo;
  Flags: Integer;
begin
  Result := False;
  Info.UnitName := UnitName;
  Info.Found := @Result;
  GetPackageInfo(Module, @Info, Flags, HasUnitProc);
end;

为当前可执行文件执行此操作传递 HInstance

To do this for the current executable pass it HInstance:

HasActiveX := IsUnitCompiledIn(HInstance, 'ActiveX');

GetPackageInfo 列举了对于具有多个单元的可执行文件可能无效的所有单元,在这种情况下,您可以剖析实现在SysUtils中编写自己的版本,当找到该单元时停止枚举。)

(GetPackageInfo enumerates all units which may be inefficient for executables with many units, in that case you can dissect the implementation in SysUtils and write your own version which stops enumerating when the unit is found.)

这篇关于如何确定一个单元是否被编译成Delphi程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆