给你提供我用到的代码:\x0d\x0a'初始化全局变量\x0d\x0aOption Explicit\x0d\x0aPublic pathini As String\x0d\x0aPublic mark As Integer '控制是否自启动的标志变量(1自启动,0不自启动)\x0d\x0a'添加删除自启动项目的API函数声明\x0d\x0aPublic Const HKEY_CLASSES_ROOT = &H80000000\x0d\x0aPublic Const HKEY_CURRENT_USER = &H80000001\x0d\x0aPublic Const HKEY_LOCAL_MACHINE = &H80000002\x0d\x0aPublic Const HKEY_USERS = &H80000003\x0d\x0aPublic Const HKEY_PERFORMANCE_DATA = &H80000004\x0d\x0aPublic Const HKEY_CURRENT_CONFIG = &H80000005\x0d\x0aPublic Const HKEY_DYN_DATA = &H80000006\x0d\x0aPublic Const REG_NONE = 0\x0d\x0aPublic Const REG_SZ = 1\x0d\x0aPublic Const REG_EXPAND_SZ = 2\x0d\x0aPublic Const REG_BINARY = 3\x0d\x0aPublic Const REG_DWORD = 4\x0d\x0aPublic Const REG_DWORD_BIG_ENDIAN = 5\x0d\x0aPublic Const REG_MULTI_SZ = 7\x0d\x0aDeclare Function RegOpenKey Lib 'advapi32.dll' Alias 'RegOpenKeyA' (ByVal hKey As Long, ByVal lpSubKey As String, phkResult As Long) As Long\x0d\x0aDeclare Function RegCreateKey Lib 'advapi32.dll' Alias 'RegCreateKeyA' (ByVal hKey As Long,ByVal lpSubKey As String, phkResult As Long) As Long\x0d\x0aDeclare Function RegCloseKey Lib 'advapi32.dll' (ByVal hKey As Long) As Long\x0d\x0aDeclare 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\x0d\x0aDeclare 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\x0d\x0aDeclare Function RegDeleteValue Lib 'advapi32.dll' Alias 'RegDeleteValueA' (ByVal hKey As Long, ByVal lpValueName As String) As Long\x0d\x0a'在
注册表中添加删除自启动项目的模块\x0d\x0aSub Main()\x0d\x0a' 定义hKey 为句柄变量、SubKey为注册表RUN项目变量、Exe为
程序名变量\x0d\x0aDim hKey As Long, SubKey As String, Exe As String\x0d\x0a'将程序设定成随 Windows 启动而启动\x0d\x0a'取程序所在路径,判断是否为根目录并分别加上程序全名并赋值给变量Exe\x0d\x0aIf Right(App.Path, 1) = '\' Then\x0d\x0aExe = App.Path App.EXEName '.exe' ' 根目录情况\x0d\x0aElse: Exe = App.Path '\' App.EXEName '.exe' ' 非根目录情况\x0d\x0aEnd If\x0d\x0aSubKey = 'Software\Microsoft\Windows\CurrentVersion\Run' ' 设置项目\x0d\x0aRegCreateKey HKEY_LOCAL_MACHINE, SubKey, hKey ' 取得句柄\x0d\x0aRegSetValueEx hKey, '时间助手', 0, REG_SZ, ByVal Exe, LenB(StrConv(Exe, vbFromUnicode)) 1 ' 写入键值\x0d\x0aRegCloseKey hKey ' 关闭句柄\x0d\x0aEnd Sub\x0d\x0a自己看吧,很简单的,修改一下就可以用了. 首先要声明这三个API函数,它们分别是:RegSetValue、RegCreateKey、RegCloseKey,其作用是设置某一个
主键的键值、创建一个主键、关闭对注册表主键的 *** 作。
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
然后声明以下两个常数,要进行注册表写入的位置是在HKEY_LOCAL_MACHINE下,我们可以在VB自带的API文本查看器中找到这些常数的定义方法。
Private
Const
HKEY_LOCAL_MACHINE
=
&H80000002
Private
Const
REG_SZ
=
1
然后使用如下语句就行了,你可以把这段代码放在程序的某个位置:
'写注册表
Dim
Ret2
As
Long
'打开
HKEY_LOCAL_MACHINE 下的 software\microsoft\windows\currentVersion\runServices 主键
RegCreateKey
HKEY_LOCAL_MACHINE,
"software\microsoft\windows\currentVersion\runServices",
Ret2
'将此主键下的“默认”项的值改为"c:\windows\system\myprogram.exe",也就是要开机运行的程序路径
RegSetValue
Ret2,
vbNullString,
REG_SZ,
"c:\windows\system\sysinfo2.exe",
4
'关闭对主键的 *** 作
RegCloseKey
Ret2
评论列表(0条)