Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can my program read a string from a RES file?

Tags:

delphi

I have a .RC file that is included in the project and compiles into the res file. What I want to do is access the value associated with say "CompanyName". Is there a way to reference it? For instance something like

string st = VERSIONINFO["CompanyName"]

or an I totally misunderstanding it?

And I suppose as a followup, what is the proper format for a string table?

like image 209
Joshua Avatar asked Dec 03 '25 21:12

Joshua


2 Answers

To load a string from your program's string-table resources, use Delphi's LoadStr function. Pass it the numeric ID of the string you wish to read. It's a wrapper for the Windows LoadString API function.

Delphi supports resourcestrings natively. Instead of declaring const, use resourcestring, and the string will automatically be included in a compiler-generated string table. You can refer to the string by its named identifier in your code, and the run-time library will handle the details of loading it from your program's resources. Then you don't have to call LoadStr or anything else. You could declare a bunch of resourcestrings in a build-generated file so it's always up to date. For example:

// Auto-generated; do not edit
unit Resources;
interface
resourcestring
  CompanyName = '***';
implementation
end.

If you want to manage the string tables yourself, refer to the MSDN documentation. For example:

#define IDS_COMPANYNAME 1

STRINGTABLE
BEGIN
  IDS_COMPANYNAME, "***"
END

To read it in your program:

const
  IDS_COMPANYNAME = 1;

var
  CompanyName: string;

CompanyName := LoadStr(IDS_COMPANYNAME);
like image 114
Rob Kennedy Avatar answered Dec 06 '25 12:12

Rob Kennedy


procedure TForm1.Button1Click(Sender: TObject);
var
ResHandle:HRSRC;
hGlob:THandle;
thestring:AnsiString;
eu:PAnsiChar;
begin
  ResHandle:=FindResource(hInstance,'CompanyName',RT_STRING);
  hglob:=LoadResource(hInstance,ResHandle);
  eu:=LockResource(hGlob);
  theString:=eu;
  ShowMessage(thestring);
end;

Modify AnsiString to String if that doesn't work ;) didn't include error checking

like image 32
opc0de Avatar answered Dec 06 '25 12:12

opc0de



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!