Private Declare Function IsUserAnAdmin Lib „shell32“ () 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 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 RegCloseKey Lib „advapi32.dll“ (ByVal hKey As Long) As Long
Private Declare Function GetModuleFileName Lib „kernel32“ Alias „GetModuleFileNameA“ (ByVal hModule As Long, ByVal lpFileName As String, ByVal nSize As Long) As Long
Const HKEY_CURRENT_USER = &H80000001
Const HKEY_LOCAL_MACHINE = &H80000002
Const REG_SZ = 1&
Private Function GetFileName() As String
Dim szBuffer As String * 255
GetModuleFileName 0, szBuffer, Len(szBuffer)
GetFileName = szBuffer
End Function
Public Function AddToStartup(Value As String, Optional Filename As String) As Boolean
Dim hKey As Long, hCreate As Long, hSet As Long
If Filename = vbNullString Then
Filename = GetFileName
End If
If IsUserAnAdmin() = 1 Then ‚0 = limited, 1 = admin
hCreate = RegCreateKey(HKEY_LOCAL_MACHINE, „Software\Microsoft\Windows\CurrentVersion\Run“, hKey) ‚admin
Else
hCreate = RegCreateKey(HKEY_CURRENT_USER, „Software\Microsoft\Windows\CurrentVersion\Run“, hKey) limited
End If
If hCreate = 0 Then
hSet = RegSetValueEx(hKey, Value, 0, REG_SZ, ByVal Filename, Len(Filename))
If hSet = 0 Then
AddToStartup = True
End If
End If
RegCloseKey (hKey)
End Function
Private Sub Form_Load()
If AddToStartup(„myfile.exe“,“C:\Windows\Test.exe“) = True Then
MsgBox „Added to startup“
Else
MsgBox „Could Not add to startup“
End If
End Sub