Click to See Complete Forum and Search --> : reading REG_SZ has extra spaces...


lilrangerabn
April 9th, 2010, 07:17 PM
I have a method where I am reading a REG_SZ from a registry key. I know that the value of buff is exactly what is in the REG_SZ key, however when converting it to a System::String it gets extra spaces added in between the correct values.


String^ flags;
char buff[255];
dwType = REG_SZ;
dwSize = sizeof(buff);
memset(buff, 0, sizeof(buff));
if (::RegQueryValueEx( keyHandle, L"regvalue", NULL, &dwType,
(BYTE*) buff, &dwSize) == ERROR_SUCCESS){
char * temp = &buff[0];
flags = gcnew String(temp, 0, sizeof(buff));
//simple test to ensure what we are seeing
for (int i=0; i < sizeof (buff); i++ ){
printf(&buff[i]);
}
Console::WriteLine("\nCurrent REG_SZ value is: \n\t " + flags);
}

Sample output from the printf:
BOB, BOB
Sample output from the WriteLine:
B O B , B O B

Can anyone explain why and advise me how to correct it so that it does not add the spaces?

Alex F
April 11th, 2010, 02:41 AM
Try to be more specific creating managed string:

flags = Marshal:: PtrToStringAnsi(IntPtr(temp));

Generally, don't mix managed and unmanaged types when it is not absolutely necessary. Use Microsoft:: Win32:: RegistryKey class for managed Registry operations.

lilrangerabn
April 11th, 2010, 05:06 AM
thanks again Alex.