Results 1 to 11 of 11

Thread: Run on start up?

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    May 2009
    Posts
    876

    Run on start up?

    Hi everyone, i followed a thread on this forum on how to make my app start on windows start up.

    the problem is it isnt working. the code is below. do i actually need to change or add anything. the code below as all exactly what i added.

    it saves and keeps the check box ticked it i say i want it to run. but it actually dont start on start up :S


    thanks!

    Code:
    Option Explicit
    
    Private Sub Form_Load()
    
        If WillRunAtStartup(App.EXEName) Then
            Start_Up.Value = 1
        Else
            Start_Up.Value = 0
        End If
    
    End Sub
    
    Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
    
        Dim SwW As Boolean
        
        If Start_Up.Value = 1 Then
            SwW = True
        Else
            SwW = False
        End If
        
        SetRunAtStartup App.EXEName, App.Path, SwW
    
    End Sub

    Bas Module

    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
    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
    
    Private Const READ_CONTROL As Long = &H20000
    Private Const KEY_SET_VALUE As Long = &H2
    Private Const KEY_CREATE_SUB_KEY As Long = &H4
    Private Const STANDARD_RIGHTS_WRITE As Long = (READ_CONTROL)
    Private Const SYNCHRONIZE As Long = &H100000
    Private Const KEY_WRITE As Long = ((STANDARD_RIGHTS_WRITE Or KEY_SET_VALUE Or KEY_CREATE_SUB_KEY) And (Not SYNCHRONIZE))
    
    Private Const STANDARD_RIGHTS_READ As Long = (READ_CONTROL)
    Private Const KEY_ENUMERATE_SUB_KEYS As Long = &H8
    Private Const KEY_NOTIFY As Long = &H10
    Private Const KEY_QUERY_VALUE As Long = &H1
    Private Const KEY_READ As Long = ((STANDARD_RIGHTS_READ Or KEY_QUERY_VALUE Or KEY_ENUMERATE_SUB_KEYS Or KEY_NOTIFY) And (Not SYNCHRONIZE))
    
    Private Const ERROR_SUCCESS As Long = 0&
    Private Const HKEY_CURRENT_USER As Long = &H80000001
    Private Const REG_SZ As Long = 1
    Public Function WillRunAtStartup(ByVal App_Name As String) As Boolean
    
      Dim hKey As Long
      Dim value_type As Long
    
        If RegOpenKeyEx(HKEY_CURRENT_USER, _
                        "Software\Microsoft\Windows\CurrentVersion\Run", _
                        0, KEY_READ, hKey) = ERROR_SUCCESS _
                        Then
            WillRunAtStartup = _
                               (RegQueryValueEx(hKey, App_Name, _
                               ByVal 0&, value_type, ByVal 0&, ByVal 0&) = _
                               ERROR_SUCCESS)
    
            RegCloseKey hKey
          Else
            WillRunAtStartup = False
        End If
    
    End Function
    
    Public Sub SetRunAtStartup(ByVal App_Name As String, ByVal app_path As String, Optional ByVal run_at_startup As Boolean = True)
    
      Dim hKey As Long
      Dim key_value As String
      Dim status As Long
    
        On Error GoTo SetStartupError
    
        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 key" & vbCrLf & Err.Description
            Exit Sub
        End If
    
        If run_at_startup Then
    
            key_value = app_path & "\" & App_Name & ".exe" & vbNullChar
            status = RegSetValueEx(hKey, App.EXEName, 0, REG_SZ, _
                     ByVal key_value, Len(key_value))
            If status <> ERROR_SUCCESS Then
                MsgBox "Error " & Err.Number & " setting key" & vbCrLf & Err.Description
            End If
    
          Else
    
            RegDeleteValue hKey, App_Name
    
        End If
    
        RegCloseKey hKey
    
    exit_sub:
    Exit Sub
    
    SetStartupError:
        MsgBox Err.Number & " " & Err.Description
        Resume exit_sub
        
    End Sub

  2. #2
    Hyperactive Member deathfxu's Avatar
    Join Date
    Mar 2009
    Location
    USA
    Posts
    279

    Re: Run on start up?

    There is a "Startup" folder in your Start Menu (usually located at "C:\Documents and Settings\[username]\Start Menu\Programs\Startup" in XP) and any programs located in that folder are automatically run by Windows when it is starting up.

    You can also add your program's path to one of the Windows startup registry folders. The registry startup paths are:

    [These will make your program start when Windows starts, for all users]
    HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Run
    HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\RunOnce
    HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\RunServices
    HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\RunServicesOnce

    [These will make your program start when Windows starts, for the current user only]
    HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Run
    HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\RunOnce
    HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\RunOnceEx

    Also notice the "Run Once" folders, so if you want your program to run when Windows starts up just once, then you have that option as well.

    As for the code you were using, I would avoid that if possible and use one of the conventional methods I just listed.

    Enjoy!
    Last edited by deathfxu; May 22nd, 2009 at 02:44 PM. Reason: added info from a link and removed the link. :D

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    May 2009
    Posts
    876

    Re: Run on start up?

    i see! thankyou very much, but how would i make it so if u check a check box then it will start on startup where as if its unchecked then it wont?

    thanks mate.

  4. #4
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: Run on start up?

    When the check box changes, see if it is set or cleared.
    If it is set, write to the registry to put the app into the Run node.
    If it has been cleared, REMOVE the setting from the Run node in the registry.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  5. #5

    Thread Starter
    Fanatic Member
    Join Date
    May 2009
    Posts
    876

    Re: Run on start up?

    hey thanks ;p

    can you just explain that in newbie language? i dont understand any of that aha sorry :>S!

  6. #6
    Hyperactive Member deathfxu's Avatar
    Join Date
    Mar 2009
    Location
    USA
    Posts
    279

    Re: Run on start up?

    If you don't know how to use the registry (which it sounds like is the case), here is a great tutorial by Koolsid that starts very basic and gets quite in depth.

    For more information about creating a shortcut on a computer, see this, or here for some cut-n-paste subroutines that can be easily modified for what you are trying to do.


    I found another great link after I posted this. This code will also help you create a shortcut (that you can add to the startup folder).
    Last edited by deathfxu; May 22nd, 2009 at 03:05 PM. Reason: adding more links

  7. #7
    PowerPoster isnoend07's Avatar
    Join Date
    Feb 2007
    Posts
    3,237

    Re: Run on start up?

    I created a startup in the past by using a special folder CSIDL_COMMON_STARTUP
    http://www.codenewsgroups.net/group/...topic7381.aspx
    Waiting for a full featured smart phone with out marrying a provider
    Go Android
    Go raiders

  8. #8

    Thread Starter
    Fanatic Member
    Join Date
    May 2009
    Posts
    876

    Re: Run on start up?

    thanks, the thing is i have a installer that will install every file needed e.t.c

    Plus i want the option to have it run on windows start up or not?

    Any other suggestions.

    THanks!

  9. #9
    Hyperactive Member deathfxu's Avatar
    Join Date
    Mar 2009
    Location
    USA
    Posts
    279

    Re: Run on start up?

    Is it a self-made installer, or a premade install packager?

  10. #10

    Thread Starter
    Fanatic Member
    Join Date
    May 2009
    Posts
    876

    Re: Run on start up?

    a pre made installer "inno setup"

    thanks!

  11. #11
    Hyperactive Member deathfxu's Avatar
    Join Date
    Mar 2009
    Location
    USA
    Posts
    279

    Re: Run on start up?

    I haven't used Inno myself, but most times you can't add an option for that in the installer. It's probably going to have to be an option in the program itself, using one of the two options listed above (putting a shortcut in the Startup folder or adding an entry in one of the Startup registry paths).
    Last edited by deathfxu; May 23rd, 2009 at 11:08 AM. Reason: clarification

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