'使用下面这三个API与两个常数(标记部份为快捷键方式增加到开始下的启动)
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
RegCreateKey
Lib
"advapi32.dll"
Alias
"RegCreateKeyA"
(ByVal
hKey
As
Long,
ByVal
lpSubKey
As
String,
phkResult
As
Long)
As
Long
Private
Declare
Function
RegCloseKey
Lib
"advapi32.dll"
(ByVal
hKey
As
Long)
As
Long
Const
HKEY_LOCAL_MACHINE
=
&H80000002
Const
REG_SZ
=
1
Private
Sub
Command1_Click()
Dim
Ret2
As
Long
'打开
HKEY_LOCAL_MACHINE
下的
software\microsoft\windows\currentVersion\run
RegCreateKey
HKEY_LOCAL_MACHINE,
"software\microsoft\windows\currentVersion\run",
Ret2
'将此主键下的
"默认"
值改为你的
exe
全路径"
RegSetValue
Ret2,
vbNullString,
REG_SZ,
"c:\windows\abc.exe",
4
'关闭对主键的 *** 作
RegCloseKey
Ret2
End
Sub
窗口总是在最前面
Option
Explicit
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 RegDeleteValue Lib "advapi32.dll" Alias "RegDeleteValueA" (ByVal hKey As Long, ByVal lpValueName As String) As Long '这个函数用来删除注册表项的值
Private Declare Function RegCloseKey Lib "advapi32.dll" (ByVal hKey As Long) As Long '这个函数用来关闭已经打开的注册表项
Private Const REG_SZ = 1 '这个常数表示注册表项的值的类型为字符串
Private Const HKEY_CURRENT_USER = &H80000001 '这个常数表示修改当前用户的注册表项
Private Const SubKey As String = "Software\Microsoft\Windows\CurrentVersion\Run" '定义要编辑的注册表的子项
'接下来,我们写一个函数,来建立自动运行
Sub RegAutoRun()
On Error Resume Next
Dim Exe As String
Dim hKey As Long
Dim rc As Long
If Right(App.Path, 1) <>"\" Then
Exe = App.Path &"\" &App.EXEName &".exe"
Else
Exe = App.Path &App.EXEName &".exe"
End If
rc = RegCreateKey(HKEY_CURRENT_USER, SubKey, hKey)
If rc = 0 Then
RegSetValueEx hKey, "AutoStart", 0, REG_SZ, ByVal Exe, LenB(StrConv(Exe, vbFromUnicode)) + 1
RegCloseKey hKey
End If
End Sub
'建立自动运行,自然要可以删除自动运行的功能
Sub DelAutoRun()
On Error Resume Next
Dim hKey As Long
Dim rc As Long
rc = RegCreateKey(HKEY_CURRENT_USER, SubKey, hKey)
If rc = 0 Then
RegDeleteValue hKey, "AutoStart"
RegCloseKey hKey
End If
End Sub
把Exe = App.Path &"\" &App.EXEName &".exe"
改成Exe = "c:\123.exe"
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)