Library code snippets

Boot RegKey API

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

Comments

  1. 29 Apr 2006 at 12:17
    I tried something yesterday but instead of deleting the one value I deleted the whole key!
    oops please can somebody give the right code?


  2. 28 Apr 2006 at 12:22

    Works Great but
    Can someone show me how to delete a reg Key?




  3. 20 Jan 2003 at 00:06

    Thanks for Your advise.

  4. 17 Jan 2003 at 08:38

    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.

  5. 17 Jan 2003 at 06:04

    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 REG
    SZ 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

  6. 13 Oct 2002 at 00:01
    Didn't work for me......
  7. 01 Jan 1999 at 00:00

    This thread is for discussions of Boot RegKey API.

Leave a comment

Sign in or Join us (it's free).

Mike J
AddThis

Related discussion

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...

We'd love to hear what you think! Submit ideas or give us feedback