是否可以找出项目使用条款中单元的位置?
我的项目(.exe 或 .dll)中有两个单元(SuperPuper.pas 和 SuperPuper777.pas)
是否可以在运行时从 SuperPuper777.pas
中的代码中找出
< code>SuperPuper.pas 列在项目的
uses
子句中;SuperPuper.pas
是项目uses
子句中的第一个单元。
该问题经过大量编辑。我想它的实际目的是找出 ShareMem.pas
单元是否在项目的 uses
子句中声明在正确的位置。
I have two units (SuperPuper.pas and SuperPuper777.pas) in a project (.exe or .dll)
Is it possible to find out at runtime from my code in SuperPuper777.pas
that
SuperPuper.pas
is listed in project'suses
clause;SuperPuper.pas
is first unit in project'suses
clause.
The question was heavily edited. I guess that it's practical purpose is to find out if ShareMem.pas
unit was declared in right position in project's uses
clause.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您想在项目的 uses 子句中强制执行单元的正确声明,我将添加一个预构建事件来运行基于正则表达式的 Perl/Python/Ruby 脚本。该脚本将对 .dpr 文件进行基于正则表达式的简单检查,如果不符合预期则返回错误。虽然不是万无一失,但可能是少量努力的效用的最佳平衡。
我意识到您的问题要求运行时检测,但这是编译时属性,因此最好在编译时进行攻击。
If you want to enforce the correct declaration of a unit in a project's
uses
clause I would add a pre-build event to run a regex based Perl/Python/Ruby script. The script would do a simple regex based check of the .dpr file and return an error if it was not as intended. Not fool-proof, but probably the best balance of utility for a small amount of effort.I realise that your question asks for runtime detection but this is a compile time property and so best attacked at compile time.
您可以从资源中获取链接到可执行文件(即运行时)的所有单元的列表。有一个名为
PACKAGEINFO
的资源,其中包含所有单元的列表。您可以从这里找到一些反向信息。也许您可以从增强型 RTTI(自 Delphi 2010 起可用)中获取此信息。关于如何检测某个单元是否位于 .dpr 使用子句中的第一个单元,我没有看到任何在运行时轻松执行此操作的方法。
PACKAGEINFO
中的列表不按此顺序排列。您可以在编译时通过解析 .dpr 内容并检查其使用子句来完成此操作。我认为猜测首先设置哪个单元的唯一方法是在公共单元中使用全局变量:
然后在每个单元的初始化部分:
然后检查
LatestUnitSet
以查看哪个单元被初始化最新的。You can get a list of all units linked to the executable (i.e. at runtime) from the resources. There is a resource named
PACKAGEINFO
which contains a list of all units. You can find some reverse information from here. Perhaps you can get this information from enhanced RTTI (available since Delphi 2010).About how to detect that an unit is first in the .dpr uses clause, I do not see any way of doing it at runtime easily. The list in
PACKAGEINFO
is not in this order. You can do that at compile time, by parsing the .dpr content and checking its uses clause.The only way I see to guess which unit was first set is to use a global variable in a common unit:
Then in the initialization section of each unit:
Then check for
LatestUnitSet
to see which one was initialized the latest.