We need you!

We're working hard on the next version of Developer Fusion. Let us know what you think we should be up to!

Members

Technology Zones

Articles

Hosted By

MaximumASP

Info

Rated
Read 16,137 times

Related Categories

Boot RegKey API

mrjdesign

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


©2001 - All Rights Reserved - MRJ Design
The source code samples and information pertained within this document are considered copyrighted material and may not be re-distributed by electronic or other media in any form or fashion whatsoever, withou

Comments

  • oops

    Posted by Eas on 29 Apr 2006

    I tried something yesterday but instead of deleting the one value I deleted the whole key!
    oops please can somebody give the right code?

  • Delete Key

    Posted by Eas on 28 Apr 2006

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

  • Posted by jhela on 20 Jan 2003

    Thanks for Your advise.

  • Posted by James Crowley on 17 Jan 2003

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

  • RegOpenKeyEx doesn't work with VB.net but does VB6

    Posted by jhela on 17 Jan 2003

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