吾爱破解 - 52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 922|回复: 9
收起左侧

[求助] VB里如何使用RegOpenKey查询注册表中是否有某项键值?

[复制链接]
jessylake 发表于 2022-11-19 18:12
[Visual Basic] 纯文本查看 复制代码
Private Declare Function RegOpenKey Lib "advapi32.dll" Alias "RegOpenKeyA" (ByVal hKey As Long, ByVal lpSubKey As String, phkResult As Long) As Long


里面的几个参数如何使用呢?

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

bester 发表于 2022-11-19 19:21
本帖最后由 bester 于 2022-11-19 19:36 编辑

完全不必用RegOpenKey,直接用RegGetValueA直接获取值,参数如下:
RegGetValueA(根键名,项路径,键名,键值类型,0,返回值,返回值缓冲大小),如果只确定是否有键值或者键名 返回值和返回值缓冲区大小都不填

实例:
获取HKEY_CURRENT_USER下面SOFTWARE\\Microsoft\\Office\\") + name+"\\Outlook\\Security中键名为OutlookSecureTempFolder的值是多少,值结果保存在Data_Path
C代码:
string Reg_Path= std::string("SOFTWARE\\Microsoft\\Office\\") + name+"\\Outlook\\Security";  
LSTATUS lsGetRet=RegGetValueA(HKEY_CURRENT_USER, Reg_Path.c_str(), "OutlookSecureTempFolder", RRF_RT_REG_SZ, 0,Data_Path, &len_Path);

IsGetRet这值为ERROR_SUCCESS 则确定有键值或者键名
 楼主| jessylake 发表于 2022-11-19 19:54
bester 发表于 2022-11-19 19:21
完全不必用RegOpenKey,直接用RegGetValueA直接获取值,参数如下:
RegGetValueA(根键名,项路径,键名, ...

这是VB写的吗,怎么看着有点陌生?
lovehfs 发表于 2022-11-19 20:12
'读取注册表 DWORD 键值
Function GetDword(ByVal hKey As Long, ByVal strPath As String, ByVal strValueName As String) As Long
    Dim lResult As Long
    Dim lValueType As Long
    Dim lBuf As Long
    Dim lDataBufSize As Long
    Dim R As Long
    Dim keyhand As Long
    R = RegOpenKey(hKey, strPath, keyhand)
    ' Get length/data type
    lDataBufSize = 4
    lResult = RegQueryValueEx(keyhand, strValueName, 0&, lValueType, lBuf, lDataBufSize)
    If lResult = ERROR_SUCCESS Then
        If lValueType = REG_DWORD Then
            GetDword = lBuf
        End If
    'Else
    ' Call errlog("GetDWORD-" & strPath, False)
    End If
    R = RegCloseKey(keyhand)
End Function

'读取注册表二进制键值
Function GetBinary(ByVal hKey As Long, ByVal strPath As String, ByVal strValueName As String) As Long
    Dim lResult As Long
    Dim lValueType As Long
    Dim lBuf As Long
    Dim lDataBufSize As Long
    Dim R As Long
    Dim keyhand As Long
    R = RegOpenKey(hKey, strPath, keyhand)
    ' Get length/data type
    lDataBufSize = 4
    lResult = RegQueryValueEx(keyhand, strValueName, 0&, lValueType, lBuf, lDataBufSize)
    If lResult = ERROR_SUCCESS Then
        If lValueType = REG_BINARY Then
            GetBinary = lBuf
        End If
    End If
    R = RegCloseKey(keyhand)
End Function
lovehfs 发表于 2022-11-19 20:14
Public Declare Function RegOpenKey Lib "advapi32.dll" Alias "RegOpenKeyA" (ByVal hKey As Long, ByVal lpSubKey As String, phkResult As Long) As Long
Public 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
 楼主| jessylake 发表于 2022-11-19 20:21
lovehfs 发表于 2022-11-19 20:14
Public Declare Function RegOpenKey Lib "advapi32.dll" Alias "RegOpenKeyA" (ByVal hKey As Long, ByVal ...

Private Declare Function RegOpenKey Lib "advapi32.dll" Alias "RegOpenKeyA" (ByVal hKey As Long, ByVal lpSubKey As String, phkResult As Long) As Long        '判断注册表项是否存在
Private Const HKEY_CURRENT_USER = &H80000001

Private Sub Command1_Click()
If RegOpenKey(HKEY_CURRENT_USER, "HKEY_CURRENT_USER\Software\VB and VBA Program Settings", 0) = 0& Then
        MsgBox "这个项存在"
    Else
        MsgBox "这个项不存在"
End If
End Sub

这是我在网上搜的,但是不对,不知道哪里有问题?
alvin123456 发表于 2022-11-19 23:45
Private Declare Function RegOpenKey Lib "advapi32.dll" Alias "RegOpenKeyA" (ByVal hKey As Long, ByVal lpSubKey As String, phkResult As Long) As Long        '判断注册表项是否存在
Private Const HKEY_CURRENT_USER = &H80000001

Private Sub Command1_Click()
If RegOpenKey(HKEY_CURRENT_USER, "Software\VB and VBA Program Settings", 1) = 0& Then
        MsgBox "这个项存在"
    Else
        MsgBox "这个项不存在"
End If
End Sub

免费评分

参与人数 1吾爱币 +1 热心值 +1 收起 理由
jessylake + 1 + 1 谢谢@Thanks!

查看全部评分

 楼主| jessylake 发表于 2022-11-20 00:13
alvin123456 发表于 2022-11-19 23:45
Private Declare Function RegOpenKey Lib "advapi32.dll" Alias "RegOpenKeyA" (ByVal hKey As Long, ByVa ...

太谢谢了,原来最后一个参数设置成1?这个1起什么作用?
 楼主| jessylake 发表于 2022-11-20 11:13
lovehfs 发表于 2022-11-19 20:12
'读取注册表 DWORD 键值
Function GetDword(ByVal hKey As Long, ByVal strPath As String, ByVal strValu ...

感谢,能不能给个使用这两个函数的例子,主要里面的变量不知道怎么用,如何取值
 楼主| jessylake 发表于 2022-11-20 18:47
本帖最后由 jessylake 于 2022-11-20 18:48 编辑

[Visual Basic] 纯文本查看 复制代码
Private Sub Command1_Click()
Dim hKey As Long
If RegOpenKey(HKEY_CURRENT_USER, "Software\VB and VBA Program Settings", hKey) = 0& Then
        MsgBox "这个项存在"
    Else
        MsgBox "这个项不存在"
End If
End Sub


其实这样才是正确的
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则

返回列表

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-11-25 06:58

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表