Re: Problem with using char* to return string by reference
<swtbase@gmail.com> ha scritto nel messaggio
news:73a1d4b5-a396-46e7-a8c3-69805ab423ec@x41g2000hsb.googlegroups.com...
Although you suggested that UTF and UNICODE formats could be a problem
for char*, I want to restate that I have already used cout to print
temp and a (local to ReadInitRegValue()) values from within the
I use the ancient VC++ 6 complier. I don't have cstring.h and don't
want to use vector stuff. What options do I have?
OK, I tried modifying your function (but I did not test on VC6, so I hope it
works... I did not compile that modified code...)
<code>
// Use STL std::string
#include <string>
void ReadInitRegVal(
std::string & a,
std::string & b,
float & c,
float & d
)
{
HKEY hKey;
DWORD DataSize;
BYTE temp[ 256 ];
// Clear output parameters
a = "";
b = "";
c = 0.0;
d = 0.0;
RegOpenKeyEx(HKEY_CURRENT_USER, REGDATASTOREKEY, 0, KEY_READ, &hKey);
DataSize = 256;
RegQueryValueEx(hKey, "Data0", 0, NULL, temp, &DataSize);
temp[ 255 ] = '\0'; // Be sure to end string
a = std::string( (char*)temp );
DataSize = 20;
RegQueryValueEx(hKey, "Data1", 0, NULL, temp, &DataSize);
temp[ 19 ] = '\0'; // Be sure to end string
b = std::string( (char*)temp );
DataSize = 20;
RegQueryValueEx(hKey, "Data2", 0, NULL, temp, &DataSize);
temp[ 19 ] = '\0'; // Be sure to end string
c = (float)atof((char*)temp);
DataSize = 20;
RegQueryValueEx(hKey, "Data3", 0, NULL, temp, &DataSize);
temp[ 19 ] = '\0'; // Be sure to end string
d = (float)atof((char*)temp);
RegCloseKey(hKey);
}
</code>
As I already wrote, I would add a check to *each* RegQueryValueEx function.
And I would write Unicode aware code...
However, in your ANSI build, the above code might work...
To test, try that:
<code>
#include <string>
int main()
{
std::string a;
std::string b;
float c;
float d;
ReadInitRegVal(a, b, c, d);
cout << a << '\n' << b << '\n' << c << '\n' << d << '\n';
return 0;
}
</code>
HTH,
Giovanni