Setting Unicode Header from Resource DLL String Table

I am having problems displaying Unicode characters incorrectly in my user interface. I only have a resource library containing a string table used to localize the user interface. I create a DLL in Delphi XE3 using a DLL project (it just has {$R 'lang.res' 'lang.rc'} in the DPR file and gives me lang.dll ). I confirmed that my lang.rc file is in UTF-8 format with Windows CRLF line breaks. When I load strings from a DLL, Unicode characters mix with the interface. Here are a few details.

Excerpt from the row table:

 STRINGTABLE { 59,"180˚" 60,"90˚ CW" 61,"90˚ CCW" } 

Here are the code snippets that illustrate the problem that I am encountering with Unicode characters:

 // explicitly assigning the degrees character shows 180˚ properly ImageMenu180Action.Caption := '180˚'; // getting the resource from the DLL shows some weird two-character string for the degrees character ImageMenu90CWAction.Caption := TLangHelper.LoadStr(IDS_ImageMenuRotationCW90); // OutputDebugString shows the degrees character in the debugger output correctly OutputDebugString(PChar('IDS_ImageMenuRotationCW90: '+TLangHelper.LoadStr(IDS_ImageMenuRotationCW90))); 

Here is my Delphi function used to load strings from a resource library:

 class function TLangHelper.LoadStr(ResourceId: Integer):String; var Buff: String; L: Integer; begin Result := ''; if LangDllHandle = 0 then begin LangDllHandle := LoadLibrary(LANGUAGE_DLL_LOCATION); if LangDllHandle = 0 then begin ShowMessage('Error loading language localization resources.'); end; end; if LangDllHandle <> 0 then begin L := 1024; SetLength(Buff, L+1); LoadString(LangDllHandle, ResourceId, PChar(Buff), L); Result := String(PChar(Buff)); end; end; 

Any suggestions?

FOLLOW-UP:

For Chinese characters, I had to add the previous L to the line definitions in the .rc file so that the DLL compilation would recognize them as Unicode. For example (English, Chinese Traditional, Chinese Simplified, French):

 STRINGTABLE { 35,"Status Bar" 1035,L"狀態欄" 2035,L"ηŠΆζ€ζ " 3035,"Barre d'Γ©tat" } 
+6
source share
1 answer

I found the link since 2002 , indicating that you need to tell the resource compiler how the .rc file is encoded. For UTF-8, this code page is 65001, so you should run this:

  brcc32 -c65001 lang.rc

Then, of course, you remove the 'lang.rc' from the $R directive in your code, because you no longer want the IDE to invoke the resource compiler itself.

If your version of Delphi is modern enough, you can keep the full $R directive and instead set the -c65001 parameter in the resource-compiler configuration of your project options.

It's hard to know the encoding of a file just by looking at it. There can be many valid conjectures. The -c option is documented, but the documentation does not mention when you need to use it, or what the IDE uses when it starts the resource compiler. The IDE probably just uses the default value, the same as the brcc32.exe file, which is the default ANSI code page.

+6
source

Source: https://habr.com/ru/post/978046/


All Articles