Results 1 to 10 of 10

Thread: How can I make my program start when Windows does?

  1. #1

    Thread Starter
    New Member
    Join Date
    Jul 2007
    Posts
    8

    How can I make my program start when Windows does?

    You all were so helpful last time, please help a novice programmer once more . I just need my prog to start when Win XP boots. I know you can add a registry value to CurrentVersion/Run or something like that. I can't recall it off the top of my head, but if I ran regedit I'm sure I could find the right place again. The thing is, I heard VB can't write keys to the registry in that area, or most areas in the registry.

    Thanks for any help,

    Ash

  2. #2
    Addicted Member xavierjohn22's Avatar
    Join Date
    Oct 2006
    Location
    Approx. 4921' and 3.11" asl
    Posts
    249

    Re: How can I make my program start when Windows does?

    If you are an admin on your PC you can write in the registry almost anything, anyways i'll go look into the run app on startup in statics codebank, wait a second.

  3. #3
    Addicted Member xavierjohn22's Avatar
    Join Date
    Oct 2006
    Location
    Approx. 4921' and 3.11" asl
    Posts
    249

    Re: How can I make my program start when Windows does?

    n the HKEY_CURRENT_USER side

    Code:
        modRegistry.WriteRegKey REG_SZ, _
        HKEY_CURRENT_USER, _
        "Software\Microsoft\Windows\CurrentVersion\Run", _
        "Dani's NUKETemp", _
        (App.Path & "\" & App.EXEName & ".exe")

    on the HKLM side

    Code:
            SaveString HKEY_LOCAL_MACHINE, "SOFTWARE\MICROSOFT\WINDOWS\CURRENTVERSION\RUN", "Transparent Analog Clock", App.Path & "\" & App.EXEName & ".exe"
    You just have to write or look for the registry functions to be able to write.

    here's one in a module.

    Code:
    Option Explicit
    
    Public Const REG_SZ = 1 ' Unicode nul terminated string
    Public Const REG_BINARY = 3 ' Free form binary
    
    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 HKEY_CURRENT_CONFIG = &H80000005
    Public Const HKEY_DYN_DATA = &H80000006
    
    Private Declare Function RegCloseKey Lib "advapi32.dll" (ByVal hKey As Long) As Long
    Private Declare Function RegCreateKey Lib "advapi32.dll" Alias "RegCreateKeyA" (ByVal hKey As Long, ByVal lpSubKey As String, phkResult 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 RegOpenKey Lib "advapi32.dll" Alias "RegOpenKeyA" (ByVal hKey As Long, ByVal lpSubKey As String, 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 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 Function RegQueryStringValue(ByVal hKey As Long, ByVal strValueName As String) As String
        Dim lResult As Long, lValueType As Long, strBuf As String, lDataBufSize As Long
        'retrieve nformation about the key
        lResult = RegQueryValueEx(hKey, strValueName, 0, lValueType, ByVal 0, lDataBufSize)
        If lResult = 0 Then
            If lValueType = REG_SZ Then
                'Create a buffer
                strBuf = String(lDataBufSize, Chr$(0))
                'retrieve the key's content
                lResult = RegQueryValueEx(hKey, strValueName, 0, 0, ByVal strBuf, lDataBufSize)
                If lResult = 0 Then
                    'Remove the unnecessary chr$(0)'s
                    RegQueryStringValue = Left$(strBuf, InStr(1, strBuf, Chr$(0)) - 1)
                End If
            ElseIf lValueType = REG_BINARY Then
                Dim strData As Integer
                'retrieve the key's value
                lResult = RegQueryValueEx(hKey, strValueName, 0, 0, strData, lDataBufSize)
                If lResult = 0 Then
                    RegQueryStringValue = strData
                End If
            End If
        End If
    End Function
    Public Function GetString(hKey As Long, strPath As String, strValue As String)
        Dim Ret
        'Open the key
        RegOpenKey hKey, strPath, Ret
        'Get the key's content
        GetString = RegQueryStringValue(Ret, strValue)
        'Close the key
        RegCloseKey Ret
    End Function
    Public Sub SaveString(hKey As Long, strPath As String, strValue As String, strData As String)
        Dim Ret
        'Create a new key
        RegCreateKey hKey, strPath, Ret
        'Save a string to the key
        RegSetValueEx Ret, strValue, 0, REG_SZ, ByVal strData, Len(strData)
        'close the key
        RegCloseKey Ret
    End Sub
    Public Sub SaveStringLong(hKey As Long, strPath As String, strValue As String, strData As String)
        Dim Ret
        'Create a new key
        RegCreateKey hKey, strPath, Ret
        'Set the key's value
        RegSetValueEx Ret, strValue, 0, REG_BINARY, CByte(strData), 4
        'close the key
        RegCloseKey Ret
    End Sub
    Public Sub DelSetting(hKey As Long, strPath As String, strValue As String)
        Dim Ret
        'Create a new key
        RegCreateKey hKey, strPath, Ret
        'Delete the key's value
        RegDeleteValue Ret, strValue
        'close the key
        RegCloseKey Ret
    End Sub

  4. #4
    Interweb adm/o/distrator Paul M's Avatar
    Join Date
    Nov 2006
    Location
    Australia, Melbourne
    Posts
    2,306

    Re: How can I make my program start when Windows does?

    You could do it much more simply and do something like the following.
    vb Code:
    1. Public Sub AddToStartup()
    2. Dim Reg As Object
    3. Set Reg = CreateObject("Wscript.shell")
    4. Reg.RegWrite "HKEY_LOCAL_MACHINE\SOFTWARE\MICROSOFT\WINDOWS\CURRENTVERSION\RUN\" & App.EXEName, App.Path & "\" & App.EXEName & ".exe"
    5. End Sub
    6.  
    7. Public Sub RemoveFromStartup()
    8. Dim Reg As Object
    9. Set Reg = CreateObject("Wscript.Shell")
    10. Reg.RegDelete "HKEY_LOCAL_MACHINE\SOFTWARE\MICROSOFT\WINDOWS\CURRENTVERSION\RUN\" & App.EXEName
    11. End Sub

  5. #5

    Thread Starter
    New Member
    Join Date
    Jul 2007
    Posts
    8

    Re: How can I make my program start when Windows does?

    Thanks so much for all your replies, I'll try it out 2morr, when I'm sober again

  6. #6

    Thread Starter
    New Member
    Join Date
    Jul 2007
    Posts
    8

    Re: How can I make my program start when Windows does?

    Hey, thanks for all the replies, I wanted to point out that I tried the last reply first because it seemed the easist to implement. I added it in it's own sub obviously, then I added "AddToStartup" to Form_Load to execute the subroutine when the program starts. Unfourtunately, when I tried to run it, it says "User Defined Type Not Defined" and highlights the line that says "Dim Reg as ObjectSet". What can I do to correct this?


    Thanks again,

    Ash

  7. #7
    Addicted Member xavierjohn22's Avatar
    Join Date
    Oct 2006
    Location
    Approx. 4921' and 3.11" asl
    Posts
    249

    Re: How can I make my program start when Windows does?

    "Dim Reg as ObjectSet" to "Dim Reg as Object"

    i tried it here, no problem:

    Code:
    Option Explicit
    
    Private Sub Form_Load()
    AddToStartup
    End Sub
    
    
          Public Sub AddToStartup()
          Dim Reg As Object
          Set Reg = CreateObject("Wscript.shell")
          Reg.RegWrite "HKEY_LOCAL_MACHINE\SOFTWARE\MICROSOFT\WINDOWS\CURRENTVERSION\RUN\" & App.EXEName, App.Path & "\" & App.EXEName & ".exe"
          End Sub
          Public Sub RemoveFromStartup()
          Dim Reg As Object
          Set Reg = CreateObject("Wscript.Shell")
          Reg.RegDelete "HKEY_LOCAL_MACHINE\SOFTWARE\MICROSOFT\WINDOWS\CURRENTVERSION\RUN\" & App.EXEName
          End Sub
    
    Private Sub Form_Unload(Cancel As Integer)
    RemoveFromStartup
    End Sub
    Last edited by xavierjohn22; Jul 22nd, 2007 at 08:28 PM.

  8. #8
    Fanatic Member schoolbusdriver's Avatar
    Join Date
    Jan 2006
    Location
    O'er yonder
    Posts
    1,020

    Re: How can I make my program start when Windows does?

    When you create apps that run at startup, you should ALWAYS give the user the option to prevent it running.
    Code:
    Option Explicit
       
    'Form level code. Has a command button (cmdExit)and a checkbox (Check1).
    
    'Registry subkey.
    Private Const SUBKEY = "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Run\"
    
    'Value name...
    Private Const VALUE_NAME = "MyApp"
    
    Private Sub cmdExit_Click()
       Unload Me
    End Sub
    
    Private Sub Form_Load()
    'Get the checkbox's previous setting. Default is to NOT start on boot-up.
       Check1.Value = GetSetting(App.EXEName, "Startup", "Run At Bootup", 0)
    End Sub
    
    Private Sub Form_Unload(Cancel As Integer)
       Dim WshShell As Object
       Dim strLocation As String
    
    'Create the scripting object.
       Set WshShell = CreateObject("WScript.Shell")
    'Get the path and file name.
       strLocation = App.Path & "\" & App.EXEName & ".exe"
    'Set up some error handling. Could be more elaborate... :)
       On Error GoTo ErrWSH
    'Set the registry value.
       Select Case Check1.Value
       Case 0 'Unchecked - delete it.
          WshShell.RegDelete SUBKEY & VALUE_NAME
       Case 1 'Checked - write it.
          WshShell.RegWrite SUBKEY & VALUE_NAME, strLocation, "REG_SZ"
       End Select
    'Remember the setting.
       SaveSetting App.EXEName, "Startup", "Run At Bootup", Check1.Value
    'Destroy the scripting object.
       Set WshShell = Nothing
    'Exit if there's no errors.
       Exit Sub
    ErrWSH:
    'Raise any error messages here if you want to...
    'Destroy the scripting object.
       Set WshShell = Nothing
    End Sub

  9. #9

    Thread Starter
    New Member
    Join Date
    Jul 2007
    Posts
    8

    Re: How can I make my program start when Windows does?

    Thanks again for each and every one of your replies, I got it going now

  10. #10
    PowerPoster Fazi's Avatar
    Join Date
    Aug 2005
    Location
    Underworld
    Posts
    2,525

    Re: How can I make my program start when Windows does?

    i have seen programs that left its startup entry uncleaned after the uninstall. so dont forget to delete your startup value from the registry using your setup program when uninstalling.

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