The situation is:
unit abc;
interface
resourcestring
aabbcc = 'my text here';
implementation
end.
From my application I received error code 'aabbcc'. Is possible to access with error code the resource name aabbcc ?
The situation is:
unit abc;
interface
resourcestring
aabbcc = 'my text here';
implementation
end.
From my application I received error code 'aabbcc'. Is possible to access with error code the resource name aabbcc ?
I imagine you're asking for a function with the following signature and behavior:
Resource strings are stored in your program in a string-table resource, where each string has a numeric index. The named identifier you use in your program is not kept anywhere at run time. Therefore, there is no built-in way to take the text
'aabbcc'
and discover which string resource it's associated with.However, there is a way to take the Delphi
resourcestring
identifier in your code and discover its numeric ID. Type-cast the expression@aabbcc
toPResStringRec
, and then read itsIdentifier
field. Look atLoadResString
in System.pas to see how the RTL uses this information.You could use the
PResStringRec
values to build aTDictionary<string, PResStringRec>
at run time, and then use that dictionary in implementing the hypotheticalGetNamedResourceString
function outlined above.If I were doing this for a real project, I would probably use a pre-build script to parse an input file to automatically generate the calls to
NamedResources.Add
from the correspondingresourcestring
declarations.Your
resourcestring
is compiled into a string table resource. These are identified by a numeric identifier. The compiler maintains a map between your declaredresourcestring
instances, and the numeric identifiers. When you access aresourcestring
the compiler knows the numeric identifier, and emits code that uses that identifier. Essentially what you are hoping to do is to be able to map from the name of yourresourcestring
and the numeric identifier. Unfortunately that map only exists during compilation. It is not contained in the executable.Your only other hope would be for the compiler to generate RTTI information for resource strings. However, it does not do this, unless I am very much mistaken.
Given these constraints you are going to need to come up with your own mechanism to map between names and resource strings. One possibility is to avoid using the built-in
resourcestring
, and manage the string table resources, and their identifiers, by your own mechanisms.