问题描述:
我学教程,完全按照教程写的代码,为什么例子中能实现自启动的注册表的改变,而我的不能,哪里错了,还是少什么,望帮助一下。谢谢!
代码如下:
Option Explicit
'把要运行的程序加载在run键下?
'该键的位置在:HKEY_LOCAL_MACHINE 下,SoftWare\Microsoft\Windows\CurrentVersion\Run
'相关代码如下:
'函数原型在advapi32.dll动态库中,在动态库中还有这样一个名字RegCreateKeyA
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
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
Const REG_SZ = 1
Const HKEY_LOCAL_MACHINE = &H80000002
'以上是api函数声明
Sub SetMyValue(hKey As Long, strPath As String, strValue As String, strdata As String)
Dim keyhandle
Dim lResult As Long
lResult = RegCreateKey(hKey, strPath, keyhandle)
lResult = RegSetValueEx(keyhandle, strValue, 0, REG_SZ, ByVal strdata, Len(strdata))
lResult = RegCloseKey(keyhandle)
End Sub
'setmyvalue函数用来改写注册表
Private Sub Command1_Click()
Dim aa As String
Dim bb As String
aa = Text1
bb = Text2
SetMyValue HKEY_LOCAL_MACHINE, "SoftWare \ Microsoft \ Windows \ CurrentVersion \ Run", aa, bb
MsgBox "ok!!"
End Sub
加 as long后还是不能在注册表中体现,也不在msconfig的启动中体现
问题解答:
Dim keyhandle 应该Dim keyhandle as long才对。
我明白了,你的"SoftWare \ Microsoft \ Windows \ CurrentVersion \ Run"中间要那些空格干嘛?快去掉吧,去掉其中的空格后就能正常加入了,你试试吧。。