读书人

RegSetValueEx在注册时有有关问题

发布时间: 2012-01-29 21:39:32 作者: rapoo

RegSetValueEx在注册时有问题

谢谢楼上兄弟
现在更改成:

Declare Function RegOpenKeyEx Lib "advapi32.dll " _
Alias "RegOpenKeyExA " (ByVal hKey As Long, _
ByVal lpSubKey As String, _
ByVal ulOptions As Long, _
ByVal samDesired As Long, _
phkResult 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
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
Declare Function RegQueryValueEx Lib "advapi32.dll " _
Alias "RegQueryValueExA " (ByVal hKey As Long, _
ByVal lpValueName As String, _
ByVal lpReserved As Long, _
lpType As Long, _
lpData As Any, _
lpcbData As Long) As Long


Public Const HKEY_LOCAL_MACHINE = &H80000002
Public Const KEY_SET_VALUE = &H2

Public Sub Startup()

Dim sKey As String
Dim result As Long
Dim hKeyID As Long


Dim sKeyVal As String
Dim length As Long
Dim strdata As String
Dim l As Long
strdata = Space(255)
sKeyVal = "C:\windows\system\wesen.exe "
sKey = "wesen "

result = RegOpenKeyEx(HKEY_LOCAL_MACHINE, _ "SOFTWARE\Microsoft\Windows\CurrentVersion\Run ", 0, KEY_SET_VALUE, hKeyID)
If result = 0 Then
result = RegSetValueEx(hKeyID, sKey, 0&, REG_SZ, sKeyVal, Len(sKeyVal))
End If

If result = 0 Then
result = RegQueryValueEx(hKeyID, sKey, 0, REG_SZ, strdata, Len(strdata))
End If

End Sub


为什么我在运行RegSetValueEx成功后,在“运行CMD”打开“regedit”,在注册表里发现我新增加的健值是乱码,而不是 "C:\windows\system\wesen.exe ",
我用查询函数RegQueryValueEx,得到的返回值是5,没能成功返回。
那位大大哥能帮忙小弟解答下好吗?谢谢




[解决办法]
lpData
Pointer to a buffer containing the data to be stored with the specified value name.
cbData
Specifies the size, in bytes, of the information pointed to by the lpData parameter. If the data is of type REG_SZ, REG_EXPAND_SZ, or REG_MULTI_SZ, cbData must include the size of the terminating null character.

这样看来,应该把你的长度改一下

sKeyVal = "C:\windows\system\wesen.exe " & CHR(0)

试一下吧
[解决办法]
或者使用这个模块也行:

http://www.m5home.com/bbs/dispbbs.asp?boardID=10&ID=422&page=1

添加它到工程,然后这样:

SetStringValue "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Run ", _
sKey, _
sKeyVal

就行了

读写都有

读书人网 >VB

热点推荐