【VB技巧】VB枚举注册表

  • A+
所属分类:lcx

Private Declare Function RegOpenKey Lib "advapi32.dll" Alias "RegOpenKeyA" (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 RegEnumValue Lib "advapi32.dll" Alias "RegEnumValueA" (ByVal hKey As Long, ByVal dwIndex As Long, ByVal lpValueName As String, lpcbValueName As Long, ByVal lpReserved As Long, lpType As Long, lpData As Byte, lpcbData As Long) As Long
Private Declare Function RegEnumKeyEx Lib "advapi32.dll" Alias "RegEnumKeyExA" (ByVal hKey As Long, ByVal dwIndex As Long, ByVal lpName As String, lpcbName As Long, ByVal lpReserved As Long, ByVal lpClass As String, lpcbClass As Long, lpftLastWriteTime As Any) As Long

Private Enum RegHKey
    hkRoot = &H80000000
    hkUser = &H80000001
    hkMachine = &H80000002
    hkUsers = &H80000003
    hkPerformanceData = &H80000004
    hkConfig = &H80000005
    hkDYNData = &H80000006
End Enum

Private Function EnumValues(ByVal strKey As String, strValue() As String, Optional ByVal hKey As RegHKey = hkMachine) As Long
    Dim Ret As Long, tmpValue As String
    Dim intZeroPos As Integer, Cnt As Long

    RegOpenKey hKey, strKey, Ret
    Cnt = 0
    Do
        tmpValue = String(255, Chr$(0))
        If RegEnumValue(Ret, Cnt, tmpValue, 255, 0, ByVal 0&, ByVal 0&, ByVal 0&) 0 Then Exit Do
        ReDim Preserve strValue(Cnt) As String
        intZeroPos = InStr(1, tmpValue, Chr$(0))
        strValue(Cnt) = IIf(intZeroPos > 0, Left$(tmpValue, intZeroPos - 1), tmpValue)
        Cnt = Cnt + 1
    Loop

    RegCloseKey Ret
    EnumValues = Cnt
End Function

'测试项目为系统启动项:

Private Sub Form_Load()
    Dim strValue() As String
    Dim i As Long
    For i = 0 To EnumValues("SoftwareMicrosoftWindowsCurrentVersionRun", strValue) - 1
        Debug.Print strValue(i)
    Next i
End Sub

文章来源于lcx.cc:【VB技巧】VB枚举注册表

相关推荐: 【技巧】批处理、Bat 全盘搜索指定名字的文件

批处理、Bat 全盘搜索指定名字的文件: set alldrive=c d e f g h i j k l m n o p q r s t u v w x y z for %%i in (%alldrive%) do if exist %%i: dir /s …

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: