Joined: 10 Mar 2004 Posts: 376
Location: Savar, Dhaka
Post subject: Visual Basic Run Your Program at start up
How to Run Your Program At Startup with Visual basic / VB
The following code will run your program at startup, without making a shortcut in the startup folder. This code is actually put your program in the registery in Software\Microsoft\Windows\CurrentVersion\Run.
To make your program run at startup only once (only at the first time you startup the computer after running this code) You should put your program at 'Software\Microsoft\Windows\CurrentVersion\RunOnce. To do so
>> Replace the 'two appearance of '...CurrentVersion\Run' in this code with '...CurrentVersion\RunOnce' Add a module to your project (In the menu choose Project -> Add Module Then click Open)
>> Add 2 Command Buttons To your Form. Press the first button to make your program run at startup. Press the seco and to delete it from running at startup.
Insert the following code to your module :
Code:
Public Type SECURITY_ATTRIBUTES
nLength As Long
lpSecurityDescriptor As Long
bInheritHandle As Long
End Type
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
Declare Function RegCloseKey Lib "advapi32.dll" (ByVal hKey As Long) As Long
Declare Function RegQueryValueEx Lib "advapi32" Alias "RegQueryValueExA" _
(ByVal hKey As Long, ByVal lpValueName As String, ByVal lpReserved As _
Long, ByRef lpType As Long, ByVal lpData As String, ByRef lpcbData As Long) As Long
Declare Function RegSetValueEx Lib "advapi32.dll" Alias "RegSetValueExA" (ByVal _
hKey As Long, ByVal lpValueName As String, ByVal Reserved As Long, ByVal _
dwType As Long, ByVal lpData As String, ByVal cbData As Long) As Long
Declare Function RegDeleteValue Lib "advapi32.dll" Alias "RegDeleteValueA" _
(ByVal hKey As Long, ByVal lpValueName As String) As Long
Public Enum T_KeyClasses
HKEY_CLASSES_ROOT = &H80000000
HKEY_CURRENT_CONFIG = &H80000005
HKEY_CURRENT_USER = &H80000001
HKEY_LOCAL_MACHINE = &H80000002
HKEY_USERS = &H80000003
End Enum
Public Sub DeleteValue(rClass As T_KeyClasses, Path As String, sKey As String)
Dim hKey As Long
Dim res As Long
res = RegOpenKeyEx(rClass, Path, 0, KEY_ALL_ACCESS, hKey)
res = RegDeleteValue(hKey, sKey)
RegCloseKey hKey
End Sub
Public Function SetRegValue(KeyRoot As T_KeyClasses, Path As String, sKey As _
String, NewValue As String) As Boolean
Dim hKey As Long
Dim KeyValType As Long
Dim KeyValSize As Long
Dim KeyVal As String
Dim tmpVal As String
Dim res As Long
Dim i As Integer
Dim x As Long
res = RegOpenKeyEx(KeyRoot, Path, 0, KEY_ALL_ACCESS, hKey)
If res <> 0 Then GoTo Errore
tmpVal = String(1024, 0)
KeyValSize = 1024
res = RegQueryValueEx(hKey, sKey, 0, KeyValType, tmpVal, KeyValSize)
Select Case res
Case 2
KeyValType = REG_SZ
Case Is <> 0
GoTo Errore
End Select
Select Case KeyValType
Case REG_SZ
tmpVal = NewValue
Case REG_DWORD
x = Val(NewValue)
tmpVal = ""
For i = 0 To 3
tmpVal = tmpVal & Chr(x Mod 256)
x = x \ 256
Next
End Select
KeyValSize = Len(tmpVal)
res = RegSetValueEx(hKey, sKey, 0, KeyValType, tmpVal, KeyValSize)
If res <> 0 Then GoTo Errore
SetRegValue = True
RegCloseKey hKey
Exit Function
Errore:
SetRegValue = False
RegCloseKey hKey
End Function
'Insert the following code to your form:
Private Sub Command1_Click()
'Replace the two 'NotePad' below with your application name, and the two
'c:\windows\notepad.exe' below with your application file.
SetRegValue HKEY_LOCAL_MACHINE, _
"Software\Microsoft\Windows\CurrentVersion\Run", "NotePad", "c:\windows\notepad.exe"
End Sub
Private Sub Command2_Click()
DeleteValue HKEY_LOCAL_MACHINE, _
"Software\Microsoft\Windows\CurrentVersion\Run", "NotePad"
End Sub
That's a easy api for Visual Basic to run your application when windows starts, without leaving any shortcut in the start up folder for the user to delete easily!