CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Apr 2010
    Posts
    13

    Question reading REG_SZ has extra spaces...

    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.

    Code:
    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?

  2. #2
    Join Date
    Jul 2002
    Posts
    2,543

    Re: reading REG_SZ has extra spaces...

    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.

  3. #3
    Join Date
    Apr 2010
    Posts
    13

    Re: reading REG_SZ has extra spaces...

    thanks again Alex.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured