'取得AutoCAD各版本的注册表HKEY_USERS路径 Function GetAutoCAD_RegPath() As String Dim Temp As String Temp = Left(ThisDrawing.Application.Version, 4) Dim Temp1 As String '路径还只能是"\"而不是"/" Temp1 = GetUserRegKey + "\Software\Autodesk\AutoCAD\R" & Temp & "\" Dim hKey As Long Dim i Dim Temp2 As String * 256 Temp2 = Space(256) Dim Temp3 As String If RegOpenKey(HKEY_USERS, Temp1, hKey) = ERROR_SUCCESS Then While RegEnumKey(hKey, i, Temp2, 256) = ERROR_SUCCESS '只有一个子键 '去掉最后一个字符,不知道是什么 Temp3 = Left(Trim(Temp2), Len(Trim(Temp2)) - 1) GetAutoCAD_RegPath = "HKEY_USERS\" & Temp1 & Temp3 & "\" i = i + 1 Wend RegCloseKey hKey End If End Function
'**模 块 名:RegWork '**创 建 人:叶帆 '**日 期:2003年01月11日 '**修 改 人: '**日 期: '**描 述:注册表操作(不同类型,读写方法有一定区别) '**版 本:版本1.0 '************************************************************************* '--------------------------------------------------------------- '-注册表 API 声明... '--------------------------------------------------------------- '关闭登录关键字 Private Declare Function RegCloseKey Lib "advapi32" (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 RegCreateKeyEx Lib "advapi32" Alias "RegCreateKeyExA" (ByVal hKey As Long, ByVal lpSubKey As String, ByVal Reserved As Long, ByVal lpClass As String, ByVal dwOptions As Long, ByVal samDesired As Long, ByRef lpSecurityAttributes As SECURITY_ATTRIBUTES, ByRef phkResult As Long, ByRef lpdwDisposition As Long) As Long '打开关键字 Private Declare Function RegOpenKeyEx Lib "advapi32" Alias "RegOpenKeyExA" (ByVal hKey As Long, ByVal lpSubKey As String, ByVal ulOptions As Long, ByVal samDesired As Long, ByRef phkResult As Long) As Long '返回关键字的类型和值 Private Declare Function RegQueryValueEx_SZ Lib "advapi32" Alias "RegQueryValueExA" (ByVal hKey As Long, ByVal lpValueName As String, ByVal lpReserved As Long, ByRef lpType As Long, ByVal lpData As String, ByRef lpcbData As Long) As Long Private Declare Function RegQueryValueEx_DWORD Lib "advapi32.dll" Alias "RegQueryValueExA" (ByVal hKey As Long, ByVal lpValueName As String, ByVal lpReserved As Long, lpType As Long, lpData As Long, lpcbData 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, ByRef lpcbData As Long) As Long
'将文本字符串与指定关键字关联 Private Declare Function RegSetValueEx_SZ Lib "advapi32" Alias "RegSetValueExA" (ByVal hKey As Long, ByVal lpValueName As String, ByVal Reserved As Long, ByVal dwType As Long, ByVal lpData As String, ByVal cbData As Long) As Long Private Declare Function RegSetValueEx_DWORD Lib "advapi32" Alias "RegSetValueExA" (ByVal hKey As Long, ByVal lpValueName As String, ByVal Reserved As Long, ByVal dwType As Long, lpData As Long, ByVal cbData As Long) As Long Private Declare Function RegSetValueEx_BINARY Lib "advapi32" 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 Declare Function RegOpenKey Lib "advapi32.dll" Alias "RegOpenKeyA" _ (ByVal hKey As Long, ByVal lpSubKey As String, phkResult As Long) As Long
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 RegDeleteKey Lib "advapi32.dll" Alias "RegDeleteKeyA" (ByVal hKey As Long, ByVal lpSubKey As String) As Long '从登录关键字中删除一个值 Private Declare Function RegDeleteValue Lib "advapi32.dll" Alias "RegDeleteValueA" (ByVal hKey As Long, ByVal lpSubKey As String) As Long
'--------------------------------------------------------------- '- 注册表安全属性类型... '--------------------------------------------------------------- Private Type SECURITY_ATTRIBUTES nLength As Long lpSecurityDescriptor As Long bInheritHandle As Boolean End Type
'************************************************************************* '**函 数 名:WriteRegKey '**输 入:ByVal KeyRoot(REGRoot) - 根 '** :ByVal KeyName(String) - 键的路径 '** :ByVal SubKeyName(String) - 键名 '** :ByVal SubKeyType(REGValueType) - 键的类型 '** :ByVal SubKeyValue(String) - 键值 '**输 出:(Boolean) - 成功返回True,失败返回False '**功能描述:写注册表 '**全局变量: '**调用模块: '**作 者:叶帆 '**日 期:2003年01月10日 '**修 改 人: '**日 期: '**版 本:版本1.0 '************************************************************************* Public Function WriteRegKey(ByVal KeyRoot As REGRoot, ByVal KeyName As String, ByVal SubKeyName As String, ByVal SubKeyType As REGValueType, ByVal SubKeyValue As String) As Boolean Dim rc As Long ' 返回代码 Dim hKey As Long ' 处理一个注册表关键字 Dim hDepth As Long ' Dim lpAttr As SECURITY_ATTRIBUTES ' 注册表安全类型 Dim i As Integer Dim bytValue(1024) As Byte
'************************************************************************* '**函 数 名:ReadRegKey '**输 入:KeyRoot(Long) - 根 '** :KeyName(String) - 键的路径 '** :SubKeyRef(String) - 键名 '**输 出:(String) - 返回键值 '**功能描述:读注册表 '**全局变量: '**调用模块: '**作 者:叶帆 '**日 期:2003年01月10日 '**修 改 人: '**日 期: '**版 本:版本1.0 '************************************************************************* Public Function ReadRegKey(ByVal KeyRoot As REGRoot, ByVal KeyName As String, ByValSubKeyName As String) As String Dim i As Long ' 循环计数器 Dim rc As Long ' 返回代码 Dim hKey As Long ' 处理打开的注册表关键字 Dim hDepth As Long ' Dim sKeyVal As String Dim lKeyValType As Long ' 注册表关键字数据类型 Dim tmpVal As String ' 注册表关键字的临时存储器 Dim KeyValSize As Long ' 注册表关键字变量尺寸 Dim lngValue As Long Dim bytValue(1024) As Byte
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" _ (dest As Any, source As Any, ByVal numBytes As Long)
Private Declare Function ExpandEnvironmentStrings Lib "kernel32" Alias "ExpandEnvironmentStringsA" _ (ByVal lpSrc As String, ByVal lpDst As String, ByVal nSize As Long) As Long
'//注册表 API 函数声明 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 RegOpenKeyEx Lib "advapi32.dll" Alias "RegOpenKeyExA" _ (ByVal hKey As Long, ByVal lpSubKey As String, ByVal ulOptions As Long, _ ByVal samDesired As Long, phkResult As Long) As Long
Private Declare Function RegCloseKey Lib "advapi32.dll" (ByVal hKey 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" Alias "RegSetValueExA" _ (ByVal hKey As Long, ByVal lpValueName As String, _ ByVal Reserved As Long, ByVal dwType As Long, _ ByVal lpbData As Any, ByVal cbData As Long) As Long
Private Declare Function RegCreateKeyEx Lib "advapi32" Alias "RegCreateKeyExA" _ (ByVal hKey As Long, ByVal lpSubKey As String, ByVal Reserved As Long, _ ByVal lpClass As String, ByVal dwOptions As Long, _ ByVal samDesired As Long, lpSecurityAttributes As SECURITY_ATTRIBUTES, _ phkResult As Long, lpdwDisposition As Long) As Long
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 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 FILETIME) 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, ByVal lpData As String, lpcbData As Long) As Long
Private Declare Function RegDeleteKey Lib "advapi32.dll" Alias "RegDeleteKeyA" _ (ByVal hKey As Long, ByVal lpSubKey As String) As Long
Private Declare Function RegDeleteValue Lib "advapi32.dll" Alias "RegDeleteValueA" _ (ByVal hKey As Long, ByVal lpValueName As String) As Long
Private Declare Function RegSetValueExString Lib "advapi32.dll" Alias "RegSetValueExA" _ (ByVal hKey As Long, ByVal ipValueName As String, _ ByVal Reserved As Long, ByVal dwType As Long, _ ByVal lpValue As String, ByVal cbData As Long) As Long
Private Declare Function RegSetValueExLong Lib "advapi32.dll" Alias "RegSetValueExA" _ (ByVal hKey As Long, ByVal lpValueName As String, _ ByVal Reserved As Long, ByVal dwType As Long, _ lpValue As Long, ByVal cbData As Long) As Long
Private Declare Function RegSetValueExByte Lib "advapi32.dll" Alias "RegSetValueExA" _ (ByVal hKey As Long, ByVal lpValueName As String, _ ByVal Reserved As Long, ByVal dwType As Long, _ lpValue As Byte, ByVal cbData As Long) As Long
Private Declare Function RegQueryInfoKey Lib "advapi32.dll" Alias "RegQueryInfoKeyA" _ (ByVal hKey As Long, ByVal lpClass As String, lpcbClass As Long, _ ByVal lpReserved As Long, lpcSubKeys As Long, _ lpcbMaxSubKeyLen As Long, lpcbMaxClassLen As Long, lpcValues As Long, _ lpcbMaxValueNameLen As Long, lpcbMaxValueLen As Long, _ lpcbSecurityDescriptor As Long, lpftLastWriteTime As FILETIME) As Long
Private Declare Function RegEnumValueInt 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 RegEnumValueStr 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 RegEnumValueByte 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 Type SECURITY_ATTRIBUTES nLength As Long lpSecurityDescriptor As Long bInheritHandle As Boolean End Type
Private Type FILETIME dwLowDateTime As Long dwHighDateTime As Long End Type
'// for specifying the type of data to save Public Enum RegValueTypes eInteger = vbInteger eLong = vbLong eString = vbString eByteArray = vbArray + vbByte End Enum
'//保存时指定类型 Public Enum RegFlags IsExpandableString = 1 IsMultiString = 2 'IsBigEndian = 3 '// 无指针同样不要设置大Endian值 End Enum
Private Const ERR_NONE = 0
Function SetRegistryValue(ByVal hKey As RootKeyEnum, ByVal KeyName As String, _ ByVal ValueName As String, ByVal Value As Variant, valueType As RegValueTypes, _ Optional Flag As RegFlags = 0) As Boolean
Dim handle As Long Dim lngValue As Long Dim strValue As String Dim binValue() As Byte Dim length As Long Dim retVal As Long
Dim SecAttr As SECURITY_ATTRIBUTES '//键的安全设置 '//设置新键值的名称和默认安全设置 SecAttr.nLength = Len(SecAttr) '//结构大小 SecAttr.lpSecurityDescriptor = 0 '//默认安全权限 SecAttr.bInheritHandle = True '//设置的默认值
'// 打开或创建键 'If RegOpenKeyEx(hKey, KeyName, 0, KEY_ALL_ACCESS, handle) Then Exit Function retVal = RegCreateKeyEx(hKey, KeyName, 0, vbNullString, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, SecAttr, handle, retVal) If retVal Then Exit Function
'//3种数据类型 Select Case VarType(Value) Case vbByte, vbInteger, vbLong '// 若是字节, Integer值或Long值... lngValue = Value retVal = RegSetValueExLong(handle, ValueName, 0, REG_DWORD, lngValue, Len(lngValue))
Case vbString '// 字符串, 扩展环境字符串或多段字符串... strValue = Value Select Case Flag Case IsExpandableString retVal = RegSetValueEx(handle, ValueName, 0, REG_EXPAND_SZ, ByVal strValue, 255) Case IsMultiString retVal = RegSetValueEx(handle, ValueName, 0, REG_MULTI_SZ, ByVal strValue, 255) Case Else '// 正常 REG_SZ 字符串 retVal = RegSetValueEx(handle, ValueName, 0, REG_SZ, ByVal strValue, 255) End Select
Case Else '// 如果其它类型 RegCloseKey handle 'Err.Raise 1001, , "不支持的值类型"
End Select
'// 返回关闭结果 RegCloseKey handle
'// 返回写入成功结果 SetRegistryValue = (retVal = 0)
End Function
Function GetRegistryValue(ByVal hKey As RootKeyEnum, ByVal KeyName As String, _ ByVal ValueName As String, Optional DefaultValue As Variant) As Variant
Dim handle As Long Dim resLong As Long Dim resString As String Dim resBinary() As Byte Dim length As Long Dim retVal As Long Dim valueType As Long
'// 若resBinary 太小则重读 If retVal = ERROR_MORE_DATA Then '// resBinary放大,且重新读取 ReDim resBinary(0 To length - 1) As Byte retVal = RegQueryValueEx(handle, ValueName, 0, valueType, resBinary(0), _ length) End If
'// 返回相应值类型 Select Case valueType Case REG_DWORD, REG_DWORD_LITTLE_ENDIAN '// REG_DWORD 和 REG_DWORD_LITTLE_ENDIAN 相同 CopyMemory resLong, resBinary(0), 4 GetRegistryValue = resLong
Case REG_DWORD_BIG_ENDIAN '// Big Endian's 用在非-Windows环境, 如Unix系统, 本地计算机远程访问 CopyMemory resLong, resBinary(0), 4 GetRegistryValue = SwapEndian(resLong)
Case REG_SZ, REG_EXPAND_SZ resString = Space$(length - 1) CopyMemory ByVal resString, resBinary(0), length - 1 If valueType = REG_EXPAND_SZ Then '// 查询对应的环境变量 GetRegistryValue = ExpandEnvStr(resString) Else GetRegistryValue = resString End If
Case Else ' 包含 REG_BINARY '// resBinary 调整 If length <> UBound(resBinary) + 1 Then ReDim Preserve resBinary(0 To length - 1) As Byte End If GetRegistryValue = resBinary()
End Select
'// 关闭 RegCloseKey handle
End Function
Public Function DeleteRegistryValueOrKey(ByVal hKey As RootKeyEnum, RegKeyName As String, _ ValueName As String) As Boolean '//删除注册表值和键,如果成功返回True
Dim lRetval As Long '//打开和输出注册表键的返回值 Dim lRegHWND As Long '//打开注册表键的句柄 Dim sREGSZData As String '//把获取值放入缓冲区 Dim lSLength As Long '//缓冲区大小. 改变缓冲区大小要在调用之后