 |
|
 |
Goran Level: Moderator
 Registered: 16-05-2002 Posts: 1681
|
Re: How to Use RegQueryValueEx In VB
Here ia a sample code from AllApi
'This program needs 3 buttons
Const REG_SZ = 1 ' Unicode nul terminated string
Const REG_BINARY = 3 ' Free form binary
Const HKEY_CURRENT_USER = &H80000001
Private Declare Function RegCloseKey Lib "advapi32.dll" (ByVal hKey As Long) As Long
Private Declare Function RegCreateKey Lib "advapi32.dll" Alias "RegCreateKeyA" (ByVal hKey As Long, ByVal lpSubKey As String, phkResult As Long) As Long
Private Declare Function RegDeleteValue Lib "advapi32.dll" Alias "RegDeleteValueA" (ByVal hKey As Long, ByVal lpValueName As String) As Long
Private Declare Function RegOpenKey Lib "advapi32.dll" Alias "RegOpenKeyA" (ByVal hKey As Long, ByVal lpSubKey As String, phkResult As Long) As Long
Private Declare Function RegQueryValueEx Lib "advapi32.dll" Alias "RegQueryValueExA" (ByVal hKey As Long, ByVal lpValueName As String, ByVal lpReserved As Long, lpType As Long, lpData As Any, lpcbData As Long) As Long
Private Declare Function RegSetValueEx Lib "advapi32.dll" Alias "RegSetValueExA" (ByVal hKey As Long, ByVal lpValueName As String, ByVal Reserved As Long, ByVal dwType As Long, lpData As Any, ByVal cbData As Long) As Long
Function RegQueryStringValue(ByVal hKey As Long, ByVal strValueName As String) As String
Dim lResult As Long, lValueType As Long, strBuf As String, lDataBufSize As Long
'retrieve nformation about the key
lResult = RegQueryValueEx(hKey, strValueName, 0, lValueType, ByVal 0, lDataBufSize)
If lResult = 0 Then
If lValueType = REG_SZ Then
'Create a buffer
strBuf = String(lDataBufSize, Chr$(0))
'retrieve the key's content
lResult = RegQueryValueEx(hKey, strValueName, 0, 0, ByVal strBuf, lDataBufSize)
If lResult = 0 Then
'Remove the unnecessary chr$(0)'s
RegQueryStringValue = Left$(strBuf, InStr(1, strBuf, Chr$(0)) - 1)
End If
ElseIf lValueType = REG_BINARY Then
Dim strData As Integer
'retrieve the key's value
lResult = RegQueryValueEx(hKey, strValueName, 0, 0, strData, lDataBufSize)
If lResult = 0 Then
RegQueryStringValue = strData
End If
End If
End If
End Function
Function GetString(hKey As Long, strPath As String, strValue As String)
Dim Ret
'Open the key
RegOpenKey hKey, strPath, Ret
'Get the key's content
GetString = RegQueryStringValue(Ret, strValue)
'Close the key
RegCloseKey Ret
End Function
Sub SaveString(hKey As Long, strPath As String, strValue As String, strData As String)
Dim Ret
'Create a new key
RegCreateKey hKey, strPath, Ret
'Save a string to the key
RegSetValueEx Ret, strValue, 0, REG_SZ, ByVal strData, Len(strData)
'close the key
RegCloseKey Ret
End Sub
Sub SaveStringLong(hKey As Long, strPath As String, strValue As String, strData As String)
Dim Ret
'Create a new key
RegCreateKey hKey, strPath, Ret
'Set the key's value
RegSetValueEx Ret, strValue, 0, REG_BINARY, CByte(strData), 4
'close the key
RegCloseKey Ret
End Sub
Sub DelSetting(hKey As Long, strPath As String, strValue As String)
Dim Ret
'Create a new key
RegCreateKey hKey, strPath, Ret
'Delete the key's value
RegDeleteValue Ret, strValue
'close the key
RegCloseKey Ret
End Sub
Private Sub Command1_Click()
Dim strString As String
'Ask for a value
strString = InputBox("Please enter a value between 0 and 255 to be saved as a binary value in the registry.", App.Title)
If strString = "" Or Val(strString) > 255 Or Val(strString) < 0 Then
MsgBox "Invalid value entered ...", vbExclamation + vbOKOnly, App.Title
Exit Sub
End If
'Save the value to the registry
SaveStringLong HKEY_CURRENT_USER, "KPD-Team", "BinaryValue", CByte(strString)
End Sub
Private Sub Command2_Click()
'Get a string from the registry
Ret = GetString(HKEY_CURRENT_USER, "KPD-Team", "BinaryValue")
If Ret = "" Then MsgBox "No value found !", vbExclamation + vbOKOnly, App.Title: Exit Sub
MsgBox "The value is " + Ret, vbOKOnly + vbInformation, App.Title
End Sub
Private Sub Command3_Click()
'Delete the setting from the registry
DelSetting HKEY_CURRENT_USER, "KPD-Team", "BinaryValue"
MsgBox "The value was deleted ...", vbInformation + vbOKOnly, App.Title
End Sub
Private Sub Form_Load()
'KPD-Team 1998
'URL: http://www.allapi.net/
'E-Mail: KPDTeam@Allapi.net
Command1.Caption = "Set Value"
Command2.Caption = "Get Value"
Command3.Caption = "Delete Value"
End Sub |
____________________________
If you find the answer helpful, please mark this topic as solved.
|
|
22-06-2004 at 05:50 PM |
|
|
Goran Level: Moderator
 Registered: 16-05-2002 Posts: 1681
|
Re: How to Use RegQueryValueEx In VB
RegQueryValueEx
- hKey identifies a currently opened key
- lpValueName contains the name of the value to be queried
- lpReserved must be null
- lpType will receive key's value type
- lpData points to a buffer that receives the value’s data
- lpcbData specifies the size, in bytes, of the buffer pointed to by the lpData parameter
So, first time RegQueryValueEx is called, we get lpType ( which in your case will be REG_BINARY, as you said) and lpcbData. Then we use lpcbData to create a buffer, which will be enough big to receive key's content. If we didnt do that, and buffer is not big enough, the function would return ERROR_MORE_DATA.
If I wasnt clear enough, ask what exactly you dont understand, and I will explain.
____________________________
If you find the answer helpful, please mark this topic as solved.
|
|
24-06-2004 at 09:20 PM |
|
|
FMFMFM Level: Guest

|
Re: How to Use RegQueryValueEx In VB
hello goran
i've also send u an email, just thought to send u a msg here as well.
When i use that function one from the example that you suggessted, the Hive is opening, and the RegQueryValue function operates successfully too the first time, but when it's called again, on the basis of the REG_BINARY type, here i just need to know that "while calling the function, should we pass the HANDLE of the opened key to the hkey parameter, or the HKEY_CURRENT_USER as a parameter to hkey.
Because when i used the HANDLE variable it was successfull, as the lreturn, returned 0, but the second time on the basis of REG_BINARY, as it executed the function, vb was shutdown, the size that was returned was 4025 in the ldatabuffsize.
THanks Goran
|
|
27-06-2004 at 03:35 PM |
|
|  |
|
|
Goran Level: Moderator
 Registered: 16-05-2002 Posts: 1681
|
Re: How to Use RegQueryValueEx In VB
It seems you are dealing with big values there, try this then:
Function RegQueryStringValue(ByVal hKey As Long, ByVal strValueName As String) As String
Dim lResult As Long
Dim lValueType As Long
Dim strBuf As String
Dim lDataBufSize As Long
Dim strTemp As String
Dim i As Integer
'retrieve nformation about the key
lResult = RegQueryValueEx(hKey, strValueName, 0, lValueType, ByVal 0, lDataBufSize)
'Create a buffer
strBuf = String(lDataBufSize, Chr$(0))
'retrieve the key's content
lResult = RegQueryValueEx(hKey, strValueName, 0, 0, ByVal strBuf, lDataBufSize)
If lResult = 0 And lValueType = REG_BINARY Then
For i = 1 To lDataBufSize
strTemp = Hex(Asc(Mid(strBuf, i, 1)))
If Len(strTemp) = 1 Then strTemp = "0" & strTemp
RegQueryStringValue = RegQueryStringValue + strTemp
Next
End If
End Function |
hKey is a handle of an opened key, handle of a key like HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\Streams\Desktop, strValueName is a Value name like "Taskbar". See the above example how to get this handle.
____________________________
If you find the answer helpful, please mark this topic as solved.
|
|
27-06-2004 at 06:07 PM |
|
|
tonymm Level: Trainee
 Registered: 06-07-2005 Posts: 1
|
Re: How to Use RegQueryValueEx In VB
Hi Goran, thanks for the sample, I was able to read big binary strings but I was not able to write them back (only small binary string. (Here is a sample of the string that I need to write back
0100048084000000900000000000000014000000020070000400000000001800FF030F000102000000000005200000002002000000002400B1010000010500000000000515000000F11357004E304F4BC42DBF553D30000000001400FF030F0001010000000000051200000000001800A101000001020000000000052000000021020000010100000000000512000000010100000000000512000000
Any ideas?
thanks in advance.
|
|
06-07-2005 at 10:05 PM |
|
|
|
|
 |
 |