用RegQueryValueEx操作注册表遇到的一个怪问题,求高手指点!!!
本帖最后由 stfger 于 2012-04-29 10:08:51 编辑
我先说一下问题在哪,我用RegQueryValueEx的时候,第二个参数里有变量,返回值就为2,常量就为0,这是生么原因?
参数还要分常量和变量吗?
用的是win7 的系统,求高手指点!!!!!
Option Explicit
Private Declare Function RegEnumKey Lib "advapi32.dll" Alias "RegEnumKeyA" (ByVal hKey As Long, ByVal dwIndex As Long, ByVal lpName As String, ByVal cbName As Long) As Long
Private Declare Function RegOpenKey Lib "advapi32.dll" Alias "RegOpenKeyA" (ByVal hKey As Long, ByVal lpSubKey As String, ByRef phkResult As Long) As Long
Private 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
Private Declare Function RegCloseKey Lib "advapi32.dll" (ByVal hKey As Long) As Long
Private Const HKEY_LOCAL_MACHINE = &H80000002
Private Const REG_SZ = 1
Dim SysName(99) As String '定义数组储存GUID
Dim i As Integer '数组元素
Dim Idx As Long
Dim hKey As Long, ret As Long
Dim lenData As Long, typeData As Long, s As String
Private Sub Command1_Click()
For i = 0 To Idx
'Debug.Print SysName(i)
ret = RegOpenKey(HKEY_LOCAL_MACHINE, "BCD00000000\Objects" & SysName(i) & "\Elements\12000004", hKey)
'Debug.Print ret
If ret = 0 Then
ret = RegQueryValueEx(hKey, "Elements", 0, REG_SZ, ByVal vbNullString, lenData) '注意ByVal千万别忘了
Debug.Print ret
End If
Next
RegCloseKey hKey
End Sub
Private Sub Form_Load()
Dim name As String
Dim tempName As String
RegOpenKey HKEY_LOCAL_MACHINE, "BCD00000000\Objects", hKey
Idx = 0
name = String(256, Chr(0))
Do
ret = RegEnumKey(hKey, Idx, name, Len(name))
If ret = 0 Then
tempName = Left(name, InStr(name, Chr(0)) - 1)
SysName(Idx) = tempName
Idx = Idx + 1
End If
Loop Until ret <> 0
End Sub
'Private Sub Command1_Click()
' ret = RegOpenKey(HKEY_LOCAL_MACHINE, "BCD00000000\Objects\{afeec1b6-6385-11e1-bacd-a5aa00bfb8e5}\Elements\12000004", hKey)
' 'Debug.Print ret
' If ret = 0 Then
' ret = RegQueryValueEx(hKey, "Element", 0, REG_SZ, ByVal vbNullString, lenData)
' 'Debug.Print ret
' If ret = 0 Then
' s = String(lenData, Chr(0))
' RegQueryValueEx hKey, "Element", 0, typeData, ByVal s, lenData '注意ByVal千万别忘了
' s = Left(s, InStr(s, Chr(0)) - 1)
' Debug.Print s
' End If
'
' End If
'End Sub
最下面的这个是我的验证代码,没有问题,但是上面的debug出来全是2,是怎么回事啊?再次球高手指点!!!
第一次发帖,没多少分,实在不好意思~~~~
[解决办法]
不懂,如你的代码,不知道HKEY_LOCAL_MACHINE下有BCD00000000这样的键
ret = RegOpenKey(HKEY_LOCAL_MACHINE, "BCD00000000\Objects" & SysName(i) & "\Elements\12000004", hKey)