Library code snippets
Boot RegKey API
By Mike J, published on 09 Mar 2002
This example shows you how to create a registry key, ie. in the Form_load statement of a form so your app starts with windows every time the user reboots the machine. Run the code as such;
Call SetBootRegKey(HKEY_LOCAL_MACHINE, _
"YourAppName", _
App.Path & "\" & App.EXEName & ".exe", _
"Software\Microsoft\Windows\CurrentVersion\Run")
'use HKEY_CURRENT_USER for single/current user only
'use HKEY_LOCAL_MACHINE for all users is hKey
To remove the value temporarily without deleting the key from the registry simply call it as such...
Call SetBootRegKey(HKEY_LOCAL_MACHINE, _
"YourAppName", _
"", _
"Software\Microsoft\Windows\CurrentVersion\Run")
Place the following in a module in your project!
Option Explicit
'THIS MODLUE PERFORMS SPECIFIC API RELATED FUNCTIONS
'SYSTEM DEFINED CONSTANTS, TYPES AND VARIABLES
'=====================================================================
'FOR REG KEY SETTINGS
Public Const HKEY_CURRENT_USER = &H80000001
Public Const HKEY_LOCAL_MACHINE = &H80000002
Public Const KEY_WRITE = &H20006
Public Const REG_SZ = 1
'END FOR REG KEY SETTINGS
'FOR REGISTRY SETTINGS
Public 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
Public Declare Function RegCloseKey Lib "advapi32.dll" (ByVal hKey As Long) As Long
Public 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
'END FOR REG SETTINGS
'RUN APP AT BOOT ALL THE TIME WITH THIS CODE.
'RUN PREFERABLY ON FIRST START OF PROGRAM.
'=====================================================================
Function SetBootRegKey(ByVal hKey As Long, _
ByVal MinorKey As String, _
ByVal strKeyValue As String, _
ByVal subkey As String)
Dim hregkey As Long, stringbuffer As String ', subkey As String,
Dim retval As Long ', strKeyValue
'strKeyValue = App.Path & "\" & App.EXEName & ".exe"
'subkey = "Software\Microsoft\Windows\CurrentVersion\Run"
'use HKEY_CURRENT_USER for single/current user only
'use HKEY_LOCAL_MACHINE for all users is hKey
'MinorKey = the value that appears for run command
retval = RegOpenKeyEx(hKey, subkey, 0, _
KEY_WRITE, hregkey)
If retval <> 0 Then
Debug.Print "Can't open the subkey"
'OPTIONALLY ADD ERROR CODE HERE
Exit Function
End If
stringbuffer = strKeyValue & vbNullChar
retval = RegSetValueEx(hregkey, MinorKey, 0, REG_SZ, _
ByVal stringbuffer, Len(stringbuffer))
RegCloseKey hregkey
End Function
Related articles
Related discussion
-
VB6, SQL 2005 & DMO
by elajaunie3 (1 replies)
-
sending sms from pc
by sriraj20074 (0 replies)
-
Automating Excel from VB6.0
by epurdy (0 replies)
-
VB6 system conversion using VBA to Word 2007
by b.macgregor@vodamail.co.za (0 replies)
-
video not working with visual basic
by Jupiter 2 (9 replies)
Related podcasts
-
Christian Beauclair
14 mai 2008 (�mission #0074) ::.Christian Beauclair: Stratégies de migration VB6 vers .NET Nous discutons avec Christian Beauclair des stratégies de migration VB6 vers .NET. Entre autres, nous discutons comment utiliser le "VB 6 Code Advisor" et le "Interop Forms Toolkit" pour ajouter la puiss...
oops please can somebody give the right code?
Works Great but
Can someone show me how to delete a reg Key?
Thanks for Your advise.
You no longer need to use API to access the registry in VB.NET. Instead, take a look at the Microsoft.Win32 namespace, which contains Registry functions. For example,
RegistryKey myKey = Registry.LocalMachine.OpenSubKey("Software\MySoftware");
MessageBox.Show (webZincKey.GetValue("Serial").ToString());
displays a message box displaying the value of the Serial item in the HKEYLOCALMACHINE\Software\MySoftware key.
I'm using code like this by VB6 and it works perfect but not by VB.NET. Why?
Option Explicit
Private Declare Function RegCloseKey Lib "advapi32.dll" (ByVal hKey As Long) As Long
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 RegSetValueEx Lib "advapi32.dll" Alias "RegSetValueExA" (ByVal hKey As Long, ByVal lpValueName As String, ByVal Reserved As Long, ByVal dwType As Long, ByVal lpValue As String, ByVal cbData As Long) As Long
Private Sub Command1Click()
Const REGSZ As Long = 1
Const HKEYLOCALMACHINE = &H80000002
Const KEYSETVALUE = &H2
Dim sKeyName As String
Dim lRetVal As Long
Dim hregKey As Long
sKeyName = "SOFTWARE\Wiesemann & Theis\Com-server\Com2"
lRetVal = RegOpenKeyEx(HKEYLOCALMACHINE, sKeyName, 0, KEYSETVALUE, hregKey)
Dim lValueType As Long
Dim vValueSetting As String
Dim sValueName As String
sValueName = "IpAddress"
lValueType = REG_SZ
vValueSetting = "123.45.67.89" & Chr$(0)
lRetVal = RegSetValueEx(hregKey, sValueName, 0, lValueType, vValueSetting, Len(vValueSetting))
RegCloseKey (hregKey)
End Sub
This thread is for discussions of Boot RegKey API.