|
-
Apr 6th, 2006, 12:44 AM
#1
Thread Starter
Frenzied Member
this function sets a particular application to run at windows startup
VB Code:
Option Explicit
Public 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
Public 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.
Public Declare Function RegDeleteValue Lib "advapi32.dll" Alias "RegDeleteValueA" _
(ByVal Hkey As Long, ByVal lpValueName As String) As Long
Public Declare Function RegCloseKey Lib "advapi32.dll" (ByVal Hkey As Long) As Long
Public 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
Public 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.
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
' 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 key" & vbCrLf & Err.Description
Exit Sub
End If
' See if we should run at startup.
If run_at_startup Then
' Create the key.
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
' Delete the value.
RegDeleteValue Hkey, app_name
End If
' Close the key.
RegCloseKey Hkey
Exit Sub
SetStartupError:
MsgBox Err.Number & " " & Err.Description
Exit Sub
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
-
Apr 6th, 2006, 07:57 AM
#2
New Member
Re: this function sets a particular application to run at windows startup
Im facing an error with this code.
I pasted it in the MDIForm of my project, when i play it says those arrays, functions..(etc) cant be public
:S
Thanx in advance
-
Apr 6th, 2006, 01:17 PM
#3
Re: this function sets a particular application to run at windows startup
That code is intended to be copied and pasted into a module. If you're going to put it in a form then change all the Delarations to Private, not Public.
-
Apr 7th, 2007, 11:41 AM
#4
Lively Member
Re: this function sets a particular application to run at windows startup
This code has many bugs! add a reference?
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|