Results 1 to 9 of 9

Thread: [SOLVED][2005] Reg_multi_sz

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jan 2007
    Posts
    79

    Resolved [SOLVED][2005] Reg_multi_sz

    After an extensive search trough our worlwide encyclopedia I have to bother you all, my apologies, but here goes;

    I'm trying to read an REG_MULTI_SZ registry key, I already know how to write it, but it flakes on the reading part, here is how far I got:

    Code:
    Dim key As Microsoft.Win32.RegistryKey
    key = Microsoft.Win32.Registry.LocalMachine.OpenSubKey("SYSTEM\Program\Attr\")
    	If key IsNot Nothing Then
    	Dim strNotes() As String
    	strNotes = CType(key.GetValue("Notes"), String())
    	If strNotes IsNot Nothing Then
    		For X = 0 To UBound(strNotes)
    			strInfo = strInfo & vbCrLf & strNotes(X)
    		Next
    	End If
    I get this error:

    "Unable to cast object of type 'System.String' to type 'System.String[]'

    But I'm sure the regvalue is a array, so I'm a bit confused.

    Could you help me with some suggestions?

    Kind Regards, Starf0x
    Last edited by Starf0x; Feb 18th, 2008 at 10:00 AM. Reason: Solved

  2. #2
    Frenzied Member dynamic_sysop's Avatar
    Join Date
    Jun 2003
    Location
    Ashby, Leicestershire.
    Posts
    1,142

    Re: [2005] Reg_multi_sz

    you are trying to retrieve a string value, but your code is specifying a string() array on the following line ...
    strNotes = CType(key.GetValue("Notes"), String())
    try making it a normal string, eg:
    Code:
    Dim strNotes As String = CType(key.GetValue("Notes"), String)
    Console.Writeline(strNotes)
    ~
    if a post is resolved, please mark it as [Resolved]
    protected string get_Signature(){return Censored;}
    [vbcode][php] please use code tags when posting any code [/php][/vbcode]

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Jan 2007
    Posts
    79

    Re: [2005] Reg_multi_sz

    Thank you Dynamic_sysop for your quick response;

    If I try this, the error is opposite from the previous one:

    "Conversion from type 'String()' to type 'String' is not valid."

    Kind Regards, Starf0x

  4. #4
    Frenzied Member dynamic_sysop's Avatar
    Join Date
    Jun 2003
    Location
    Ashby, Leicestershire.
    Posts
    1,142

    Re: [2005] Reg_multi_sz

    ok, you can check the type of data you are retrieving, then depending if it's a string or string[] handle it as req.
    eg:
    Code:
    '/// not tested this on a reg key, but did it with a string / string[] example i made.
    Dim key As Microsoft.Win32.RegistryKey
    key = Microsoft.Win32.Registry.LocalMachine.OpenSubKey("SYSTEM\Program\Attr\")
    	If key IsNot Nothing Then
                     If key.GetValue("Notes").GetType Is Type.GetType("System.String[]", False, True) Then
                     '/// it's an array.
    	    Dim strNotes() As String = CType(key.GetValue("Notes"), String())
    	    If strNotes IsNot Nothing Then
    		    For X = 0 To UBound(strNotes)
    			    strInfo = strInfo & vbCrLf & strNotes(X)
    		    Next
                     Else
                         '/// it's a normal string.
                         Dim strNotes As String = CType(key.GetValue("Notes"), String)
                         '/// NO ARRAY HANDLING THIS TIME DUE TO NORMAL STRING...
                         strInfo = strInfo & strNotes
    	End If
    ~
    if a post is resolved, please mark it as [Resolved]
    protected string get_Signature(){return Censored;}
    [vbcode][php] please use code tags when posting any code [/php][/vbcode]

  5. #5
    Fanatic Member schoolbusdriver's Avatar
    Join Date
    Jan 2006
    Location
    O'er yonder
    Posts
    1,020

    Re: [2005] Reg_multi_sz

    A REG_MULTI_SZ is a simple string which uses a null as a seperator for the different elements it contains. This is a snippet.... but you'll see how it works.

    The value in the registry is:
    Code:
    "UserName PassWord 123456789 Easter Egg".
    When retrieved it appears in a text box as:
    Code:
    0 'Indicates success...
    UserName '1st part
    PassWord '2nd part
    123456789 '3rd part
    Easter Egg '4th part
    vb Code:
    1. Dim strRet() As String = Nothing
    2.  
    3. Case butMultiSz_2.Name 'Read a REG_MULTI_SZ.
    4.    txtMsgs.Text = NetReadMultiSZ(hKeyString, (txtSubKey.Text), (txtValueName.Text), strRet)
    5.    If strRet Is Nothing Then Exit Sub
    6.    For i As Integer = 0 To strRet.Length - 1
    7.       txtMsgs.Text = txtMsgs.Text & vbCrLf & Trim$(strRet(i))
    8.    Next
    vb Code:
    1. Public Function NetReadMultiSZ(ByRef strhKey As String, ByRef strSubKey As String, ByRef strValueName As String, ByRef strRetVal() As String) As Integer
    2.    On Error GoTo NetERROR
    3.    Dim strKey As String = strhKey & "\" & strSubKey
    4.    strRetVal = My.Computer.Registry.GetValue(strKey, strValueName, ERROR_FAILED)
    5.    Return ERROR_SUCCESS '0
    6.    Exit Function
    7. NetERROR:
    8.    Return ERROR_FAILED 'Custom error -999
    9. End Function

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2005] Reg_multi_sz

    When you read a REG_MULTI_SZ from the Registry it is returned as a String array. If it's not in your case then it's not a REG_MULTI_SZ in the first place. I just tested this and it worked perfectly:

    1. Create a new WinForms project.
    2. Add two Buttons to the form.
    3. Add this code:
    Code:
    Private Sub Button1_Click(ByVal sender As Object, _
                              ByVal e As EventArgs) Handles Button1.Click
        Dim values As String() = {"Hello", "World"}
    
        My.Computer.Registry.SetValue("HKEY_LOCAL_MACHINE\Software", _
                                      "Test", _
                                      values, _
                                      Microsoft.Win32.RegistryValueKind.MultiString)
    
    End Sub
    
    Private Sub Button2_Click(ByVal sender As Object, _
                              ByVal e As EventArgs) Handles Button2.Click
        Dim values As String() = TryCast(My.Computer.Registry.GetValue("HKEY_LOCAL_MACHINE\Software", _
                                                                       "Test", _
                                                                       Nothing), _
                                         String())
    
        For Each value As String In values
            MessageBox.Show(value)
        Next
    End Sub
    4. Run the project.
    5. Click Button1.
    6. Open the Registry to HKEY_LOCAL_MACHINE\Software and confirm that the value 'Test' exists and that its value is "Hello World".
    7. Click Button2 and see that the array contains elements "Hello" and "World".

    This is exactly the expected behaviour.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  7. #7

    Thread Starter
    Lively Member
    Join Date
    Jan 2007
    Posts
    79

    Re: [2005] Reg_multi_sz

    Quote Originally Posted by dynamic_sysop
    ok, you can check the type of data you are retrieving, then depending if it's a string or string[] handle it as req.
    eg:
    Code:
    .....
    I See what you mean Dynamic_Sysop, I shouldn't assume it would be an array, but check first.
    This is something I was looping in, I needed an eye opener, Thank You, I will proceed with this idea.

    Kind Regards, Starf0x

  8. #8
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [SOLVED][2005] Reg_multi_sz

    I would tend to disagree a bit here. I would not get the data and then test its type. A REG_MULTI_SZ is going to be returned as a String array EVERY time. If you know that the Registry value you're retrieving is a REG_MULTI_SZ then you know it will be returned as a String array. If you aren't 100% sure what the type of the value you're retrieving is then you can use the GetValueKind method:
    vb.net Code:
    1. Dim key As RegistryKey = Registry.LocalMachine.OpenSubKey("key path here")
    2.  
    3. Select Case key.GetValueKind("value name here")
    4.     Case RegistryValueKind.String
    5.         'The value is type REG_SZ and will be returned as a String.
    6.     Case RegistryValueKind.MultiString
    7.         'The value is type REG_MULTI_SZ and will be returned as a String array.
    8.     'And so on for the other types.
    9. End Select
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  9. #9

    Thread Starter
    Lively Member
    Join Date
    Jan 2007
    Posts
    79

    Re: [SOLVED][2005] Reg_multi_sz

    Quote Originally Posted by jmcilhinney
    I would tend to disagree a bit here. I would not get the data and then test its type. A REG_MULTI_SZ is going to be returned as a String array EVERY time. If you know that the Registry value you're retrieving is a REG_MULTI_SZ then you know it will be returned as a String array. If you aren't 100% sure what the type of the value you're retrieving is then you can use the GetValueKind method
    This is a good approach, I'll use this to approach my problem, Thank you

    Kind Regards, Starf0x

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