1

I have a unit which has a resourcestring in its implementation section. How can I get the resourcestring's identifier in another unit?

unit Unit2;

interface

implementation

resourcestring
  SampleStr = 'Sample';

end.

If it is available in the interface section, I can write this:

PResStringRec(@SampleStr).Identifier
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Denis Sletkov
  • 221
  • 1
  • 4
  • [According to Allen Bauer](http://stackoverflow.com/questions/30390079/how-resourcestring-identifiers-are-generated-by-delphi-complier): "The compiler generates an identifier for each resource string based on the unit-name and the resource string identifier, so that is always stable even if the value changes." So you might try to determine `PResStringRec(@SampleStr).Identifier` at runtime and use the determined value as constant (if you really cannot modify `Unit2` at all.) – yonojoy Jun 16 '16 at 08:06

1 Answers1

4

Anything declared in a unit's implementation section is private to the unit. It CANNOT be accessed directly from another unit. So, you will have to either:

  1. move the resourcestring to the interface section:

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

    uses
      Unit2;
    
    ID := PResStringRec(@Unit2.SampleStr).Identifier;
    
  2. leave the resourcestring in the implementation section, and declare a function in the interface section to return the identifier:

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

    uses
      Unit2;
    
    ID := GetSampleStrResID;
    
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • All there methods require modification of Unit2, that is not desirable. Now i moved a resourcestring to interface section, but resourcestring are stored in file resources and i can get it by identifier (without move to interface section). So i want to find (maybe a hack) method to get these identifier. – Denis Sletkov Mar 19 '15 at 08:53
  • The identifier doesn't exist at run time - it's a compile time symbol. The mapping between the identifier and the resource ID is in the .drc file (which is generated by the compiler). – SpeedFreak Mar 19 '15 at 13:57