Library code snippets

Register Program to Start Up Automatically

This sample code shows you how to register your visual basic program on the windows startup registry. First of all, copy and past all below code to a module.

Option Explicit
Public Declare Function RegCloseKey Lib "advapi32.dll" (ByVal Hkey As Long) As Long
Public Declare Function RegCreateKey Lib "advapi32.dll" Alias "RegCreateKeyA" (ByVal Hkey As Long, ByVal lpSubKey As String, phkResult As Long) As Long
Public Declare Function RegDeleteKey Lib "advapi32.dll" Alias "RegDeleteKeyA" (ByVal Hkey As Long, ByVal lpSubKey As String) As Long
Public Declare Function RegDeleteValue Lib "advapi32.dll" Alias "RegDeleteValueA" (ByVal Hkey As Long, ByVal lpValueName As String) As Long
Public Declare Function RegOpenKey Lib "advapi32.dll" Alias "RegOpenKeyA" (ByVal Hkey As Long, ByVal lpSubKey As String, phkResult As Long) As Long
Public 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
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
Public Const REG_SZ = 1 ' Unicode nul terminated String
Public Const REG_DWORD = 4 ' 32-bit number
Public Const HKEY_CLASSES_ROOT = &H80000000
Public Const HKEY_CURRENT_USER = &H80000001
Public Const HKEY_LOCAL_MACHINE = &H80000002
Public Const HKEY_USERS = &H80000003
Public Const HKEY_PERFORMANCE_DATA = &H80000004
Public Const ERROR_SUCCESS = 0&


Public Sub SaveString(Hkey As Long, strPath As String, strValue As String, strdata As String)
   Dim keyhand As Long
   Dim r As Long
   r = RegCreateKey(Hkey, strPath, keyhand)
   r = RegSetValueEx(keyhand, strValue, 0, REG_SZ, ByVal strdata, Len(strdata))
   r = RegCloseKey(keyhand)
End Sub

Next, to make the entry in the registry, use code like this (this only really needs to be done once, but could be called in Form_Load each time your app starts):

SaveString(HKEY_LOCAL_MACHINE, "SOFTWARE\Microsoft\Windows\CurrentVersion\Run", _
    App.ExeName, App.Path & "\" & App.ExeName & ".exe")

Hope this code will make you programming life easy...:D

AddThis

Comments

  1. 29 May 2005 at 23:59

    Hey Guys


    How to convert picture files to binary?


    Please help me!

  2. 14 Mar 2004 at 22:19

    yap u can do that oso,
    before that u may need to know how to del the registry key,(the question as amoibabe asked)...D

  3. 14 Mar 2004 at 22:14

    Would it be easier to run a check of the change on start up and if present not run again

  4. 25 Jan 2004 at 19:40

    sorry man, i jz finish my chinese new year...
    so may be late reply ur question, hope u can forgive me...


    to delete the register key u can using the following code

    Code:

    r = RegDeleteValue(keyhand, strValue)


    above code will allow u to delete the register key.
    you jz replace it on to the savestring before

    Code:
    regclosekey(keyhand)


    or u can create a new sub for it...


    hope this will help u...

  5. 25 Jan 2004 at 10:18

    Well... It seems i failed on using RegDeleteKey properly. Can you provide me with some code please?


    Thank you very much for your help, i really appreciate it...


  6. 23 Jan 2004 at 06:04

    Thank you for your help, i will try to do it so...


  7. 23 Jan 2004 at 04:31

    this will register when the user run the program, although u have use the 'msconfig' to del the startup,
    it will stil keep on register the program when u restart or reopen the program


    for example:
    u have a x.exe with is using this code to register to startup
    when u try to using the 'msconfig' to del the register value,
    but when u reopen the x.exe the program will register to the startup


    this was different with the installing service
    because the installing service will jz register it once, after the user using it to del the register value
    it won startup anymore.


    understand now?

  8. 23 Jan 2004 at 04:26

    yeah as James say, u can del it by using the regdeletekey
    or u can use the 'msconfig' in win9X and winXP to delete it...

  9. 22 Jan 2004 at 19:54

    you'd probably want to take a look at the RegDeleteKey api function (which is also listed) - and just call that on the same key that you create in this code?

  10. 22 Jan 2004 at 08:25

    This is very intresting, but what about deleting my entry from the registry so that my program won't begin at start up?


  11. 21 Jan 2004 at 10:49

    What do you as the difference between this solution and creating and installing a service?  When would you choose one over the other?

Leave a comment

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

Related discussion