【问题标题】:Get resourcestring identifier from implementation area从实现区域获取资源字符串标识符
【发布时间】:2015-05-22 00:14:53
【问题描述】:

我有一个单元在其implementation 部分中有一个resourcestring。如何在另一个单元中获取resourcestring 的标识符?

unit Unit2;

interface

implementation

resourcestring
  SampleStr = 'Sample';

end.

如果在interface 部分可用,我可以这样写:

PResStringRec(@SampleStr).Identifier

【问题讨论】:

  • According to Allen Bauer: "编译器根据单元名称和资源字符串标识符为每个资源字符串生成一个标识符,因此即使值发生变化也始终保持稳定。"因此,您可能会尝试在运行时确定PResStringRec(@SampleStr).Identifier,并将确定的值用作常量(如果您真的根本无法修改Unit2。)

标签: delphi internationalization resourcestring


【解决方案1】:

在单元的implementation 部分中声明的任何内容都是该单元的私有不能直接从另一个单元访问。因此,您必须:

  1. resourcestring 移动到interface 部分:

    unit Unit2;
    
    interface
    
    resourcestring
      SampleStr = 'Sample';
    
    implementation
    
    end.
    

    uses
      Unit2;
    
    ID := PResStringRec(@Unit2.SampleStr).Identifier;
    
  2. resourcestring 保留在implementation 部分中,并在interface 部分中声明一个函数以返回标识符:

    unit Unit2;
    
    interface
    
    function GetSampleStrResID: Integer;
    
    implementation
    
    resourcestring
      SampleStr = 'Sample';
    
    function GetSampleStrResID: Integer;
    begin
      Result := PResStringRec(@SampleStr).Identifier;
    end;
    
    end.
    

    uses
      Unit2;
    
    ID := GetSampleStrResID;
    

【讨论】:

  • 所有方法都需要修改 Unit2,这是不可取的。现在我将资源字符串移动到接口部分,但资源字符串存储在文件资源中,我可以通过标识符获取它(无需移动到接口部分)。所以我想找到(也许是破解)方法来获取这些标识符。
  • 该标识符在运行时不存在 - 它是一个编译时符号。标识符和资源ID之间的映射在.drc文件中(由编译器生成)。
猜你喜欢
  • 2012-06-09
  • 2015-09-17
  • 1970-01-01
  • 1970-01-01
  • 2011-05-30
  • 2011-05-24
  • 2014-05-11
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多