Re: Problem with using char* to return string by reference
swtbase@gmail.com wrote:
I have a problem using char* in C++.
My code looks like this:
void ReadInitRegVal(char* a, char* b, float& c, float& d)
Irks. You know about references, so simply pass in a reference to a
std::string.
HKEY hKey;
DWORD DataSize;
BYTE temp[256];
Just a suggestion, declare and initialise your variables as close to where
you use them as possible.
RegOpenKeyEx(HKEY_CURRENT_USER, REGDATASTOREKEY, 0, KEY_READ, &hKey);
DataSize = 256;
RegQueryValueEx(hKey, "Data0", 0, NULL, temp, &DataSize);
strcpy(a, (char*)temp);
Do not use these casts in C++. For a beginner in particular, that is a 100%
never! Secondly, you have no idea how much data you can store in 'a', which
is one reason that std::string would be much better.
The program displays values b, c and d correctly but displays nothing
in place of a (null value to be correct). On checking, a[0 to 8] gives
output ' n t e l L A N ' (String to be read is 'Intel LAN Adapter'.
My guess is that the data format stored in the registry is a text encoded in
UTF-16, where every character uses one or two 16-bit sequences as opposed
to the 8-bit sequence your code assumes.
Just a last suggestion: try to stay away from the win32 API for now and get
familiar with C++ a bit more, in particular the area of string handling.
;)
Uli
--
C++ FAQ: http://parashift.com/c++-faq-lite
Sator Laser GmbH
Gesch??ftsf??hrer: Thorsten F??cking, Amtsgericht Hamburg HR B62 932