Tipp 0307 Anwendung bei Windows-Start ausführen
Autor/Einsender:
Datum:
  Michael Werner
29.01.2003
Entwicklungsumgebung:   VB 5
Dieses Beispiel zeigt, wie und wo man den Eintrag in der Systemregistrierung tätigen muss, damit eine Anwendung bei jedem Start von Windows automatisch ausgeführt wird.
Speicherort in der Registry:
   HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Run
Eintrag -> Name des Programms
Zusätzlich und fast "nebenbei" zeigt dieser Tipp etwas Grundsätzliches: Das Lesen, Schreiben und Löschen eines Eintrags an einem beliebigem Ort in der Registry.
Code im Codebereich des Moduls
 
Option Explicit

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 Any) 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 RegCloseKey Lib "advapi32.dll" _
    (ByVal hKey As Long) As Long

Public Declare Function RegSetValueEx_String 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

Public Declare Function RegDeleteValue Lib "advapi32.dll" Alias _
    "RegDeleteValueA" (ByVal hKey As Long, ByVal lpValueName _
    As String) As Long

Private Const HKEY_CURRENT_USER = &H80000001
Private Const HKEY_LOCAL_MACHINE = &H80000002

Private Const KEY_QUERY_VALUE = &H1
Private Const KEY_SET_VALUE = &H2
Private Const KEY_CREATE_SUB_KEY = &H4
Private Const KEY_ENUMERATE_SUB_KEYS = &H8
Private Const KEY_NOTIFY = &H10
Private Const KEY_CREATE_LINK = &H20
Private Const KEY_ALL_ACCESS = KEY_QUERY_VALUE Or KEY_SET_VALUE _
              Or KEY_CREATE_SUB_KEY Or KEY_ENUMERATE_SUB_KEYS Or _
              KEY_NOTIFY Or KEY_CREATE_LINK

Private Const KEY_READ = KEY_QUERY_VALUE Or _
              KEY_ENUMERATE_SUB_KEYS Or KEY_NOTIFY

Private Const KEY_WRITE = KEY_SET_VALUE Or KEY_CREATE_SUB_KEY
Private Const KEY_EXECUTE = KEY_READ

Private Const ERROR_SUCCESS = 0&

Private Const REG_NONE = 0
Private Const REG_SZ = 1

Private Const root = HKEY_LOCAL_MACHINE
Private Const key As String = _
              "SOFTWARE\Microsoft\Windows\CurrentVersion\Run"

Public Function SetRun() As Boolean
  Dim lResult As Long
  Dim lKeyHandle As Long
  Dim sField As String
  Dim sPath As String

  sField = App.EXEName
  sPath = App.Path & IIf(Right(App.Path, 1) = "\", "", "\") & _
          App.EXEName & ".exe"

  lResult = RegOpenKeyEx(root, key, 0, KEY_ALL_ACCESS, lKeyHandle)
  If lResult <> ERROR_SUCCESS Then
    SetRun = False
    Exit Function
  End If

  lResult = RegSetValueEx_String(lKeyHandle, sField, 0, _
                  REG_SZ, sPath, Len(sPath) + 1)
  RegCloseKey lKeyHandle
  SetRun = (lResult = ERROR_SUCCESS)
End Function

Public Function DeleteRun() As Boolean
  Dim lResult As Long
  Dim lKeyHandle As Long
  Dim sField As String
  Dim sPath As String

  sField = App.EXEName
  sPath = App.Path & IIf(Right(App.Path, 1) = "\", "", "\") & _
          App.EXEName & ".exe"

  lResult = RegOpenKeyEx(root, key, 0, KEY_ALL_ACCESS, lKeyHandle)
  If lResult <> ERROR_SUCCESS Then
    DeleteRun = False
    Exit Function
  End If

  lResult = RegDeleteValue(lKeyHandle, sField)
  DeleteRun = (lResult = ERROR_SUCCESS)
  RegCloseKey lKeyHandle
End Function

Public Function IsRun() As Boolean
  Dim sField As String
  Dim Value As Variant
  Dim lResult As Long
  Dim lKeyHandle As Long
  Dim dwType As Long
  Dim lBufferSize As Long
  Dim sBuffer As String
  Dim iPos As Integer

  sField = App.EXEName
  Value = App.Path & IIf(Right(App.Path, 1) = "\", "", "\") & _
          App.EXEName & ".exe"

  lResult = RegOpenKeyEx(root, key, 0, KEY_READ, lKeyHandle)
  IsRun = (lResult = ERROR_SUCCESS)

  If lResult <> ERROR_SUCCESS Then Exit Function
  lResult = RegQueryValueEx(lKeyHandle, sField, 0&, dwType, _
                    ByVal 0&, lBufferSize)
  IsRun = (lResult = ERROR_SUCCESS)
  If lResult <> ERROR_SUCCESS Then Exit Function

  If dwType = REG_SZ Then
    sBuffer = Space$(lBufferSize + 1)
    lResult = RegQueryValueEx(lKeyHandle, sField, 0&, dwType, _
          ByVal sBuffer, lBufferSize)
    IsRun = (lResult = ERROR_SUCCESS)
    If lResult <> ERROR_SUCCESS Then Exit Function
    iPos = InStr(sBuffer, Chr$(0))
    If iPos Then Value = Left(sBuffer, iPos - 1)
  End If

  If lResult = ERROR_SUCCESS Then RegCloseKey lKeyHandle
  IsRun = True
End Function
 
Code im Codebereich der Form
 
Option Explicit

Private Sub Form_Load()
  If IsCompiled = False Then
    MsgBox "Diese Sperre beim Starten des Programms aus der " & _
      "Entwicklungsumgebung von VB" & vbNewLine & _
      "wurde eingebaut, um einen doppelten Eintrag unter " & _
      "dem Schlüssel RUN zu vermeiden." & vbNewLine & vbNewLine & _
      "Starten Sie das Programm von der kompilierten EXE." & _
      vbNewLine & "Speicherort in der Registry:" & vbNewLine & _
      "HKEY_LOCAL_MACHINE\SOFTWARE\...\CurrentVersion\Run" & _
      vbNewLine & "Eintrag 'RunMe'", vbExclamation
    End
  End If

  If IsRun Then
    Check1.Value = vbChecked
    Label1.Caption = "AutoRun ist aktiv." & vbNewLine & _
            "Beim Booten des PCs wird " & LCase(App.EXEName) & _
            ".exe ausgeführt."
  Else
    Check1.Value = vbUnchecked
    Label1.Caption = "AutoRun ist deaktiv."
  End If
End Sub

Private Sub cmdUebernehmen_Click()
  If Check1.Value = vbChecked Then
    If Not IsRun Then
      SetRun
      Label1.Caption = "AutoRun wurde aktiviert." & vbNewLine & _
            "Beim Booten des PCs wird " & LCase(App.EXEName) & _
            ".exe ausgeführt."
    Else
      Label1.Caption = "AutoRun war schon aktiviert." & vbCrLf & _
            "Beim Booten des PCs wird " & LCase(App.EXEName) & _
            ".exe ausgeführt."
    End If
  Else
    If IsRun Then
      DeleteRun
      Label1.Caption = "AutoRun wurde deaktiviert."
    Else
      Label1.Caption = "AutoRun war schon deaktiviert."
    End If
  End If
End Sub

Private Function IsCompiled() As Boolean
  On Error GoTo NotCompiled
  Debug.Print 1 / 0
  IsCompiled = True
NotCompiled:
End Function
 
Weitere Links zum Thema
Anwendung im Admin-Modus ausführen

Windows-Version
95
98/SE
ME
NT
2000
XP
Vista
Win 7
VB-Version
VBA 5
VBA 6
VB 4/16
VB 4/32
VB 5
VB 6


Download  (4,3 kB) Downloads bisher: [ 2279 ]

Vorheriger Tipp Zum Seitenanfang Nächster Tipp

Startseite | Projekte | Tutorials | API-Referenz | VB-/VBA-Tipps | Komponenten | Bücherecke | VB/VBA-Forum | VB.Net-Forum | DirectX-Forum | Foren-Archiv | DirectX | VB.Net-Tipps | Chat | Spielplatz | Links | Suchen | Stichwortverzeichnis | Feedback | Impressum

Seite empfehlen Bug-Report
Letzte Aktualisierung: Sonntag, 14. August 2011