We help IT Professionals succeed at work.

Registry Manipulations In VB Module

bonnseye
bonnseye asked
on
Medium Priority
279 Views
Last Modified: 2012-06-21
Hello Experts,

I am making a VB application in which in the first module I am doing some manipulations with registry settings.

From MSDN I am not very clear that VB can do this alone. For this I switched to USING Win32 Api's in VB. But as I am new to this concept, I just was unable to do it and I have no time to look particularly in this matter as time for my project is at the last stage.

Can any one of u please help me, in With all DECLARES, CONTANTS,TYPES, PROTOTYPES etc. of using these registry API'S. I just need opening, creating, editing examples in the form of code, in VB.

I need some examples for doing all these.

Best Regards
Bonns

Comment
Watch Question

Richie_SimonettiIT Operations
CERTIFIED EXPERT

Commented:
Is kinda easy, waht type of values you will stored... strings, numbers?

Author

Commented:
can be both types two keys with string value and one with number value.

Best Regards
Bonns


Commented:
1) add this code to some method in your application eg. form_load event

Const HKEY_LOCAL_MACHINE = &H80000002
SetRegistryValue HKEY_LOCAL_MACHINE, "path\to\the\key", "TestValue", "Hello"
MsgBox GetRegistryValue(HKEY_LOCAL_MACHINE, "path\to\the\key", "TestValue", "It's still empty")

2) add this code to a .bas module
Option Explicit

Private Declare Function RegOpenKeyEx Lib "advapi32.dll" Alias "RegOpenKeyExA" (ByVal hKey As Long, ByVal lpSubKey As String, ByVal ulOptions As Long, ByVal samDesired As Long, phkResult As Long) As Long
Private Declare Function RegCloseKey Lib "advapi32.dll" (ByVal hKey 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

Const KEY_WRITE = &H20006

Const REG_SZ = 1
Const REG_BINARY = 3
Const REG_DWORD = 4
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 Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (dest As Any, source As Any, ByVal numBytes As Long)

Const KEY_READ = &H20019


Const REG_EXPAND_SZ = 2
Const REG_MULTI_SZ = 7
Const ERROR_MORE_DATA = 234

Public Function GetRegistryValue(ByVal hKey As Long, ByVal KeyName As String, ByVal ValueName As String, Optional DefaultValue As Variant) As Variant
    Dim handle As Long
    Dim resLong As Long
    Dim resString As String
    Dim resBinary() As Byte
    Dim length As Long
    Dim retVal As Long
    Dim valueType As Long
    GetRegistryValue = IIf(IsMissing(DefaultValue), Empty, DefaultValue)
    If RegOpenKeyEx(hKey, KeyName, 0, KEY_READ, handle) Then
        Exit Function
    End If
    length = 1024
    ReDim resBinary(0 To length - 1) As Byte
    retVal = RegQueryValueEx(handle, ValueName, 0, valueType, resBinary(0), length)
    If retVal = ERROR_MORE_DATA Then
        ReDim resBinary(0 To length - 1) As Byte
        retVal = RegQueryValueEx(handle, ValueName, 0, valueType, resBinary(0), length)
    End If
    Select Case valueType
        Case REG_DWORD
            CopyMemory resLong, resBinary(0), 4
            GetRegistryValue = resLong
        Case REG_SZ, REG_EXPAND_SZ
            resString = Space$(length - 1)
            CopyMemory ByVal resString, resBinary(0), length - 1
            GetRegistryValue = resString
        Case REG_BINARY
            If length <> UBound(resBinary) + 1 Then
                ReDim Preserve resBinary(0 To length - 1) As Byte
            End If
            GetRegistryValue = resBinary()
        Case REG_MULTI_SZ
            resString = Space$(length - 2)
            CopyMemory ByVal resString, resBinary(0), length - 2
            GetRegistryValue = resString
        Case Else
            RegCloseKey handle
            Err.Raise 1001, , "Unsupported value type"
    End Select
    RegCloseKey handle
End Function

Public Function SetRegistryValue(ByVal hKey As Long, ByVal KeyName As String, ByVal ValueName As String, value As Variant) As Boolean
    Dim handle As Long
    Dim lngValue As Long
    Dim strValue As String
    Dim binValue() As Byte
    Dim length As Long
    Dim retVal As Long
    If RegOpenKeyEx(hKey, KeyName, 0, KEY_WRITE, handle) Then
        Exit Function
    End If
    Select Case VarType(value)
        Case vbInteger, vbLong
            lngValue = value
            retVal = RegSetValueEx(handle, ValueName, 0, REG_DWORD, lngValue, 4)
        Case vbString
            strValue = value
            retVal = RegSetValueEx(handle, ValueName, 0, REG_SZ, ByVal strValue, Len(strValue))
        Case vbArray + vbByte
            binValue = value
            length = UBound(binValue) - LBound(binValue) + 1
            retVal = RegSetValueEx(handle, ValueName, 0, REG_BINARY, binValue(LBound(binValue)), length)
        Case Else
            RegCloseKey handle
            Err.Raise 1001, , "Unsupported value type"
    End Select
    RegCloseKey handle
    SetRegistryValue = (retVal = 0)
End Function

3) replace "path\to\the\key" with the key that you would like to change

Ark
CERTIFIED EXPERT

Commented:
Hi
If you're not against of Windows Scripting Host:
Dim objWSHShell As Object
Set objWSHShell = CreateObject("WScript.Shell")
'Create a key "MyApp"
objWSHShell.RegWrite "HKLM\Software", "MyApp\"
'Create a value "MyValue" with value "MyValue Is Something"
objWSHShell.RegWrite "HKLM\Software\MyApp\MyValue", "MyValue Is Something"
'Read value
s = objWSHShell.RegRead("HKLM\Software\MyApp\MyValue")
Set objWSHShell = Nothing

Cheers

Explore More ContentExplore courses, solutions, and other research materials related to this topic.