1

I found this tutorial a few days ago and I followed it. Seeing as I am only interested in loading text from the resource file, I only used the that was relevant to what I needed. The code can be found from the tutorial can be found here. Note: I am using code from the functions GetResourceAsPointer and GetResourceAsString. I have:

  • created the resource file which contains one text file. The text file contains "test"
  • included {$R resource.res}

Note: I use a memo to load the string from resource.

The program is able to compile without any errors but when I click the button to load the string into the memo, a string is loaded but not "test". Instead I get random characters such a squares and Chinese characters.

Does anyone know what the problem could be? Has anyone experience this before?

Thank you in advanced, Peter

ple103
  • 2,080
  • 6
  • 35
  • 50

2 Answers2

2

I use the following two routines in order to obtain text from resources added to the EXE: one routine is for ANSI strings and one for Unicode strings. Your random characters may be due to reading more characters than needed.

Function GetResString (i, lang: integer): string;
var
 buffer: array [0..400] of char;
 ls: integer;

begin
 result:= '';
 ls:= loadstring (hinstance, i + lang * 1000, buffer, sizeof (buffer));
 if ls <> 0 then result:= buffer
end;

function LoadResW (id, lang: integer): WideString;
const
 maxlength = 1024;

var
 ls: integer;

begin;
 setlength (result, maxlength);
 ls:= loadstringw (hinstance, lang * 1000 + id,
 pwidechar (result), length (result));
 if ls > 0
  then setlength (result, ls)
  else result:= ''
end;
No'am Newman
  • 6,395
  • 5
  • 38
  • 50
  • Thank you for sharing your code but what values should I use to be able to load strings from resource? – ple103 Jan 15 '12 at 10:23
  • 1
    The value that you used in the resource. The resource file would look like this: STRINGTABLE { 1001, "Indifferent" 1002, "Objective" 1003, "Likes change" 1004, "Autonomous" 1005, "Optimistic"}. In order to obtain the string 'Autonomous', you would pass the GetResString function the values 4 and 1 (I store strings for different languages with the same suffix but different prefix, ie there might be an entry 2004, "Autonomo" if Italian were language 2. – No'am Newman Jan 15 '12 at 11:45
1

In which encoding is text file that you put to resource? If ANSI, try save it as Unicode (GetResourceAsString from article use PChar type).

Pol
  • 5,064
  • 4
  • 32
  • 51