Attribute VB_Name = "hlp_Autostart"
' Modul for Visual Basic 6
' Copyright (C) 2003-2005 Jan Vorel
'
' This program is free software; you can redistribute it and/or modify
' it under the terms of the GNU General Public License as published by
' the Free Software Foundation; either version 2 of the License, or
' (at your option) any later version.

' This program is distributed in the hope that it will be useful,
' but WITHOUT ANY WARRANTY; without even the implied warranty of
' MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
' GNU General Public License for more details.

' You should have received a copy of the GNU General Public License
' along with this program; if not, write to the Free Software
' Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

' This is a shared modul, class modul, usercontrol or form. It is also
' published under the GNU General Public License.

' If you have any questions, suggestions or bug reports about this product,
' contact me: info@ctuser.net; Do not contact me about general B.A.S.I.C.
' language or A.P.I. issues

Option Explicit
Option Base 0

Private Declare Function RegCreateKeyExA Lib "advapi32.dll" (ByVal HKEY As Long, ByVal lpSubKey As String, ByVal Reserved As Long, ByVal lpClass As String, ByVal dwOptions As Long, ByVal samDesired As Long, lpSecurityAttributes As Any, phkResult As Long, lpdwDisposition As Long) As Long
Private Declare Function RegOpenKeyExA Lib "advapi32.dll" (ByVal HKEY As Long, ByVal lpSubKey As String, ByVal ulOptions As Long, ByVal samDesired As Long, phkResult As Long) As Long
Private Declare Function RegSetValueExA Lib "advapi32.dll" (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 RegQueryValueExA Lib "advapi32.dll" (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 RegDeleteValueA Lib "advapi32.dll" (ByVal HKEY As Long, ByVal lpValueName As String) As Long
Private Declare Function RegCloseKey Lib "advapi32.dll" (ByVal HKEY As Long) As Long

Public Enum Comparemodes
  None = 0
  EXEName = 1
  Full = 2
  End Enum

Public Enum AutoStartModes
  CURun = 0
  CURunOnce = 1
  CURunServices = 2
  CURunServicesOnce = 3
  LMRun = 4
  LMRunOnce = 5
  LMRunServices = 6
  LMRunServicesOnce = 7
  End Enum

Public Sub SetAutoRun(ByVal AutoStartMode As AutoStartModes, ByVal Caption As String, Optional ByVal ExeFileName As String, Optional ByVal Parameters As String)
  Dim xLong1 As Long
  Dim xLong2 As Long
  Dim KeyChars As Variant
  KeyChars = Array("Run", "RunOnce", "RunServices", "RunServicesOnce")
  
  'If RegCreateKeyExA(&H80000001 + (AutoStartMode And 4) \ 4, "Software\Microsoft\Windows\CurrentVersion\" & KeyChars(AutoStartMode And 3), 0&, "", 0, 983103, ByVal 0&, xLong1, xLong2) = 0& Then
  If RegCreateKeyExA(&H80000001 + (AutoStartMode And 4) \ 4, "Software\Microsoft\Windows\CurrentVersion\" & KeyChars(AutoStartMode And 3), 0&, "", 0, 2&, ByVal 0&, xLong1, xLong2) = 0& Then
    If ExeFileName = "" Then
      ExeFileName = AppBase
      End If
    If Not InStr(EXEName, " ") = 0 Then
      ExeFileName = Chr(34) & ExeFileName & Chr(34)
      End If
    If Not Parameters = "" Then
      ExeFileName = ExeFileName & Chr(32) & Parameters
      End If
    Call RegSetValueExA(xLong1, Caption, 0&, 1, ByVal ExeFileName, Len(ExeFileName))
    Call RegCloseKey(xLong1)
    End If
  End Sub
    
Public Function GetAutoRun(ByVal AutoStartMode As AutoStartModes, ByVal Caption As String, Optional ByVal ExeFileName As String, Optional ByVal Parameters As String, Optional ByVal Comparemode As Comparemodes = None) As Boolean
  Dim xLong1 As Long
  Dim xLong2 As Long
  Dim xLong3 As Long
  Dim KeyChars As Variant
  KeyChars = Array("Run", "RunOnce", "RunServices", "RunServicesOnce")
  
  'If RegOpenKeyExA(&H80000001 + (AutoStartMode And 4) \ 4, "Software\Microsoft\Windows\CurrentVersion\" & KeyChars(AutoStartMode And 3), 0&, 983103, xLong1) = 0& Then
  If RegOpenKeyExA(&H80000001 + (AutoStartMode And 4) \ 4, "Software\Microsoft\Windows\CurrentVersion\" & KeyChars(AutoStartMode And 3), 0&, 1&, xLong1) = 0& Then
    If Comparemode = None Then
      ExeFileName = ""
      Else
      If ExeFileName = "" Then
        ExeFileName = AppBase
        End If
      If Not InStr(EXEName, " ") = 0 Then
        ExeFileName = Chr(34) & ExeFileName & Chr(34)
        End If
      If Not Parameters = "" And Comparemode = Full Then
        ExeFileName = ExeFileName & Chr(32) & Parameters
        End If
      ExeFileName = UCase(ExeFileName)
      End If
    xLong3 = 1024: Parameters = String(xLong3, Chr(0))
    If RegQueryValueExA(xLong1, Caption, 0&, xLong2, ByVal Parameters, xLong3) = 0& Then
      GetAutoRun = ExeFileName = UCase(Left(Parameters, Len(ExeFileName)))
      End If
    Call RegCloseKey(xLong1)
    End If
  
  End Function
    
Public Sub DelAutoRun(ByVal AutoStartMode As AutoStartModes, ByVal Caption As String, Optional ByVal ExeFileName As String, Optional ByVal Parameters As String)
  Dim xLong1 As Long
  Dim KeyChars As Variant
  KeyChars = Array("Run", "RunOnce", "RunServices", "RunServicesOnce")
  
  'If RegOpenKeyExA(&H80000001 + (AutoStartMode And 4) \ 4, "Software\Microsoft\Windows\CurrentVersion\" & KeyChars(AutoStartMode And 3), 0&, 983103, xLong1) = 0& Then
  If RegOpenKeyExA(&H80000001 + (AutoStartMode And 4) \ 4, "Software\Microsoft\Windows\CurrentVersion\" & KeyChars(AutoStartMode And 3), 0&, 2&, xLong1) = 0& Then
    Call RegDeleteValueA(xLong1, Caption)
    Call RegCloseKey(xLong1)
    End If
  End Sub

