开发者

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

开发者 https://www.devze.com 2023-03-16 21:56 出处:网络
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 associat开发者_JAVA百科ed with say \"CompanyName\".Is there a way to reference it

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 associat开发者_JAVA百科ed 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?


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);


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


This looks like version info resource, right? Then use GetFileVersionInfo API to read it. First two Delphi examples that google turned up:

1) How to extract version information using the Windows API

2) Get EXE Version Information

0

精彩评论

暂无评论...
验证码 换一张
取 消