Option ExplicitConst REG_SZ As Long = 1Const HKEY_LOCAL_MACHINE = &H80000002"建立登錄檔項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 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"獲取子項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 LongConst REG_DWORD = 4Private Sub Form_Load()"為text1設定預設路徑程式Text1.Text = "C:\Program Files\****\****.exe""請修改自動啟動程式的路徑。End SubPrivate Sub Command1_Click()"設定開機啟動Dim hKey As LongDim myexe As StringDim myint As Integermyint = Len(Text1.Text) - InStrRev(Text1.Text, "\")"取得應用程式檔名字串的位元組數,含副檔名myexe = Right(Text1.Text, myint)"取得應用程式檔名字串,含副檔名If Text1.Text <> "" ThenRegCreateKey HKEY_LOCAL_MACHINE, "Software\Microsoft\Windows\CurrentVersion\Run", hKey""HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Run"是登錄檔中的路徑RegSetValueEx hKey, myexe, 0, REG_SZ, ByVal Text1.Text, 255"myexe是上面登錄檔路徑中新增的項,Text1.Text是該項的資料。End IfMsgBox "設定完成,請您重新啟動計算機!" & vbCrLf & vbCrLf & "你設定的程式將開機自動執行!", 64, "提示資訊"End SubPrivate Sub Command2_Click()"查詢自動啟動的程式DHK1.DialogTitle = "選擇程式""注意開啟對話方塊的名稱是DHK1DHK1.Filter = "應用程式(*.exe)|*.exe"DHK1.ShowOpenIf Len(DHK1.FileName) ThenText1.Text = DHK1.FileNameEnd IfEnd Sub5Private Sub Command3_Click()EndEnd Sub
用VB開啟電腦的應用程式是呼叫API函式來修改或增加註冊表中的值,這樣在系統啟動時就會自動執行。
Option ExplicitConst REG_SZ As Long = 1Const HKEY_LOCAL_MACHINE = &H80000002"建立登錄檔項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 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"獲取子項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 LongConst REG_DWORD = 4Private Sub Form_Load()"為text1設定預設路徑程式Text1.Text = "C:\Program Files\****\****.exe""請修改自動啟動程式的路徑。End SubPrivate Sub Command1_Click()"設定開機啟動Dim hKey As LongDim myexe As StringDim myint As Integermyint = Len(Text1.Text) - InStrRev(Text1.Text, "\")"取得應用程式檔名字串的位元組數,含副檔名myexe = Right(Text1.Text, myint)"取得應用程式檔名字串,含副檔名If Text1.Text <> "" ThenRegCreateKey HKEY_LOCAL_MACHINE, "Software\Microsoft\Windows\CurrentVersion\Run", hKey""HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Run"是登錄檔中的路徑RegSetValueEx hKey, myexe, 0, REG_SZ, ByVal Text1.Text, 255"myexe是上面登錄檔路徑中新增的項,Text1.Text是該項的資料。End IfMsgBox "設定完成,請您重新啟動計算機!" & vbCrLf & vbCrLf & "你設定的程式將開機自動執行!", 64, "提示資訊"End SubPrivate Sub Command2_Click()"查詢自動啟動的程式DHK1.DialogTitle = "選擇程式""注意開啟對話方塊的名稱是DHK1DHK1.Filter = "應用程式(*.exe)|*.exe"DHK1.ShowOpenIf Len(DHK1.FileName) ThenText1.Text = DHK1.FileNameEnd IfEnd Sub5Private Sub Command3_Click()EndEnd Sub