VB - Add/Remove your program to computer startup (through the registry)-VBForums
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
    Other side of town 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,843

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

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

  3. #3
    Super Moderator Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,335

    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?
    Please use [Code]your code goes in here[/Code] tags when posting code.
    When you have received an answer to your question, please mark it as resolved using the Thread Tools menu.
    Before posting your question, did you look here?
    Got a question on Linux? Visit our Linux sister site.
    I dont answer coding questions via PM or EMail. Please post a thread in the appropriate forum section.

    Creating A Wizard In VB.NET
    Paging A Recordset
    What is wrong with using On Error Resume Next
    Good Article: Language Enhancements In Visual Basic 2010
    Upgrading VB6 Code To VB.NET
    Microsoft MVP 2005/2006/2007/2008/2009/2010/2011/2012/Defrocked

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

    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
  •  



Featured


Click Here to Expand Forum to Full Width

Survey posted by VBForums.