2015-03-19 95 views
0

我有一个单元,其implementation部分中有一个resourcestring。我怎样才能在另一个单位获得resourcestring的标识符?从执行区域获取资源字符串标识符

unit Unit2; 

interface 

implementation 

resourcestring 
    SampleStr = 'Sample'; 

end. 

如果是在interface部分提供,我可以写这样的:在一个单位的implementation部分声明

PResStringRec(@SampleStr).Identifier 
+0

[根据艾伦鲍尔(http://stackoverflow.com/questions/30390079/how-resourcestring-identifiers-are-generated-by-delphi-complier) :“编译器根据单元名称和资源字符串标识符为每个资源字符串生成一个标识符,因此即使值发生更改也始终保持稳定。”所以你可能会尝试在运行时确定'PResStringRec(@SampleStr).Identifier',并使用确定的值作为常量(如果你真的不能修改'Unit2') – yonojoy 2016-06-16 08:06:13

回答

4

一切皆有私人到单位。它不能直接从另一个单位访问。所以,你将不得不采取:

  1. resourcestringinterface部分:

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

    uses 
        Unit2; 
    
    ID := PResStringRec(@Unit2.SampleStr).Identifier; 
    
  2. 离开resourcestringimplementation部分,并声明函数在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; 
    
+0

所有这些方法都需要修改Unit2,这是不可取的。现在我将资源字符串移到了接口部分,但resourcestring存储在文件资源中,我可以通过标识符(不移动到接口部分)来获取它。所以我想找到(也许是黑客)方法来获得这些标识符。 – 2015-03-19 08:53:59

+0

标识符在运行时不存在 - 它是编译时的符号。标识符和资源ID之间的映射位于.drc文件中(由编译器生成)。 – SpeedFreak 2015-03-19 13:57:20