|
-
Feb 17th, 2008, 03:04 PM
#1
Thread Starter
Lively Member
[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
-
Feb 17th, 2008, 03:09 PM
#2
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]
-
Feb 17th, 2008, 03:17 PM
#3
Thread Starter
Lively Member
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
-
Feb 17th, 2008, 04:20 PM
#4
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]
-
Feb 18th, 2008, 05:07 AM
#5
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:
Dim strRet() As String = Nothing
Case butMultiSz_2.Name 'Read a REG_MULTI_SZ.
txtMsgs.Text = NetReadMultiSZ(hKeyString, (txtSubKey.Text), (txtValueName.Text), strRet)
If strRet Is Nothing Then Exit Sub
For i As Integer = 0 To strRet.Length - 1
txtMsgs.Text = txtMsgs.Text & vbCrLf & Trim$(strRet(i))
Next
vb Code:
Public Function NetReadMultiSZ(ByRef strhKey As String, ByRef strSubKey As String, ByRef strValueName As String, ByRef strRetVal() As String) As Integer
On Error GoTo NetERROR
Dim strKey As String = strhKey & "\" & strSubKey
strRetVal = My.Computer.Registry.GetValue(strKey, strValueName, ERROR_FAILED)
Return ERROR_SUCCESS '0
Exit Function
NetERROR:
Return ERROR_FAILED 'Custom error -999
End Function
-
Feb 18th, 2008, 07:37 AM
#6
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.
-
Feb 18th, 2008, 09:57 AM
#7
Thread Starter
Lively Member
Re: [2005] Reg_multi_sz
 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:
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
-
Feb 18th, 2008, 05:50 PM
#8
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:
Dim key As RegistryKey = Registry.LocalMachine.OpenSubKey("key path here") Select Case key.GetValueKind("value name here") Case RegistryValueKind.String 'The value is type REG_SZ and will be returned as a String. Case RegistryValueKind.MultiString 'The value is type REG_MULTI_SZ and will be returned as a String array. 'And so on for the other types. End Select
-
Feb 19th, 2008, 03:30 AM
#9
Thread Starter
Lively Member
Re: [SOLVED][2005] Reg_multi_sz
 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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|