首先要声明这三个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
延伸阅读
文章来源于领测软件测试网 https://www.ltesting.net/