VBGood网站全文搜索 Google

搜索VBGood全站网页(全文搜索)
首页 - 经验之谈 - 如何取得与设定、删除Registry内的值
发表评论(0)作者:不详, 平台:VB6.0+Win98, 阅读:9525, 日期:2001-09-17
如何取得与设定、删除Registry内的值


作者: 王国荣 

  程式启动时,会在 "HKEY_LOCAL_MACHINE\kj\Registry" Subkey 底下写入:(此时
会呼叫 SetDefaultValue 及 SetValue 函数)

        资料类型        名称            资料
        =========       ==============  ================================
                        (预设值)        kj Registry Master
        REG_SZ          StringData      这是字串
        REG_MULTI_SZ    MultiString     字串一(0) +字串二+Chr(0) +Chr(0)
        REG_DWORD       LongData        99999
        REG_BINARY      BinaryData      11 22 33 44 AA BB CC DD

  接着当您按下「显示所有 Value 时」(command1)时,程式会读出来所有 Value 并且
  显示在ListBox 之中(此时会呼叫 GetDefaultValue、GetValueByIndex 函数)。

  最後当程式结束时,则会删除以上所有的 Value(此时会呼叫 GetValueByIndex 函数
及 RegDeleteValue API 函数)。

注释:请放3个CommandBox一个ListBox於form上

Option Explicit
注释:
Private Sub Form_Load()
    Dim hKey As Long, ret As Long

    ret = SetDefaultValue(HKEY_LOCAL_MACHINE, "SOFTWARE\kj\Registry", _
                       "kj Registry Master")
    ret = RegOpenKey(HKEY_LOCAL_MACHINE, "SOFTWARE\kj\Registry", hKey)

    ret = SetValue(hKey, "StringData", REG_SZ, "这是字串")
    ret = SetValue(hKey, "MultiString", REG_MULTI_SZ, "字串一" + Chr(0) _
          + "字串二" + Chr(0))
    ret = SetValue(hKey, "LongData", REG_DWORD, 99999)
    ret = SetValue(hKey, "BinaryData", REG_BINARY, _
                    Array(&H11, &H22, &H33, &H44, &HAA, &HBB, &HCC, &HDD), 8)
    Call RegCloseKey(hKey)
    MsgBox "已写入资料到登录资料库中,您可以开启 RegEdit 加以检查!"
End Sub

Private Sub Command1_Click() 注释: 显示所有 Value
    Dim Index As Long, ret As Long, hKey As Long
    Dim bArr() As Byte, Name As String, vType As Long

    ret = RegOpenKey(HKEY_LOCAL_MACHINE, "SOFTWARE\kj\Registry", hKey)
    ret = GetValueByIndex(hKey, Index, Name, bArr, vType)
    While ret
        If Len(Name) = 0 Then Name = "(预 设 值)"
        List1.AddItem Name & vbTab & ValueOutput(bArr, vType)
        Index = Index + 1
        ret = GetValueByIndex(hKey, Index, Name, bArr, vType)
    Wend
    Call RegCloseKey(hKey)
End Sub

Private Sub Form_Unload(Cancel As Integer)
    Dim Index As Long, ret As Long, hKey As Long
    Dim bArr() As Byte, Name As String, vType As Long

    ret = RegOpenKey(HKEY_LOCAL_MACHINE, "SOFTWARE\kj\Registry", hKey)
    ret = GetValueByIndex(hKey, Index, Name, bArr, vType)
    While ret
        Call RegDeleteValue(hKey, Name)
        注释: 不可以执行 Index = Index + 1,因为 Index = 0 的 Value 已删除,
        注释: 後面的 Index 向前递减,所以 Index = 0 又可以读到 Value,
        注释: 其实在这一个 While 回圈中,您可以将 Index 变数改成 0
        ret = GetValueByIndex(hKey, Index, Name, bArr, vType)
    Wend
    Call RegCloseKey(hKey)
    MsgBox "kj\Registry 的 Value 已删除,利用 RegEdit 检查时,记得要先执行功能的「检视/重新整理」!"
End Sub

Function ValueOutput(bArr() As Byte, ByVal vType As Long) As String
    Dim S As String, S2 As String, length As Integer, L As Long
    Dim i As Integer, sArr() As String

    Select Case vType
        Case REG_SZ, REG_EXPAND_SZ
            ByteArrayToString bArr, S

            注释: 呼叫 ExpandEnvironmentStrings
            S2 = String(Len(S) + 256, Chr(0))
            length = ExpandEnvironmentStrings(S, S2, Len(S2))
            S = Left(S2, length - 1)
            ValueOutput = "Type=String, Data=" & S

        Case REG_MULTI_SZ
            ByteArrayToMultiString bArr, sArr
            ValueOutput = "Type=MultiString, Data="
            For i = LBound(sArr) To UBound(sArr)
                ValueOutput = ValueOutput & sArr(i) & ", "
            Next i

        Case REG_DWORD, REG_DWORD_BIG_ENDIAN
            ByteArrayToLong bArr, L
            ValueOutput = "Type=Long, Data=" & L

        Case REG_BINARY
            ValueOutput = "Type=Byte Array, Data="
            For i = LBound(bArr) To UBound(bArr)
                ValueOutput = ValueOutput + Format(Hex(bArr(i)), "00")
            Next i
    End Select
End Function

Private Sub Command2_Click()
    Unload Me
    End
End Sub


Private Sub Command3_Click()
Dim hKey As Long, resu As Long
Dim aa As Boolean
Dim bytary() As Byte
Dim str5 As String
resu = RegOpenKey(HKEY_LOCAL_MACHINE, _
    "SOFTWARE\Microsoft\Windows\CurrentVersion", hKey)
aa = GetValue(hKey, "ProductId", bytary, REG_SZ)
Call ByteArrayToString(bytary, str5)
Debug.Print str5
Call RegCloseKey(hKey)
End Sub