Results 1 to 7 of 7

Thread: [RESOLVED] Visual Basic - Add application to startup with windows

  1. #1

    Thread Starter
    Addicted Member sinner0636's Avatar
    Join Date
    Sep 2009
    Posts
    233

    Resolved [RESOLVED] Visual Basic - Add application to startup with windows

    Hello
    i have been coding in realbasic for years switched to vb was wondering how to use the registry in vb to add my application to startup with a easy function like this below i wrote and realbasic that allows you to add your program to startup with a boolean any info would be great thanks!

    Module Code

    Code:
    Sub AddToStartUp(Name As String,Path As String, Enabled As Boolean)
      Try
        Dim Reg As RegistryItem
        'If CurrentUser Then 
        'Reg = New RegistryItem("HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Run")
        'Else
        Reg = New RegistryItem("HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Run") 'All Users
        'End
        Select Case Enabled
        Case True 'Add Item
          If Reg<>NIL Then
            Reg.Value( Name ) = Path
          End
        Case False 'Remove Item
          If Reg<>NIL Then
            Reg.Delete( Name )
          End
        End Select
      Catch RegistryAccessErrorException
        Exit
      End
    End Sub
    How to call the method
    Code:
    'add true to add application to the registry
    'add false to remove application from registry
    
    Call AddToStartUp("MyApplication",SpecialFolder.CurrentWorkingDirectory.NativePath+App.ExecutableFile.Name,True)

  2. #2
    PowerPoster
    Join Date
    Sep 2006
    Location
    Egypt
    Posts
    2,580

    Re: Visual Basic - Add application to startup with windows

    There are many VB6 classes to read/write the registry, here is a good one



  3. #3
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,482

    Re: Visual Basic - Add application to startup with windows

    I believe that is an option in the VB Package & Deployment wizard. IMO that is the place you would ask your client (person installing your app) to opt for it
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  4. #4

    Thread Starter
    Addicted Member sinner0636's Avatar
    Join Date
    Sep 2009
    Posts
    233

    Re: Visual Basic - Add application to startup with windows

    Heres what i come up with seems to work pretty good adds and removes the keys from the registry to make your program startup with windows!

    Pushbutton Code
    Code:
    'True - Add to registry
    'False - Remove from registry
    
    Call AddToStartUp("MyApp", "C:\Program Files (x86)\My Application", True)
    Module Code
    Code:
    Option Explicit
    Private Declare Function RegCreateKeyEx Lib "advapi32.dll" Alias "RegCreateKeyExA" (ByVal hKey As Long, ByVal lpSubKey As String, ByVal Reserved As Long, ByVal lpClass As String, ByVal dwOptions As Long, ByVal samDesired As Long, ByVal lpSecurityAttributes As Long, phkResult As Long, lpdwDisposition 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, lpData As Any, ByVal cbData As Long) As Long         ' Note that if you declare the lpData parameter as String, you must pass it By Value.
    Private Declare Function RegDeleteValue Lib "advapi32.dll" Alias "RegDeleteValueA" (ByVal hKey As Long, ByVal lpValueName As String) As Long
    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 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         ' Note that if you declare the lpData parameter as String, you must pass it By Value.
    
    Private Const READ_CONTROL = &H20000
    Private Const KEY_SET_VALUE = &H2
    Private Const KEY_CREATE_SUB_KEY = &H4
    Private Const STANDARD_RIGHTS_WRITE = (READ_CONTROL)
    Private Const SYNCHRONIZE = &H100000
    Private Const KEY_WRITE = ((STANDARD_RIGHTS_WRITE Or KEY_SET_VALUE Or KEY_CREATE_SUB_KEY) And (Not SYNCHRONIZE))
    Private Const STANDARD_RIGHTS_READ = (READ_CONTROL)
    Private Const KEY_ENUMERATE_SUB_KEYS = &H8
    Private Const KEY_NOTIFY = &H10
    Private Const KEY_QUERY_VALUE = &H1
    Private Const KEY_READ = ((STANDARD_RIGHTS_READ Or KEY_QUERY_VALUE Or KEY_ENUMERATE_SUB_KEYS Or KEY_NOTIFY) And (Not SYNCHRONIZE))
    Private Const ERROR_SUCCESS = 0&
    Private Const HKEY_CURRENT_USER = &H80000001
    Private Const REG_SZ = 1
    Private Const EndOfLine = vbCrLf
    
    Public Sub AddToStartUp(ByVal Name As String, ByVal Path As String, Optional ByVal Enabled As Boolean)
    Dim hKey As Long
    Dim RegValue As String
    Dim Result As Long
    On Error GoTo RegError
    'Open the key, creating it if it doesn't exist.
    If RegCreateKeyEx(HKEY_CURRENT_USER, "Software\Microsoft\Windows\CurrentVersion\Run", ByVal 0&, ByVal 0&, ByVal 0&, KEY_WRITE, ByVal 0&, hKey, ByVal 0&) <> ERROR_SUCCESS Then
    MsgBox "Error " & Err.Number & " Opening RegKey" & EndOfLine & Err.Description
    Exit Sub
    End If
    Select Case Enabled
    'Create key
    Case True
    RegValue = Chr$(34) + Path & "\" & Name & ".exe" + Chr$(34)
    Result = RegSetValueEx(hKey, Name, 0, REG_SZ, ByVal RegValue, Len(RegValue))
    If Result <> ERROR_SUCCESS Then
    MsgBox "Error " & Err.Number & " Creating RegKey" & EndOfLine & Err.Description
    End If
    Case False
    'Delete Key
    RegDeleteValue hKey, Name
    End Select
    ' Close the key.
    RegCloseKey hKey
    Exit Sub
    RegError:
    MsgBox Err.Number & " " & Err.Description
    Exit Sub
    End Sub

  5. #5

    Thread Starter
    Addicted Member sinner0636's Avatar
    Join Date
    Sep 2009
    Posts
    233

    Re: Visual Basic - Add application to startup with windows

    Say i wanted to add a checkbox that loads and saves the checkboxes value to know if the user wants to have there program to startup with windows or not when the app is loaded get the value to the checkbox would this work with visual basics built registry save get settings for to save or load a check boxes value?

    Get/Save Registry Settings
    Code:
    Private Sub Form_Load()
    if GetSetting(App.EXEName, "STARTUPCHECKBOXVALUE", "CheckboxValue", Check1.Value) = "True" then
    Check1.Value=vbChecked 
    Elseif GetSetting(App.EXEName, "STARTUPCHECKBOXVALUE", "CheckboxValue", Check1.Value) = "False" then
    Check1.Value=vbUnChecked 
    End If
    End Sub
    
    Private Sub Form_Unload(Cancel As Integer)
    SaveSetting App.EXEName, "STARTUPVALUE", "CheckboxValue", Check1.Value
    End Sub
    Checkbox Click

    Code:
    Private Sub Check1_Click()
    If Check1.Value = vbChecked Then
    Call AddToStartUp("MyApp", "C:\Program Files (x86)\My Application", True)
    
    ElseIf Check1.Value = vbUnchecked Then
    Call AddToStartUp("MyApp", "C:\Program Files (x86)\My Application", False)
    End If
    End Sub
    Last edited by sinner0636; Apr 13th, 2015 at 03:17 PM.

  6. #6
    PowerPoster
    Join Date
    Sep 2006
    Location
    Egypt
    Posts
    2,580

    Re: Visual Basic - Add application to startup with windows

    Yes, it should work but it didn't because you made a mistake, you are saving Check1.Value under a section named "STARTUPVALUE" while reading from different section named "STARTUPCHECKBOXVALUE"

    Try this
    Code:
    Private Sub Form_Load()
        
        If CBool(GetSetting(App.EXEName, "STARTUPCHECKBOXVALUE", "CheckboxValue", Check1.Value)) Then
            Check1.Value = vbChecked
        Else
            Check1.Value = vbUnchecked
        End If
    
    End Sub
    
    Private Sub Form_Unload(Cancel As Integer)
        SaveSetting App.EXEName, "STARTUPCHECKBOXVALUE", "CheckboxValue", Check1.Value
    End Sub



  7. #7

    Thread Starter
    Addicted Member sinner0636's Avatar
    Join Date
    Sep 2009
    Posts
    233

    Re: Visual Basic - Add application to startup with windows

    Code:
    Public Function SaveToRegistry(AppName As String, FolderName As String, KeyName As Variant, KeyData As Variant)
    SaveSetting AppName, FolderName, KeyName, KeyData
    End Function
    
    Public Function LoadFromRegistry(AppName As String, FolderName As String, KeyName As Variant, KeyData As Variant) As String
    LoadFromRegistry = GetSetting(AppName, FolderName, KeyName, KeyData)
    End Function
    did not notice that the code u gave me worked thanks
    Last edited by sinner0636; May 19th, 2015 at 11:16 AM.

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