是否可以找出项目使用条款中单元的位置?

发布于 2024-12-23 10:17:19 字数 388 浏览 1 评论 0原文

我的项目(.exe 或 .dll)中有两个单元(SuperPuper.pas 和 SuperPuper777.pas)

是否可以在运行时从 SuperPuper777.pas 中的代码中找出

  1. < code>SuperPuper.pas 列在项目的 uses 子句中;

  2. 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

  1. SuperPuper.pas is listed in project's uses clause;

  2. SuperPuper.pas is first unit in project's uses 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

幼儿园老大 2024-12-30 10:17:19

如果您想在项目的 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.

醉生梦死 2024-12-30 10:17:19

您可以从资源中获取链接到可执行文件(即运行时)的所有单元的列表。有一个名为 PACKAGEINFO 的资源,其中包含所有单元的列表。您可以从这里找到一些反向信息。也许您可以从增强型 RTTI(自 Delphi 2010 起可用)中获取此信息。

关于如何检测某个单元是否位于 .dpr 使用子句中的第一个单元,我没有看到任何在运行时轻松执行此操作的方法。 PACKAGEINFO 中的列表不按此顺序排列。您可以在编译时通过解析 .dpr 内容并检查其使用子句来完成此操作。

我认为猜测首先设置哪个单元的唯一方法是在公共单元中使用全局变量:

 var LatestUnitSet: (oneUnit, anotherUnit);

然后在每个单元的初始化部分:

initialization
  LatestUnitSet := OneUnit;
...


initialization
  LatestUnitSet := anotherUnit;
...

然后检查 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:

 var LatestUnitSet: (oneUnit, anotherUnit);

Then in the initialization section of each unit:

initialization
  LatestUnitSet := OneUnit;
...


initialization
  LatestUnitSet := anotherUnit;
...

Then check for LatestUnitSet to see which one was initialized the latest.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文