'------------------------------ '作者:David_Lv '时间:2003-06-08 ' '类名:CRegister '功能:对注册表的一些简单操作 '------------------------------ Option Explicit '======变量定义====== Private Const HKEY_CURRENT_USER = &H80000001 '所需注册的hKey位置 Private Const REG_SZ = 1 '键值的数据类型 '======API声明====== Private Declare Function RegOpenKey Lib "advapi32.dll" Alias "RegOpenKeyA" _ (ByVal hKey As Long, ByVal lpSubKey As String, phkResult As Long) As LongPrivate Declare Function RegCreateKey Lib "advapi32.dll" Alias "RegCreateKeyA" _ (ByVal hKey As Long, ByVal lpSubKey As String, phkResult 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 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 LongPrivate 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 RegDeleteValue Lib "advapi32.dll" Alias "RegDeleteValueA" _ (ByVal hKey As Long, ByVal lpSubKey As String) As Long '======方法定义======'创建一个项 Public Function CreateRegKey(ByVal vSubKey As String) As Boolean On Error GoTo ErrH Dim nResult As Long
'检查传入的值是否合法 If Len(vSubKey) = 0 Then GoTo ErrH
'在注册表中创建一个项 nResult = RegCreateKey(HKEY_CURRENT_USER, vSubKey, nResult) If nResult <> 0 Then GoTo ErrH CreateRegKey = True Exit Function
ErrH: CreateRegKey = False End Function '删除一个最底层项 Public Function DeleteRegKey(ByVal vSubKey As String) As Boolean On Error GoTo ErrH Dim nResult As Long
'检查传入的值是否合法 If Len(vSubKey) = 0 Then GoTo ErrH
'在注册表中删除一个项 nResult = RegDeleteKey(HKEY_CURRENT_USER, vSubKey) If nResult <> 0 Then GoTo ErrH DeleteRegKey = True Exit Function
ErrH: DeleteRegKey = False End Function '在指定的项中写入一个值 Public Function SetRegValue(ByVal vSubKey As String, ByVal vKeyName As String, ByVal vKeyValue As String) As Boolean On Error GoTo ErrH Dim nResult As Long Dim nKey As Long
'检查传入的值是否合法 If Len(vSubKey) = 0 Then GoTo ErrH If Len(vKeyName) = 0 Then GoTo ErrH If Len(vKeyValue) = 0 Then GoTo ErrH
'检查是否存在vSubKey项,成功会返回一个nKey值 nResult = RegOpenKey(HKEY_CURRENT_USER, vSubKey, nKey) If nResult <> 0 Then GoTo ErrH
'根据nKey键值,写入键名及键值 SetRegValue = RegSetValueEx(nKey, vKeyName, 0, REG_SZ, ByVal vKeyValue, LenB(StrConv(vKeyValue, vbFromUnicode)) + 1) If SetRegValue <> 0 Then GoTo ErrH SetRegValue = True Exit Function
ErrH: SetRegValue = False End Function '查找指定项中指定键值名称的键值 '如果注册表中没有指定的项值vSubKey,lpcbData参数返回0 '如果有项值但没有键名vKeyName,lpcbData参数返回2 '如果vKeyValue为一个字符,lpcbData参数返回2 '如果vKeyValue大于一个字符,lpcbData参数返回>2 Public Function GetRegValue(ByVal vSubKey As String, ByVal vKeyName As String) As String On Error GoTo ErrH Dim nResult As Long, nKey As Long, nKeyValue As String Dim nBufferSize As Long
'检查传入的参数是否合法 If Len(vSubKey) = 0 Then GoTo ErrH If Len(vKeyName) = 0 Then GoTo ErrH
'打开指定的项,成功会返回一个nKey值 nResult = RegOpenKey(HKEY_CURRENT_USER, vSubKey, nKey) If nResult <> 0 Then GoTo ErrH
ErrH: GetRegValue = "" End Function '删除指定项中的一个键 Public Function DeleteRegValue(ByVal vSubKey As String, ByVal vKeyName As String) As Boolean On Error GoTo ErrH Dim nResult As Long Dim nKey As Long
'检查传入的参数是否合法 If Len(vSubKey) = 0 Then GoTo ErrH If Len(vKeyName) = 0 Then GoTo ErrH
'打开指定的项,成功会返回一个nKey值 nResult = RegOpenKey(HKEY_CURRENT_USER, vSubKey, nKey) If nResult <> 0 Then GoTo ErrH
'根据nKey删除指定的键 nResult = RegDeleteValue(nKey, vKeyName) If nResult <> 0 Then GoTo ErrH DeleteRegValue = True Exit Function
ErrH: DeleteRegValue = False End Function 以上这些是用于进行注册表操作的代码,可以写成一个类或模块 下面是另外的代码,是界面操作的 Option Explicit '======注册表所需用的参数 Private Const mSubKey = "SOFTWARE\EMIS\ServerInfo" '默认注册的项名 Private Const mDBServerKeyName = "DBServerName" '默认数据库注册键名 Private Const mCOMServerKeyName = "COMServerName" '默认组件注册键名 Private mDBServerName As String '数据库服务器名 Private mCOMServerName As String '组件服务器名 '初始化 Private Sub Form_Load() Dim nRegister As New clsRegister
你先根据找到你的注册表与安装路径有关的键值,然后在你的升级程序中先读出该键值,问题应该可以搞定。 以下是读写注册表的一些模块 '读写注册表Public Const HKEY_CLASSES_ROOT = &H80000000 Public Const HKEY_CURRENT_USER = &H80000001 Public Const HKEY_LOCAL_MACHINE = &H80000002 Public Const HKEY_USERS = &H80000003 Public Const HKEY_PERFORMANCE_DATA = &H80000004 Public Const ERROR_SUCCESS = 0&Declare Function RegCloseKey Lib "advapi32.dll" (ByVal hKey As Long) As Long Declare Function RegCreateKey Lib "advapi32.dll" Alias "RegCreateKeyA" (ByVal hKey As Long, ByVal lpSubKey As String, phkResult As Long) As Long Declare Function RegDeleteKey Lib "advapi32.dll" Alias "RegDeleteKeyA" (ByVal hKey As Long, ByVal lpSubKey As String) As Long Declare Function RegDeleteValue Lib "advapi32.dll" Alias "RegDeleteValueA" (ByVal hKey As Long, ByVal lpValueName As String) As Long Declare Function RegOpenKey Lib "advapi32.dll" Alias "RegOpenKeyA" (ByVal hKey As Long, ByVal lpSubKey As String, phkResult As Long) As Long 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 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 LongPrivate 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 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 Any, lpcbData As Long) As LongPublic Const REG_SZ = 1 ' Unicode nul terminated string Public Const REG_DWORD = 4 ' 32-bit number '读写注册表 '读写注册表 Public Function GetString(hKey As Long, strPath As String, strValue As String)
Dim keyhand As Long Dim datatype As Long Dim lResult As Long Dim strBuf As String Dim lDataBufSize As Long Dim intZeroPos As Integer r = RegOpenKey(hKey, strPath, keyhand) lResult = RegQueryValueEx(keyhand, strValue, 0&, lValueType, ByVal 0&, lDataBufSize) If lValueType = REG_SZ Then strBuf = String(lDataBufSize, " ") lResult = RegQueryValueEx(keyhand, strValue, 0&, 0&, 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 IfEnd FunctionPublic Sub SaveString(hKey As Long, strPath As String, strValue As String, strdata As String)
Dim keyhand As Long Dim r As Long r = RegCreateKey(hKey, strPath, keyhand) r = RegSetValueEx(keyhand, strValue, 0, REG_SZ, ByVal strdata, Len(strdata)) r = RegCloseKey(keyhand)End SubPublic Function DelString(ByVal hKey As Long, ByVal strPath As String, ByVal strValue As String)
Dim keyhand As Long Dim r As Long r = RegOpenKey(hKey, strPath, keyhand) r = RegDeleteValue(keyhand, strValue) r = RegCloseKey(keyhand)End FunctionPublic Function DelKeys(ByVal hKey As Long, ByVal strPath As String) ', ByVal strKeys As String) Dim r As Long r = RegDeleteKey(hKey, strPath)End FunctionPublic Function CreateKeys(ByVal hKey As Long, ByVal strPath As String) Dim keyhand As Long Dim r As Long r = RegCreateKey(hKey, strPath, keyhand) r = RegCloseKey(keyhand)End Function Public Function ListSubkey(ByVal hKey As Long, ByVal strPath As String) As String Dim keyhand As Long Dim r As Long Dim strlistsubkey As String
Dim sName As String, sData As String, Ret As Long, RetData As Long Dim indexs As Long
Const ERROR_NO_MORE_ITEMS = 259& Const BUFFER_SIZE As Long = 255 Ret = BUFFER_SIZE sName = Space(BUFFER_SIZE)
r = RegOpenKey(hKey, strPath, keyhand) While RegEnumKeyEx(keyhand, indexs, sName, Ret, ByVal 0&, vbNullString, ByVal 0&, ByVal 0&) <> ERROR_NO_MORE_ITEMS If strlistsubkey = "" Then strlistsubkey = Left$(sName, Ret) Else strlistsubkey = strlistsubkey + "_" + Left$(sName, Ret) End If indexs = indexs + 1 Wend ListSubkey = strlistsubkey End Function'读写注册表
'作者:David_Lv
'时间:2003-06-08
'
'类名:CRegister
'功能:对注册表的一些简单操作
'------------------------------
Option Explicit
'======变量定义======
Private Const HKEY_CURRENT_USER = &H80000001 '所需注册的hKey位置
Private Const REG_SZ = 1 '键值的数据类型
'======API声明======
Private Declare Function RegOpenKey Lib "advapi32.dll" Alias "RegOpenKeyA" _
(ByVal hKey As Long, ByVal lpSubKey As String, phkResult As Long) As LongPrivate Declare Function RegCreateKey Lib "advapi32.dll" Alias "RegCreateKeyA" _
(ByVal hKey As Long, ByVal lpSubKey As String, phkResult 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 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 LongPrivate 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 RegDeleteValue Lib "advapi32.dll" Alias "RegDeleteValueA" _
(ByVal hKey As Long, ByVal lpSubKey As String) As Long
'======方法定义======'创建一个项
Public Function CreateRegKey(ByVal vSubKey As String) As Boolean
On Error GoTo ErrH
Dim nResult As Long
'检查传入的值是否合法
If Len(vSubKey) = 0 Then GoTo ErrH
'在注册表中创建一个项
nResult = RegCreateKey(HKEY_CURRENT_USER, vSubKey, nResult)
If nResult <> 0 Then GoTo ErrH
CreateRegKey = True
Exit Function
ErrH:
CreateRegKey = False
End Function
'删除一个最底层项
Public Function DeleteRegKey(ByVal vSubKey As String) As Boolean
On Error GoTo ErrH
Dim nResult As Long
'检查传入的值是否合法
If Len(vSubKey) = 0 Then GoTo ErrH
'在注册表中删除一个项
nResult = RegDeleteKey(HKEY_CURRENT_USER, vSubKey)
If nResult <> 0 Then GoTo ErrH
DeleteRegKey = True
Exit Function
ErrH:
DeleteRegKey = False
End Function
'在指定的项中写入一个值
Public Function SetRegValue(ByVal vSubKey As String, ByVal vKeyName As String, ByVal vKeyValue As String) As Boolean
On Error GoTo ErrH
Dim nResult As Long
Dim nKey As Long
'检查传入的值是否合法
If Len(vSubKey) = 0 Then GoTo ErrH
If Len(vKeyName) = 0 Then GoTo ErrH
If Len(vKeyValue) = 0 Then GoTo ErrH
'检查是否存在vSubKey项,成功会返回一个nKey值
nResult = RegOpenKey(HKEY_CURRENT_USER, vSubKey, nKey)
If nResult <> 0 Then GoTo ErrH
'根据nKey键值,写入键名及键值
SetRegValue = RegSetValueEx(nKey, vKeyName, 0, REG_SZ, ByVal vKeyValue, LenB(StrConv(vKeyValue, vbFromUnicode)) + 1)
If SetRegValue <> 0 Then GoTo ErrH
SetRegValue = True
Exit Function
ErrH:
SetRegValue = False
End Function
'查找指定项中指定键值名称的键值
'如果注册表中没有指定的项值vSubKey,lpcbData参数返回0
'如果有项值但没有键名vKeyName,lpcbData参数返回2
'如果vKeyValue为一个字符,lpcbData参数返回2
'如果vKeyValue大于一个字符,lpcbData参数返回>2
Public Function GetRegValue(ByVal vSubKey As String, ByVal vKeyName As String) As String
On Error GoTo ErrH
Dim nResult As Long, nKey As Long, nKeyValue As String
Dim nBufferSize As Long
'检查传入的参数是否合法
If Len(vSubKey) = 0 Then GoTo ErrH
If Len(vKeyName) = 0 Then GoTo ErrH
'打开指定的项,成功会返回一个nKey值
nResult = RegOpenKey(HKEY_CURRENT_USER, vSubKey, nKey)
If nResult <> 0 Then GoTo ErrH
'根据nKey获取键值的长度
nResult = RegQueryValueEx(nKey, vKeyName, 0, REG_SZ, 0, nBufferSize)
'根据nKey获取键值的内容
nKeyValue = Space(nBufferSize + 1)
nResult = RegQueryValueEx(nKey, vKeyName, 0, REG_SZ, ByVal nKeyValue, nBufferSize)
'返回的nKeyValue的后面会有Null字符,所以先用""替换Null字符,再进行Trim
nKeyValue = Trim(Replace(nKeyValue, vbNullChar, ""))
GetRegValue = nKeyValue
Exit Function
ErrH:
GetRegValue = ""
End Function
'删除指定项中的一个键
Public Function DeleteRegValue(ByVal vSubKey As String, ByVal vKeyName As String) As Boolean
On Error GoTo ErrH
Dim nResult As Long
Dim nKey As Long
'检查传入的参数是否合法
If Len(vSubKey) = 0 Then GoTo ErrH
If Len(vKeyName) = 0 Then GoTo ErrH
'打开指定的项,成功会返回一个nKey值
nResult = RegOpenKey(HKEY_CURRENT_USER, vSubKey, nKey)
If nResult <> 0 Then GoTo ErrH
'根据nKey删除指定的键
nResult = RegDeleteValue(nKey, vKeyName)
If nResult <> 0 Then GoTo ErrH
DeleteRegValue = True
Exit Function
ErrH:
DeleteRegValue = False
End Function
以上这些是用于进行注册表操作的代码,可以写成一个类或模块
下面是另外的代码,是界面操作的
Option Explicit
'======注册表所需用的参数
Private Const mSubKey = "SOFTWARE\EMIS\ServerInfo" '默认注册的项名
Private Const mDBServerKeyName = "DBServerName" '默认数据库注册键名
Private Const mCOMServerKeyName = "COMServerName" '默认组件注册键名
Private mDBServerName As String '数据库服务器名
Private mCOMServerName As String '组件服务器名
'初始化
Private Sub Form_Load()
Dim nRegister As New clsRegister
'从注册表中读取注册值
txtDBServer.Text = nRegister.GetRegValue(mSubKey, mDBServerKeyName)
txtCOMServer.Text = nRegister.GetRegValue(mSubKey, mCOMServerKeyName)
Set nRegister = Nothing
End Sub
HKEY_CLASSES_ROOT\CLSID\{01B576C7-98FA-463C-8D2B-0323053ECA59}\InprocServer32等等如果各位老大有什么代码,请mail:[email protected]
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\你也可以搜索一下你的程序安装时建立的快捷方式,根据快捷方式确定所在路径
以下是读写注册表的一些模块
'读写注册表Public Const HKEY_CLASSES_ROOT = &H80000000
Public Const HKEY_CURRENT_USER = &H80000001
Public Const HKEY_LOCAL_MACHINE = &H80000002
Public Const HKEY_USERS = &H80000003
Public Const HKEY_PERFORMANCE_DATA = &H80000004
Public Const ERROR_SUCCESS = 0&Declare Function RegCloseKey Lib "advapi32.dll" (ByVal hKey As Long) As Long
Declare Function RegCreateKey Lib "advapi32.dll" Alias "RegCreateKeyA" (ByVal hKey As Long, ByVal lpSubKey As String, phkResult As Long) As Long
Declare Function RegDeleteKey Lib "advapi32.dll" Alias "RegDeleteKeyA" (ByVal hKey As Long, ByVal lpSubKey As String) As Long
Declare Function RegDeleteValue Lib "advapi32.dll" Alias "RegDeleteValueA" (ByVal hKey As Long, ByVal lpValueName As String) As Long
Declare Function RegOpenKey Lib "advapi32.dll" Alias "RegOpenKeyA" (ByVal hKey As Long, ByVal lpSubKey As String, phkResult As Long) As Long
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
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 LongPrivate 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 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 Any, lpcbData As Long) As LongPublic Const REG_SZ = 1 ' Unicode nul terminated string
Public Const REG_DWORD = 4 ' 32-bit number
'读写注册表
'读写注册表
Public Function GetString(hKey As Long, strPath As String, strValue As String)
Dim keyhand As Long
Dim datatype As Long
Dim lResult As Long
Dim strBuf As String
Dim lDataBufSize As Long
Dim intZeroPos As Integer
r = RegOpenKey(hKey, strPath, keyhand)
lResult = RegQueryValueEx(keyhand, strValue, 0&, lValueType, ByVal 0&, lDataBufSize)
If lValueType = REG_SZ Then
strBuf = String(lDataBufSize, " ")
lResult = RegQueryValueEx(keyhand, strValue, 0&, 0&, 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 IfEnd FunctionPublic Sub SaveString(hKey As Long, strPath As String, strValue As String, strdata As String)
Dim keyhand As Long
Dim r As Long
r = RegCreateKey(hKey, strPath, keyhand)
r = RegSetValueEx(keyhand, strValue, 0, REG_SZ, ByVal strdata, Len(strdata))
r = RegCloseKey(keyhand)End SubPublic Function DelString(ByVal hKey As Long, ByVal strPath As String, ByVal strValue As String)
Dim keyhand As Long
Dim r As Long
r = RegOpenKey(hKey, strPath, keyhand)
r = RegDeleteValue(keyhand, strValue)
r = RegCloseKey(keyhand)End FunctionPublic Function DelKeys(ByVal hKey As Long, ByVal strPath As String) ', ByVal strKeys As String) Dim r As Long
r = RegDeleteKey(hKey, strPath)End FunctionPublic Function CreateKeys(ByVal hKey As Long, ByVal strPath As String) Dim keyhand As Long
Dim r As Long
r = RegCreateKey(hKey, strPath, keyhand)
r = RegCloseKey(keyhand)End Function
Public Function ListSubkey(ByVal hKey As Long, ByVal strPath As String) As String Dim keyhand As Long
Dim r As Long
Dim strlistsubkey As String
Dim sName As String, sData As String, Ret As Long, RetData As Long
Dim indexs As Long
Const ERROR_NO_MORE_ITEMS = 259&
Const BUFFER_SIZE As Long = 255
Ret = BUFFER_SIZE
sName = Space(BUFFER_SIZE)
r = RegOpenKey(hKey, strPath, keyhand)
While RegEnumKeyEx(keyhand, indexs, sName, Ret, ByVal 0&, vbNullString, ByVal 0&, ByVal 0&) <> ERROR_NO_MORE_ITEMS
If strlistsubkey = "" Then
strlistsubkey = Left$(sName, Ret)
Else
strlistsubkey = strlistsubkey + "_" + Left$(sName, Ret)
End If
indexs = indexs + 1
Wend
ListSubkey = strlistsubkey
End Function'读写注册表