views:

275

answers:

2

I'm using codegear c++ builder 2007. I'm trying to read a string value with a path from the registry. This path can contain unicode characters, for example russian.

I have added a string value with regedit and verified by exporting that the value really contains the expected unicode characters. The result in S1, S2 and S3 below all contains '?' (0x3F) instead of the unicode characters. What am I missing?

TRegistry *Registry = new TRegistry;  
try  
{  
  Registry->RootKey = HKEY_CURRENT_USER;  
  if (Registry->OpenKey ("Software\\qwe\\asd", false))  
    {  
        AnsiString S1 = Registry->ReadString ("zxc");  
        WideString S2 = Registry->ReadString ("zxc");  
        UTF8String S3 = Registry->ReadString ("zxc");  
    }  
}  
__finally  
{  
    delete Registry;  
}

/Björn

A: 

http://do-the-right-things.blogspot.com/2008/03/codegear-delphi-2006nets-tregistry.html

CodeGear Delphi 2006.Net's TRegistry fails in Framework 2 SP1

I don't know whether C++ 2007 is also affected, but if it is, maybe there is a patch available somewhere.

Pim
Unfortunately that's not the problem. The length of the returned string is correct, but every unicode character is replaced with a question mark (ascii 0x3F).
Björn
+2  A: 

The VCL in C++Builder (and Delphi) 2007 uses Ansi, not Unicode. TRegistry::ReadString() is internally calling the Win32 API RegQueryValueExA() function instead of RegQueryValueExW(), and TRegistry::ReadString() returns an AnsiString that uses the OS default Ansi codepage. Any Unicode data gets automatically converted to Ansi by the OS before your code ever sees it. The '?' character means that a Unicode character got converted to an Ansi codepage that does not support that character. It does not matter what string type you assign the result of ReadString() to, the Unicode data has already been lost before ReadString() even exits.

If you need to read Unicode data as Unicode, then you need to call RegQueryValueExW() directly instead of using TRegistry::ReadString() (or upgrade to C++Builder 2009 or later, which now use Unicode).

Remy Lebeau - TeamB