Read Binary data from registry to a string RRS feed

  • Question

  • Here what i did till yet.
    Now remain how i get a bytearray from the systemobject-pointer.
    In regobj is a pointer to the systemobject.
    From it i am unable to make a byte array.
    If i use the ToString-Method i receive only the string "SYSTEM.BYTE[]"
    as content (it's type - not the contents).
    How can i make a string or a bytearray from System.Byte? 

    private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) {

    using namespace Microsoft::Win32;

    using namespace System;

    RegistryKey^ rk;

    Object^ regobj;

    String^ regstr;

    rk = Registry::LocalMachine->OpenSubKey("SYSTEM\\CurrentControlSet\\Services\\RpcSs", true);

    if (!rk){System::Windows::Forms::MessageBox::Show("Error", "");}

    regobj = rk->GetValue("FailureActions");


    regstr = regobj->ToString();

    //Byte regbyte[] = System::Text::Encoding::Convert(System::Text::Encoding::Unicode, System::Text::Encoding::ASCII, regobj);

    System::Windows::Forms::MessageBox::Show(regstr, "");



    Saturday, February 25, 2006 11:56 PM


  • Your going to have trouble converting that particular registry key to a string, primarily because it contains too many 0s, which cannot be displayed in normal Message boxes.

    If you have a string visualiser that can understand zeroes (eg. Console::WriteLine, C#'s string visualiser, String::Length, FileStream), then you will see that the string correctly holds the zero bytes.

    If the registry value has the REG_BINARY type, RegistryKey::GetValue's return value will be of type cli::array<unsigned char> ^. Note that the debugger (which calls into the .NET framework to get the type) returns "System.Byte[]" mostly to help C# / VB programmers.

    If you want to know the type returned by GetValue at runtime, use the GetValueKind method.

    To convert a cli::array<unsigned char>^ to String^, call the GetString method of System::Text::Encoding^.

    In the example below I replace all zeroes with the character '_' (just so I can see the thing in the message box).

    int ShowKeyValue(void)


    RegistryKey ^rk = Registry::LocalMachine->OpenSubKey("SYSTEM\\CurrentControlSet\\Services\\RpcSs", false);

    if (!rk)
    "Error", "");

    Object ^regobj = rk->GetValue("FailureActions");

    /* To create an array of bytes, you use this syntax: */
    array<unsigned char> ^regbyte =
      /* Can also use static_cast, or safe_cast. Don't recommend C-Style casts though */
    dynamic_cast<cli::array<unsigned char> ^>(regobj);

    for each (unsigned char %item in regbyte)
    /* This loop replaces all occurrences of zeros in the array with the character '_' */

      if(item == 0) item = '_';


    String ^regstr = System::Text::Encoding::ASCII->GetString(regbyte);
    int stringlength = regstr->Length; /* Exercise: Guess this value. */
    System::Windows::Forms::MessageBox::Show(regstr, "");

    return 0;


    Sunday, February 26, 2006 3:16 PM