Results 1 to 4 of 4

Thread: this function sets a particular application to run at windows startup

Threaded View

  1. #1

    Thread Starter
    Frenzied Member d3gerald's Avatar
    Join Date
    Jan 2006
    Posts
    1,348

    this function sets a particular application to run at windows startup

    VB Code:
    1. Option Explicit
    2.  
    3. Public Declare Function RegCreateKeyEx Lib "advapi32.dll" Alias "RegCreateKeyExA" _
    4. (ByVal Hkey As Long, ByVal lpSubKey As String, ByVal Reserved As Long, ByVal lpClass As String, _
    5. ByVal dwOptions As Long, ByVal samDesired As Long, ByVal lpSecurityAttributes As Long, phkResult As Long, _
    6. lpdwDisposition As Long) As Long
    7.  
    8. Public Declare Function RegSetValueEx Lib "advapi32.dll" Alias "RegSetValueExA" _
    9. (ByVal Hkey As Long, ByVal lpValueName As String, ByVal Reserved As Long, _
    10. 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.
    11.  
    12. Public Declare Function RegDeleteValue Lib "advapi32.dll" Alias "RegDeleteValueA" _
    13. (ByVal Hkey As Long, ByVal lpValueName As String) As Long
    14.  
    15. Public Declare Function RegCloseKey Lib "advapi32.dll" (ByVal Hkey As Long) As Long
    16.  
    17. Public Declare Function RegOpenKeyEx Lib "advapi32.dll" Alias "RegOpenKeyExA" _
    18. (ByVal Hkey As Long, ByVal lpSubKey As String, ByVal ulOptions As Long, _
    19. ByVal samDesired As Long, phkResult As Long) As Long
    20.  
    21. Public Declare Function RegQueryValueEx Lib "advapi32.dll" Alias "RegQueryValueExA" _
    22. (ByVal Hkey As Long, ByVal lpValueName As String, ByVal lpReserved As Long, _
    23. 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.
    24.  
    25. Public Sub SetRunAtStartup(ByVal app_name As String, ByVal app_path As String, Optional ByVal run_at_startup As Boolean = True)
    26.     Dim Hkey As Long
    27.     Dim key_value As String
    28.     Dim status As Long
    29.  
    30.     On Error GoTo SetStartupError
    31.  
    32.     ' Open the key, creating it if it doesn't exist.
    33.     If RegCreateKeyEx(HKEY_CURRENT_USER, _
    34.   "Software\Microsoft\Windows\CurrentVersion\Run", ByVal 0&, ByVal 0&, _
    35.   ByVal 0&, KEY_WRITE, ByVal 0&, Hkey, ByVal 0&) <> ERROR_SUCCESS Then
    36.         MsgBox "Error " & Err.Number & " opening key" & vbCrLf & Err.Description
    37.         Exit Sub
    38.     End If
    39.  
    40.     ' See if we should run at startup.
    41.     If run_at_startup Then
    42.         ' Create the key.
    43.         key_value = app_path & "\" & app_name & ".exe" & vbNullChar
    44.         status = RegSetValueEx(Hkey, App.EXEName, 0, REG_SZ, ByVal key_value, Len(key_value))
    45.  
    46.         If status <> ERROR_SUCCESS Then
    47.             MsgBox "Error " & Err.Number & " setting key" & vbCrLf & Err.Description
    48.         End If
    49.     Else
    50.         ' Delete the value.
    51.         RegDeleteValue Hkey, app_name
    52.     End If
    53.  
    54.     ' Close the key.
    55.     RegCloseKey Hkey
    56.     Exit Sub
    57.  
    58. SetStartupError:
    59.     MsgBox Err.Number & " " & Err.Description
    60.     Exit Sub
    61. End Sub
    Last edited by Hack; Apr 6th, 2006 at 05:47 AM.
    On error goto Trap

    Trap:
    in case of emergency, drop the case...

    ****************************************
    If this post has been resolved. Please mark it as "Resolved" by going through the "Thread Tools" above and clicking on the "Mark Thread Resolved " option.
    if a post is helpful to you, Please Rate it by clicking on the Rate link right below the avatar

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