Results 1 to 5 of 5

Thread: VB - Add/Remove your program to computer startup (through the registry)

  1. #1

    Thread Starter
    Super Moderator manavo11's Avatar
    Join Date
    Nov 2002
    Location
    Around the corner from si_the_geek
    Posts
    7,171

    VB - Add/Remove your program to computer startup (through the registry)

    Add two command buttons (Command1 , Command2) and add the following code :

    VB Code:
    1. Option Explicit
    2.  
    3. Private Declare Function RegCloseKey Lib "advapi32.dll" (ByVal Hkey As Long) As Long
    4. Private Declare Function RegCreateKey Lib "advapi32.dll" Alias "RegCreateKeyA" (ByVal Hkey As Long, ByVal lpSubKey As String, phkResult As Long) As Long
    5. Private Declare Function RegDeleteKey Lib "advapi32.dll" Alias "RegDeleteKeyA" (ByVal Hkey As Long, ByVal lpSubKey As String) As Long
    6. Private Declare Function RegDeleteValue Lib "advapi32.dll" Alias "RegDeleteValueA" (ByVal Hkey As Long, ByVal lpValueName As String) As Long
    7. Private Declare Function RegOpenKey Lib "advapi32.dll" Alias "RegOpenKeyA" (ByVal Hkey As Long, ByVal lpSubKey As String, phkResult As Long) As Long
    8. Private 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
    9. 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
    10. Private Const REG_SZ = 1                         ' Unicode nul terminated string
    11. Private Const REG_DWORD = 4                      ' 32-bit number
    12. Private Const ERROR_SUCCESS = 0&
    13.  
    14. Public Enum pvpHK
    15.     HKEY_CLASSES_ROOT = &H80000000
    16.     HKEY_CURRENT_USER = &H80000001
    17.     HKEY_LOCAL_MACHINE = &H80000002
    18.     HKEY_USERS = &H80000003
    19.     HKEY_PERFORMANCE_DATA = &H80000004
    20. End Enum
    21.  
    22. Private Const pvpRunHKey = "Software\Microsoft\Windows\CurrentVersion\Run"
    23.  
    24. Private Sub savestring(ByVal Hkey As Long, strPath As String, strValue As String, strData As String)
    25.     Dim keyhand As Long
    26.     Dim r As Long
    27.     r = RegCreateKey(Hkey, strPath, keyhand)
    28.     r = RegSetValueEx(keyhand, strValue, 0, REG_SZ, ByVal strData, Len(strData))
    29.     If r = 87 Then
    30.         DeleteValue Hkey, strPath, strValue
    31.     End If
    32.     r = RegCloseKey(keyhand)
    33. End Sub
    34.  
    35. Private Function DeleteValue(ByVal Hkey As Long, ByVal strPath As String, ByVal strValue As String)
    36.     Dim keyhand As Long
    37.     Dim r As Long
    38.     r = RegOpenKey(Hkey, strPath, keyhand)
    39.     r = RegDeleteValue(keyhand, strValue)
    40.     r = RegCloseKey(keyhand)
    41. End Function
    42.  
    43. Public Function RunAtStartup(sAppTitle As String, strsAppName As String)
    44.          savestring pvpHK.HKEY_CURRENT_USER, pvpRunHKey, sAppTitle, strsAppName
    45. End Function
    46.  
    47. Public Function RemoveFromStartup(sAppTitle As String, strsAppName As String)
    48.          DeleteValue pvpHK.HKEY_CURRENT_USER, pvpRunHKey, sAppTitle
    49. End Function
    50.  
    51. Private Sub Command1_Click()
    52.     RunAtStartup App.Title, App.Path & "\" & App.EXEName & ".EXE"
    53. End Sub
    54.  
    55. Private Sub Command2_Click()
    56.     RemoveFromStartup App.Title, App.Path & "\" & App.EXEName & ".EXE"
    57. End Sub
    58.  
    59. Private Sub Form_Load()
    60.  Command1.Caption = "Add To Startup"
    61.  Command2.Caption = "Remove from Startup"
    62. End Sub
    Last edited by manavo11; Mar 12th, 2003 at 04:32 PM.


    Has someone helped you? Then you can Rate their helpful post.

  2. #2
    PowerPoster
    Join Date
    Apr 2005
    Location
    Debug.Print
    Posts
    3,885

    Re: VB - Add/Remove your program to computer startup (through the registry)

    run-time error '28':
    out of stack space

  3. #3
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: VB - Add/Remove your program to computer startup (through the registry)

    Quote Originally Posted by BrailleSchool
    run-time error '28':
    out of stack space
    That generally means some kind of runaway recursion is going on.

    What did you run/click on that resulted in the error, the RunAtStartup or RemoveFromStartup?

  4. #4
    PowerPoster
    Join Date
    Apr 2005
    Location
    Debug.Print
    Posts
    3,885

    Re: VB - Add/Remove your program to computer startup (through the registry)

    Quote Originally Posted by Hack
    That generally means some kind of runaway recursion is going on.

    What did you run/click on that resulted in the error, the RunAtStartup or RemoveFromStartup?
    i put the code in my code window and ran it in the IDE and thats what i got.

  5. #5
    New Member
    Join Date
    May 2011
    Posts
    2

    Re: VB - Add/Remove your program to computer startup (through the registry)

    I want the program should auto hide from startup

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width