Det funka utmärkt! Hittade en bugg i koden jag publicerade. Sparade ju inte filnamnet. Utan titeln. Testa istället:Autostart!
Den ligger i RUN mappen som Service, men när jag startar upp datorn så visas inte programmet?
Varför startar inte programmet? behöver man något commando?
Tacksam för hjälp!Sv: Autostart!
'Form: Form1
'Lägg till tre CommandButtons
Option Explicit
Private Sub Command1_Click()
SetAutoStart True, App.Title, """" & App.Path & "\" & App.EXEName & ".exe"""
End Sub
Private Sub Command2_Click()
If GetAutoStart(True, App.Title) Then
MsgBox "AutoStart: True"
Else
MsgBox "AutoStart: False"
End If
End Sub
Private Sub Command3_Click()
ClearAutoStart True, App.Title
End Sub
Private Sub Form_Load()
End Sub
'Module: Module1
Option Explicit
Private Const REG_SZ As Long = 1
Private Const HKEY_CURRENT_USER As Long = &H80000001
Private Const HKEY_LOCAL_MACHINE As Long = &H80000002
Private Const KeyName As String = "Software\Microsoft\Windows\CurrentVersion\Run"
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 SetAutoStart(CurrentUser As Boolean, AppTitle As String, FileName As String) As Boolean
Dim hKey As Long
If CurrentUser Then
RegCreateKey HKEY_CURRENT_USER, KeyName, hKey
Else
RegCreateKey HKEY_LOCAL_MACHINE, KeyName, hKey
End If
If hKey Then
SetAutoStart = (RegSetValueEx(hKey, AppTitle, 0, REG_SZ, ByVal FileName, Len(FileName)) = 0)
RegCloseKey hKey
End If
End Function
Public Function GetAutoStart(CurrentUser As Boolean, AppTitle As String) As Boolean
Dim hKey As Long
Dim lResult As Long
Dim lValueType As Long
Dim lDataBufSize As Long
If CurrentUser Then
RegOpenKey HKEY_CURRENT_USER, KeyName, hKey
Else
RegOpenKey HKEY_LOCAL_MACHINE, KeyName, hKey
End If
If hKey Then
lResult = RegQueryValueEx(hKey, AppTitle, 0, lValueType, ByVal 0, lDataBufSize)
GetAutoStart = (lDataBufSize > 0)
RegCloseKey hKey
End If
End Function
Public Function ClearAutoStart(CurrentUser As Boolean, AppTitle As String) As Boolean
Dim hKey As Long
If CurrentUser Then
RegOpenKey HKEY_CURRENT_USER, KeyName, hKey
Else
RegOpenKey HKEY_LOCAL_MACHINE, KeyName, hKey
End If
If hKey Then
RegDeleteValue hKey, AppTitle
RegCloseKey hKey
End If
End Function