注册 登录
编程论坛 VB6论坛

读写注册表的问题,求指点,刚接触不懂

wxflw 发布于 2012-12-02 01:54, 354 次点击

'=========================
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 RegCreateKey Lib "advapi32.dll" Alias "RegCreateKeyA" (ByVal hKey As Long, ByVal lpSubKey As String, 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 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
Private Const HKEY_LOCAL_MACHINE = &H80000002
Private Const REG_SZ = 1&
Private Const REG_EXPAND_SZ = 2&
Private Const REG_BINARY = 3&
Private Const REG_DWORD = 4&
Private Const ERROR_SUCCESS = 0&

'写入注册表字符串键值

Private Sub SetString(hKey As Long, strPath As String, strValue As String, strdata As String, lngType As Long)

Dim keyhand As Long
RegCreateKey hKey, strPath, keyhand
RegSetValueEx keyhand, strValue, 0, lngType, ByVal strdata, Len(strdata) + 8
RegCloseKey keyhand
End Sub
'获得注册表值
Private Function GetString(hKey As Long, strPath As String, strValue As String)

Dim keyhand As Long
Dim lResult As Long
Dim strBuf As String
Dim lDataBufSize As Long
Dim intZeroPos As Integer
Dim lValueType As Long 'new add
RegOpenKey hKey, strPath, keyhand
lResult = RegQueryValueEx(keyhand, strValue, 0&, lValueType, ByVal 0&, lDataBufSize)
If lValueType = REG_SZ Or lValueType = REG_EXPAND_SZ Then
strBuf = String(lDataBufSize, " ")
lResult = RegQueryValueEx(keyhand, strValue, 0&, lValueType, ByVal strBuf, lDataBufSize)
If lResult = ERROR_SUCCESS Then
intZeroPos = InStr(strBuf, Chr$(0))
If intZeroPos > 0 Then
GetString = Left$(strBuf, intZeroPos - 1)
Else: GetString = strBuf
End If
End If
End If
End Function
'=======================================
Private Sub Command1_Click()
SetString HKEY_LOCAL_MACHINE, "software\管理", "注", "值", ZCfile  'ZCfile是一个变量里面是一个字符串,
                                                                    '长度有98个字节为什么写入的时候提示ByRef参数不符?要什么弄?关键是问题出在哪里?在学!
end  sub                                                             'ZCfile=3B98E2DFFC6CB06A89DCB0D5C60A02069D3D9048DB16A7EEE539E93E3618CBE7AA53CA0B650DFD85C4F59FA156F7A2CC                                                                           
4 回复
#2
bczgvip2012-12-02 16:21
Option Explicit

'=========================
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 RegCreateKey Lib "advapi32.dll" Alias "RegCreateKeyA" (ByVal hKey As Long, ByVal lpSubKey As String, 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 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
Private Const HKEY_LOCAL_MACHINE As Long = &H80000002
Private Const REG_SZ As Long = 1&
Private Const REG_EXPAND_SZ As Long = 2&
Private Const REG_BINARY As Long = 3&
Private Const REG_DWORD As Long = 4&
Private Const ERROR_SUCCESS As Long = 0&

'写入注册表字符串键值
Private Sub SetString(ByVal hKey As Long, strPath As String, strValue As String, strData As String, ByVal lngType As Long)
    Dim keyhand As Long
    Dim lRet As Long
    lRet = RegCreateKey(hKey, strPath, keyhand)
    If lRet = ERROR_SUCCESS Then
        RegSetValueEx keyhand, strValue, 0, lngType, ByVal strData, Len(strData) + 1
        RegCloseKey keyhand
    End If
End Sub
'获得注册表值
Private Function GetString(hKey As Long, strPath As String, strValue As String) As String
    Dim keyhand As Long
    Dim lResult As Long
    Dim strBuf As String
    Dim lDataBufSize As Long
    Dim intZeroPos As Integer
    Dim lValueType As Long 'new add
    RegOpenKey hKey, strPath, keyhand
    lResult = RegQueryValueEx(keyhand, strValue, 0&, lValueType, ByVal 0&, lDataBufSize)
    If lValueType = REG_SZ Or lValueType = REG_EXPAND_SZ Then
        strBuf = String$(lDataBufSize, vbNullChar)
        lResult = RegQueryValueEx(keyhand, strValue, 0&, lValueType, ByVal strBuf, lDataBufSize)
        If lResult = ERROR_SUCCESS Then
            intZeroPos = InStr(strBuf, vbNullChar)
            If intZeroPos > 0 Then
                GetString = Left$(strBuf, intZeroPos - 1)
            Else: GetString = strBuf
            End If
        End If
    End If
End Function

'=======================================
Private Sub Command1_Click()
    SetString HKEY_LOCAL_MACHINE, "software\管理", "注", "值", REG_SZ   'ZCfile是一个变量里面是一个字符串,
                                                                    '长度有98个字节为什么写入的时候提示ByRef参数不符?要什么弄?关键是问题出在哪里?在学!
                                                                    'ZCfile=3B98E2DFFC6CB06A89DCB0D5C60A02069D3D9048DB16A7EEE539E93E3618CBE7AA53CA0B650DFD85C4F59FA156F7A2CC
    Debug.Print GetString(HKEY_LOCAL_MACHINE, "software\管理", "注")
End Sub
'ZCfile 是神马类型。
#3
wxflw2012-12-02 16:36
'ZCfile是一个变量存储的是md5加密后的加密码应该是字符型吧?
#4
Artless2012-12-02 20:19
以下是引用wxflw在2012-12-2 16:36:29的发言:

'ZCfile是一个变量存储的是md5加密后的加密码应该是字符型吧?

ByRef参数不符
#5
ych96312012-12-02 20:21
回复 4楼 Artless
版主我有一个问题!一段程序出错了,不知为何,求指导!
1