如何通过修改注册表实现自启动啊?
新手上路请多关照!
Option Explicit
Public 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 Declare Function RegCreateKey Lib "advapi32.dll" Alias "RegCreateKeyA" (ByVal hKey As Long, ByVal lpSubKey As String, phkResult As Long) As Long
Public Declare Function RegCloseKey Lib "advapi32.dll" (ByVal hKey As Long) As Long
Public Declare Function RegDeleteValue Lib "advapi32.dll" Alias "RegDeleteValueA" (ByVal hKey As Long, ByVal lpValueName As String) As Long
Public Const REG_SZ = 1
Public Const HKEY_LOCAL_MACHINE = &H80000002
'*************************************************************************
'**函 数 名: SetAutoRun
'**输 入: ByVal Autorun(Boolean) -
'**输 出: 无
'**功能描述: 随WINDOWS自动启动/取消启动模块
'**全局变量:
'**调用方法: Call SetAutoRun(True/False)
'**作 者: Mr.David
'**日 期: 2006-09-05 09:07:25
'**修 改 人:
'**日 期:
'**版 本: V1.0.0
'*************************************************************************
Public Sub SetAutoRun(ByVal Autorun As Boolean)
Dim KeyId As Long
Dim MyexePath As String
Dim regkey As String
MyexePath = App.Path & "\" & App.EXEName & ".exe" '获取程序位置
regkey = "Software\Microsoft\Windows\CurrentVersion\Run" '键值位置变量
Call RegCreateKey(HKEY_LOCAL_MACHINE, regkey, KeyId) '建立
If Autorun Then
RegSetValueEx KeyId, "MySoftware", 0&, REG_SZ, ByVal MyexePath, LenB(MyexePath)
Else
RegDeleteValue KeyId, "MySoftware"
End If
RegCloseKey KeyId
End Sub
调用方法
SetAutoRun(ByVal Autorun As Boolean)
先引用系统里面都有的WSHom.Ocx
Option Explicit
'*************************************************************************
'**函 数 名: SetAutoRun
'**输 入: ByVal Autorun(Boolean) -
'**输 出: 无
'**功能描述: 随WINDOWS自动启动/取消启动模块
'**全局变量:
'**调用方法: Call SetAutoRun(True/False)
'**作 者: Mr.David
'**日 期: 2006-09-05 09:07:25
'**修 改 人:
'**日 期:
'**版 本: V1.0.0
'*************************************************************************
Public Sub SetAutoRun(ByVal Autorun As Boolean)
'WshShell 对象
'ProgId Wscript.Shell
'文件名 WSHom.Ocx
Dim WshShell As WshShell
Set WshShell = CreateObject("Wscript.Shell")
If Autorun Then
WshShell.RegWrite "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Run\" & App.EXEName, App.Path & "\" & App.EXEName & ".exe"
Else
WshShell.RegDelete "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Run\" & App.EXEName
End If
Set WshShell = Nothing
End Sub
'=====================================
' 注册表的读写 声明
'=====================================
Private Declare Function RegCloseKey Lib "advapi32.dll" _
(ByVal hKey As Long) As Long ' 关闭RegCreateKey打开的键
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 RegSetValue Lib "advapi32.dll" Alias "RegSetValueA" _
(ByVal hKey As Long, _
ByVal lpSubKey As String, _
ByVal dwType As Long, _
ByVal lpData As String, _
ByVal cbData As Long) As Long '写入启动值
Private Declare Function RegDeleteKey Lib "advapi32.dll" Alias "RegDeleteKeyA" ( _
ByVal hKey As Long, _
ByVal lpSubKey As String) As Long '删除加入的键值
Private Const HKEY_CURRENT_MACHINE = &H80000002
Private Const REG_SZ = 1
'============================
' 添加启动写注册表
'============================
Public Sub SaveString(hKey As Long, strPath As String, strValue As String, strData As String)
Dim ret
RegCreateKey hKey, strPath, ret
RegSetValue ret, strValue, REG_SZ, strData, Len(strData)
RegCloseKey ret
End Sub
'===========================
' 删除在注册表的根键
'===========================
Public Sub delKey(hKey As Long, strPath As String, delKey As String)
Dim ret
RegCreateKey hKey, strPath, ret
RegDeleteKey ret, delKey
RegCloseKey ret
End Sub
'============================
' 判断是否加为启动项
'============================
Public Sub makeRun(X As Boolean)
If X Then
SaveString HKEY_CURRENT_MACHINE, _
"SOFTWARE\MICROSOFT\WINDOWS\CURRENTVERSION\RUN", _
App.EXEName, App.Path & "\" & App.EXEName & ".exe"
MsgBox "成功加入启动项", , "提示"
Else
delKey HKEY_CURRENT_MACHINE, _
"SOFTWARE\MICROSOFT\WINDOWS\CURRENTVERSION\RUN", _
App.EXEName
MsgBox "成功删除启动项", , "提示"
End If
End Sub
Private Sub Command1_Click()
makeRun True
End Sub
Private Sub Command2_Click()
makeRun False
End Sub