Adding/Editing a Registry Key
Hi All
I want to add a regsitry based on a choice that the user will make when they click on the COMMAND BOTTON.
How do I create the registry key using VB6, I read something about using API.
Thats what I have, does that make sence?
Code:
Option Explicit
Private Const REG_SZ As Long = 1 'REG_SZ represents a fixed-length text string.
Private Const REG_DWORD As Long = 4 'REG_DWORD represents data by a number that is 4 bytes long.
Private Const HKEY_CLASSES_ROOT = &H80000000 'The information stored here ensures that the correct program opens when you open a file by using Windows Explorer.
Private Const HKEY_CURRENT_USER = &H80000001 'Contains the root of the configuration information for the user who is currently logged on.
Private Const HKEY_LOCAL_MACHINE = &H80000002 'Contains configuration information particular to the computer (for any user).
Private Const HKEY_USERS = &H80000003 'Contains the root of all user profiles on the computer.
'Return values for all registry functions
Private Const ERROR_SUCCESS = 0
Private Const ERROR_NONE = 0
Private Const KEY_QUERY_VALUE = &H1 'Required to query the values of a registry key.
Private Const KEY_ALL_ACCESS = &H3F 'Combines the STANDARD_RIGHTS_REQUIRED, KEY_QUERY_VALUE, KEY_SET_VALUE, KEY_CREATE_SUB_KEY, KEY_ENUMERATE_SUB_KEYS, KEY_NOTIFY, and KEY_CREATE_LINK access rights.
'API Calls for writing to Registry
'Close Registry Key
Private Declare Function RegCloseKey Lib "advapi32.dll" (ByVal hKey As Long) As Long
'Create Registry Key
Private Declare Function RegCreateKey Lib "advapi32.dll" Alias "RegCreateKeyA" (ByVal hKey As Long, ByVal lpSubKey As String, phkResult As Long) As Long
'Open Registry Key
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
'Query a String Value
Private Declare Function RegQueryValueExString Lib "advapi32.dll" Alias "RegQueryValueExA" (ByVal hKey As Long, ByVal lpValueName As String, ByVal lpReserved As Long, lpType As Long, ByVal lpData As String, lpcbData As Long) As Long
'Query a Long Value
Private Declare Function RegQueryValueExLong Lib "advapi32.dll" Alias "RegQueryValueExA" (ByVal hKey As Long, ByVal lpValueName As String, ByVal lpReserved As Long, lpType As Long, lpData As Long, lpcbData As Long) As Long
'Query a NULL Value
Private Declare Function RegQueryValueExNULL Lib "advapi32.dll" Alias "RegQueryValueExA" (ByVal hKey As Long, ByVal lpValueName As String, ByVal lpReserved As Long, lpType As Long, ByVal lpData As Long, lpcbData As Long) As Long
'Enumerate Sub Keys
Private Declare Function RegEnumKey Lib "advapi32.dll" Alias "RegEnumKeyA" (ByVal hKey As Long, ByVal dwIndex As Long, ByVal lpName As String, ByVal cbName As Long) As Long
'Store a Value
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
'Delete Key
Private Declare Function RegDeleteKey Lib "advapi32.dll" Alias "RegDeleteKeyA" (ByVal hKey As Long, ByVal lpSubKey As String) As Long
Private Sub SaveValue(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, TEST MACHINE , REG_SZ, ByVal strData, Len(strData)
'close the key
RegCloseKey ret
End Sub
I need this code to create a registry KEY TEST and STR NAME PC with VALUE = TEST MACHINE
What am I doing wrong here.
Any help will be appreciated
Re: Adding/Editing a Registry Key
Try this to add keys:
vb Code:
Public Declare Function RegCreateKeyEx Lib "advapi32.dll" Alias "RegCreateKeyExA" (ByVal hKey As Integer, ByVal lpSubKey As String, ByVal Reserved As Integer, ByVal lpClass As String, ByVal dwOptions As Integer, ByVal samDesired As Integer, ByVal lpSecurityAttributes As Integer, ByRef phkResult As Integer, ByRef lpdwDisposition As Integer) As Integer
Public Const KEY_ALL_ACCESS = &HF003F
Public Const HKEY_LOCAL_MACHINE = &H80000002
Public Const HKEY_CURRENT_USER = &H80000001
Public Const HKEY_CURRENT_CONFIG = &H80000005
Public Const HKEY_CLASSES_ROOT = &H80000000
Public Const HKEY_USERS = &H80000003
Public Const REG_OPTION_NON_VOLATILE = 0 ' Key is preserved when system is rebooted
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim phkResult As Integer
Dim lpDisp As Integer
Dim keyName As String = "System\TestSubKey" 'name of key
Dim ret As Integer = RegCreateKeyEx(HKEY_LOCAL_MACHINE, keyName, 0, Nothing, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, 0, phkResult, lpDisp)
End Sub
Re: Adding/Editing a Registry Key
Try this to set values to the key:
vb Code:
Public Declare Function RegCreateKeyEx Lib "advapi32.dll" Alias "RegCreateKeyExA" (ByVal hKey As Integer, ByVal lpSubKey As String, ByVal Reserved As Integer, ByVal lpClass As String, ByVal dwOptions As Integer, ByVal samDesired As Integer, ByVal lpSecurityAttributes As Integer, ByRef phkResult As Integer, ByRef lpdwDisposition As Integer) As Integer
Public Declare Function RegSetValueEx Lib "advapi32.dll" Alias "RegSetValueExA" (ByVal hKey As Integer, ByVal lpValueName As String, ByVal Reserved As Integer, ByVal dwType As Integer, ByVal lpData As String, ByVal cbData As Integer) As Integer ' Note that if you declare the lpData parameter as String, you must pass it By Value.
Public Const KEY_ALL_ACCESS = &HF003F
Public Const HKEY_LOCAL_MACHINE = &H80000002
Public Const HKEY_CURRENT_USER = &H80000001
Public Const HKEY_CURRENT_CONFIG = &H80000005
Public Const HKEY_CLASSES_ROOT = &H80000000
Public Const HKEY_USERS = &H80000003
Public Const REG_OPTION_NON_VOLATILE = 0 ' Key is preserved when system is rebooted
Public Const REG_SZ = 1 ' Unicode nul terminated string
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim phkResult As Integer
Dim lpDisp As Integer
Dim keyName As String = "System\TestSubKey" 'name of key
Dim dataName As String = "STR NAME PC"
Dim dataVal As String = "TEST MACHINE"
Dim ret As Integer = RegCreateKeyEx(HKEY_LOCAL_MACHINE, keyName, 0, Nothing, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, 0, phkResult, lpDisp)
Dim ret2 As Integer = RegSetValueEx(phkResult, dataName, 0, REG_SZ, dataVal, dataVal.Length)
End Sub
Re: Adding/Editing a Registry Key
Thats what I have now, but I am trying to understand is to create a binary a STRING and DWARD key, I can create them separately on two separate scripts, but when I put them in one script, the STRING section will not work.
Thats what I have:
Code:
Option Explicit
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 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, ByVal lpData As String, ByVal cbData As Long) As Long
Private Const HKEY_LOCAL_MACHINE = &H80000002
Private Const REG_DWORD = 4
Dim nBufferKey As Long
Dim nVal As Long
Private Sub Form_Load()
nVal = 5
RegOpenKey HKEY_LOCAL_MACHINE, "SOFTWARE\test", nBufferKey
RegSetValueEx nBufferKey, "TEST", 0, REG_DWORD, nVal, Len(nVal)
RegCloseKey nBufferKey
' Unload Me
End Sub
Private Sub Command1_Click()
Set wshshell = CreateObject("WScript.Shell")
wshshell.RegWrite "HKEY_LOCAL_MACHINE\HKEY_LOCAL_MACHINE\SOFTWARE\test", "home"
End Sub
Private Sub Command2_Click()
Set wshshell = CreateObject("WScript.Shell")
wshshell.RegWrite "HKEY_LOCAL_MACHINE\SOFTWARE\test", "home2"
End Sub
Private Sub Command3_Click()
Set wshshell = CreateObject("WScript.Shell")
wshshell.RegWrite "HKEY_LOCAL_MACHINE\SOFTWARE\test", "Asia Pacific"
End Sub
I need to learn how to be able to create DWARD and STRING registry keys in one script
Any help is appreciated
Thanks
George
Re: Adding/Editing a Registry Key
:ehh:
What exactly are you trying to do ? Use the APIs or WSH ? They are 2 different technologies.
I firmly believe that if you are going to work with the registry, you must understand absolutely what you are doing, rather than simply be given the code and work blindly.
(In "Private Sub Command1_Click()" you have duplicated "HKEY_LOCAL_MACHINE"....)
If you intend to use WSH, check these links:
http://msdn.microsoft.com/archive/de...htm/wshTOC.asp
http://msdn.microsoft.com/archive/de...MthRegRead.asp
http://msdn.microsoft.com/archive/de...thRegWrite.asp
http://msdn.microsoft.com/archive/de...hRegDelete.asp
Also: http://www.vbforums.com/showthread.php?t=462193
Using the APIs, well that's a different kettle of fish. Search my previous posts and the forum (search link is near the top of the page).
Re: Adding/Editing a Registry Key
i need samething if someone can help thankz