[求助]vb6.0中能否通过API查询到注册表中某个键下子键的名称?(判断AutoCAD版本, 用
[求助]vb6.0中能否通过API查询到注册表中某个键下子键的名称?(判断AutoCAD版本, 用于分发VBA)<FONT face=宋体>我想通过查询注册表中Autodesk\AutoCAD键下子键的名称来判断AutoCAD的版本,同时再通过查询R16.1(本人的是2005)键下ACAD-301:804子键的Location项得到AutoCAD的安装路径,但是API中好象没有查询子键名称的函数,是我学得不够还是就没有这个函数?<BR>主要是想通过修改C:\Program Files\AutoCAD 2005\Support目录下auto2005.lsp文件达到运行AutoCAD就能运行自己的VBA文件的目的?也就是主要解决VBA程序分发给别人,自己用vb6.0做一个相当于安装程序的小程序。<BR>(以上注册表键名及文件路径只是本人机器上的,因版本和个人而异)</FONT>回复
本帖最后由 作者 于 2005-4-24 9:40:04 编辑 <br /><br /> <BR>'Description:<BR>'<BR>'This module exposes some very powerful methods that allow you to read and write to the Windows<BR>'registry. All methods are effective and, when used correctly, safe.<BR>'However; because altering the registry can cause serious problems you use these procedures at your own risk!'Snippet Code: - Registry Module<BR>'<BR>'
Option Explicit<BR>'@~~~~~~~~ API Constants for Win32 Reg. ~~~~~~~~~~~@<BR>Public Const REG_SZ = 1<BR>Public Const REG_EXPAND_SZ = 2<BR>Public Const REG_BINARY = 3<BR>Public Const REG_DWORD = 4
Public Const HKEY_CLASSES_ROOT = &H80000000<BR>Public Const HKEY_CURRENT_USER = &H80000001<BR>Public Const HKEY_LOCAL_MACHINE = &H80000002<BR>Public Const HKEY_USERS = &H80000003<BR>Public Const HKEY_PERFORMANCE_DATA = &H80000004<BR>Public Const HKEY_CURRENT_CONFIG = &H80000005<BR>Public Const HKEY_DYN_DATA = &H80000006
Public Const REG_OPTION_NON_VOLATILE = 0<BR>Public Const REG_CREATED_NEW_KEY = &H1<BR>Public Const REG_OPENED_EXISTING_KEY = &H2
Public Const KEY_QUERY_VALUE = &H1<BR>Public Const KEY_ENUMERATE_SUB_KEYS = &H8<BR>Public Const KEY_NOTIFY = &H10<BR>Public Const READ_CONTROL = &H20000<BR>Public Const STANDARD_RIGHTS_ALL = &H1F0000<BR>Public Const STANDARD_RIGHTS_EXECUTE = (READ_CONTROL)<BR>Public Const STANDARD_RIGHTS_READ = (READ_CONTROL)<BR>Public Const STANDARD_RIGHTS_REQUIRED = &HF0000<BR>Public Const SYNCHRONIZE = &H100000<BR>Public Const KEY_READ = ((STANDARD_RIGHTS_READ Or KEY_QUERY_VALUE Or KEY_ENUMERATE_SUB_KEYS Or KEY_NOTIFY) And (Not SYNCHRONIZE))<BR>Public Const KEY_SET_VALUE = &H2<BR>Public Const KEY_CREATE_SUB_KEY = &H4<BR>Public Const KEY_CREATE_LINK = &H20<BR>Public Const STANDARD_RIGHTS_WRITE = (READ_CONTROL)<BR>Public Const KEY_WRITE = ((STANDARD_RIGHTS_WRITE Or KEY_SET_VALUE Or KEY_CREATE_SUB_KEY) And (Not SYNCHRONIZE))<BR>Public Const KEY_ALL_ACCESS = ((STANDARD_RIGHTS_ALL Or KEY_QUERY_VALUE Or KEY_SET_VALUE Or KEY_CREATE_SUB_KEY Or KEY_ENUMERATE_SUB_KEYS Or KEY_NOTIFY Or KEY_CREATE_LINK) And (Not SYNCHRONIZE))
Public Const ERROR_SUCCESS = 0&<BR>Public Const ERROR_ACCESS_DENIED = 5&<BR>Public Const ERROR_NO_MORE_ITEMS = 259&<BR>Public Const ERROR_BADKEY = 1010&<BR>Public Const ERROR_CANTOPEN = 1011&<BR>Public Const ERROR_CANTREAD = 1012&<BR>Public Const ERROR_REGISTRY_CORRUPT = 1015&
'@~~~~~~~~~~~~~~~~~ API Types ~~~~~~~~~~~~~~~~@
Type SECURITY_ATTRIBUTES<BR>nLength As Long<BR>lpSecurityDescriptor As Long<BR>bInheritHandle As Boolean<BR>End Type
Public Type FILETIME<BR>dwLowDateTime As Long<BR>dwHighDateTime As Long<BR>End Type
'@~~~~~~~~~~~~~~~~~~ The Declares ~~~~~~~~~~~~~~~~~~~~~@
Public Declare Function RegOpenKeyEx Lib "advapi32.dll" Alias _<BR>"RegOpenKeyExA" (ByVal hKey As Long, ByVal lpSubKey As String, _<BR>ByVal ulOptions As Long, ByVal samDesired As Long, phkResult As Long) As Long
Public Declare Function RegQueryValueEx Lib "advapi32.dll" Alias _<BR>"RegQueryValueExA" (ByVal hKey As Long, ByVal lpValueName As _<BR>String, ByVal lpReserved As Long, lpType As Long, lpData As Any, _<BR>dwSize As Long) As Long
Public Declare Function RegCreateKeyEx Lib "advapi32" _<BR>Alias "RegCreateKeyExA" (ByVal hKey As Long, _<BR>ByVal lpSubKey As String, ByVal Reserved As Long, _<BR>ByVal lpClass As String, ByVal dwOptions As Long, _<BR>ByVal samDesired As Long, lpSecurityAttributes As SECURITY_ATTRIBUTES, _<BR>phkResult As Long, lpdwDisposition As Long) As Long
Public Declare Function RegSetValueEx Lib "advapi32.dll" _<BR>Alias "RegSetValueExA" (ByVal hKey As Long, _<BR>ByVal lpValueName As String, ByVal dwReserved As Long, _<BR>ByVal dwType As Long, lpValue As Any, ByVal dwSize As Long) As Long
Public Declare Function RegDeleteKey Lib "advapi32.dll" _<BR>Alias "RegDeleteKeyA" (ByVal hKey As Long, ByVal lpSubKey As String) As Long
Public Declare Function RegDeleteValue Lib "advapi32.dll" _<BR>Alias "RegDeleteValueA" (ByVal hKey As Long, _<BR>ByVal lpValueName As String) As Long<BR>Public Declare Function RegCloseKey Lib "advapi32.dll" _<BR>(ByVal hKey As Long) As Long
Public Declare Function RegConnectRegistry Lib "advapi32.dll" _<BR>Alias "RegConnectRegistryA" (ByVal lpMachineName As String, ByVal _<BR>hKey As Long, phkResult As Long) As Long
Public Declare Function RegCreateKey Lib "advapi32.dll" Alias _<BR>"RegCreateKeyA" (ByVal hKey As Long, ByVal lpSubKey As String, _<BR>phkResult As Long) As Long
Public Declare Function RegEnumKey Lib "advapi32.dll" Alias _<BR>"RegEnumKeyA" (ByVal hKey As Long, ByVal dwIndex As Long, ByVal _<BR>lpName As String, ByVal cbName As Long) As Long
Public Declare Function RegEnumValue Lib "advapi32.dll" Alias _<BR>"RegEnumValueA" (ByVal hKey As Long, ByVal dwIndex As Long, ByVal _<BR>lpValueName As String, lpcbValueName As Long, lpReserved As Long, _<BR>lpType As Long, lpData As Byte, lpcbData As Long) As Long
Public Declare Function RegEnumKeyEx Lib "advapi32.dll" Alias _<BR>"RegEnumKeyExA" (ByVal hKey As Long, ByVal dwIndex As Long, ByVal _<BR>lpName As String, lpcbName As Long, ByVal lpReserved As Long, ByVal _<BR>lpClass As String, lpcbClass As Long, lpftLastWriteTime As FILETIME) As Long
Public Declare Function RegLoadKey Lib "advapi32.dll" Alias "RegLoadKeyA" _<BR>(ByVal hKey As Long, ByVal lpSubKey As String, ByVal lpFile As String) As Long
Public Declare Function RegNotifyChangeKeyValue Lib "advapi32.dll" _<BR>(ByVal hKey As Long, ByVal bWatchSubtree As Long, ByVal dwNotifyFilter _<BR>As Long, ByVal hEvent As Long, ByVal fAsynchronus As Long) As Long
Public Declare Function RegOpenKey Lib "advapi32.dll" _<BR>(ByVal hKey As Long, ByVal lpSubKey As String, phkResult As Long) As Long
Public Declare Function OSRegQueryValue Lib "advapi32.dll" _<BR>Alias "RegQueryValueA" (ByVal hKey As Long, ByVal lpSubKey As _<BR>String, ByVal lpValue As String, lpcbValue As Long) As Long
Public Declare Function RegReplaceKey Lib "advapi32.dll" Alias _<BR>"RegReplaceKeyA" (ByVal hKey As Long, ByVal lpSubKey As String, _<BR>ByVal lpNewFile As String, ByVal lpOldFile As String) As Long
Public Declare Function RegRestoreKey Lib "advapi32.dll" Alias _<BR>"RegRestoreKeyA" (ByVal hKey As Long, ByVal lpFile As String, _<BR>ByVal dwFlags As Long) As Long
Public Declare Function RegQueryInfoKey Lib "advapi32.dll" Alias _<BR>"RegQueryInfoKeyA" (ByVal hKey As Long, ByVal lpClass As String, _<BR>lpcbClass As Long, ByVal lpReserved As Long, lpcSubKeys As Long, _<BR>lpcbMaxSubKeyLen As Long, lpcbMaxClassLen As Long, lpcValues As Long, _<BR>lpcbMaxValueNameLen As Long, lpcbMaxValueLen As Long, lpcbSecurityDescriptor _<BR>As Long, lpftLastWriteTime As FILETIME) As Long
'@~~~~~~~~~~~~~~ DeleteRegKey ~~~~~~~~~~~~~~~~~~~~@<BR>' BEWARE! WE CAN'T HELP YOU IF YOU DELETE THE WRONG KEY!<BR>' Always back up your registry before you use any of these<BR>' Methods. In fact, if you don't know EXACTLY what you are<BR>' Doing, stay safe and use the built in VB Registry methods.<BR>'@~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~@
Public Function DeleteRegKey(lngKey As Long, SubKey As String) As Long<BR> Dim lngRet As Long<BR> lngRet = RegDeleteKey(lngKey, SubKey)<BR> DeleteRegKey = lngRet<BR>End Function
'@~~~~~~~~~~~~~~ DeleteRegValue ~~~~~~~~~~~~~~~~~~@<BR>' Delete the value of a key, please use caution.<BR>'@~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~@
Public Function DeleteRegValue(lngKey As Long, SubKey As String, ValueName As String) As Long<BR> Dim lngRet As Long<BR> Dim lngKeyRet As Long
lngRet = RegOpenKeyEx(lngKey, SubKey, 0, KEY_WRITE, lngKeyRet)<BR> If lngRet <> ERROR_SUCCESS Then Exit Function
lngRet = RegDeleteValue(lngKeyRet, ValueName)<BR> DeleteRegValue = lngRet<BR> RegCloseKey lngKeyRet<BR>End Function
'@~~~~~~~~~~~~~~~~ WriteRegLong ~~~~~~~~~~~~~~~~~~@<BR>' Write a long Data type to a key<BR>'@~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~@
Public Function WriteRegLong(lngKey As Long, SubKey As String, _<BR> DataName As String, datavalue As Long) As Long
Dim SEC As SECURITY_ATTRIBUTES<BR> Dim lngKeyRet As Long<BR> Dim lngDis As Long<BR> Dim lngRet As Long
lngRet = RegCreateKeyEx(lngKey, SubKey, 0, "", REG_OPTION_NON_VOLATILE, _<BR> KEY_ALL_ACCESS, SEC, lngKeyRet, lngDis)
If (lngRet = ERROR_SUCCESS) Or (lngRet = REG_CREATED_NEW_KEY) Or _<BR> (lngRet = REG_OPENED_EXISTING_KEY) Then<BR> lngRet = RegSetValueEx(lngKeyRet, DataName, 0&, REG_DWORD, datavalue, 4)<BR> RegCloseKey lngKeyRet<BR> End If<BR> WriteRegLong = lngRet<BR>End Function<BR>Public Function WriteStringValue(lngKey As Long, SubKey As String, _<BR> DataName As String, datavalue As String) As Long
Dim SEC As SECURITY_ATTRIBUTES<BR> Dim lngKeyRet As Long<BR> Dim lngDis As Long<BR> Dim lngRet As Long
lngRet = RegCreateKeyEx(lngKey, _<BR> SubKey, 0, vbNullString, _<BR> REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, _<BR> SEC, lngKeyRet, lngDis)<BR> 'Trust me on this next line...<BR> If datavalue <= "" Then datavalue = ""<BR> If (lngRet = ERROR_SUCCESS) Or (lngRet = REG_CREATED_NEW_KEY) Or _<BR> (lngRet = REG_OPENED_EXISTING_KEY) Then<BR> lngRet = RegSetValueEx(lngKeyRet, DataName, 0&, _<BR> REG_SZ, ByVal datavalue, Len(datavalue))<BR> RegCloseKey lngKeyRet<BR> End If<BR> WriteStringValue = lngRet<BR>End Function
'@~~~~~~~~~~~~~~~~ ReadRegVal ~~~~~~~~~~~~~~~~~~~~~~~@<BR>' Read a value! see example AppInfo<BR>'@~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~@<BR>Public Function ReadRegVal(lngKey As Long, SubKey As String, _<BR> DataName As String, DefaultData As Variant) As Variant<BR> Dim lngKeyRet As Long<BR> Dim lngData As Long<BR> Dim strData As String<BR> Dim Datatype As Long<BR> Dim DataSize As Long<BR> Dim lngRet As Long<BR> ReadRegVal = DefaultData<BR> lngRet = RegOpenKeyEx(lngKey, SubKey, 0, KEY_QUERY_VALUE, lngKeyRet)<BR> If lngRet <> ERROR_SUCCESS Then Exit Function<BR> 'If you declare a lpData as a string (that's DataName in this function) you<BR> 'must pass it ByVal as shown here<BR> lngRet = RegQueryValueEx(lngKeyRet, DataName, 0&, Datatype, ByVal 0, DataSize)<BR> If lngRet <> ERROR_SUCCESS Then<BR> RegCloseKey lngKeyRet<BR> Exit Function<BR> End If<BR> Select Case Datatype<BR> Case REG_SZ<BR> strData = Space(DataSize + 1)<BR> lngRet = RegQueryValueEx(lngKeyRet, DataName, 0&, Datatype, ByVal strData, DataSize)<BR> If lngRet = ERROR_SUCCESS Then<BR> ReadRegVal = CVar(StripNulls(RTrim$(strData)))<BR> End If<BR> Case REG_DWORD<BR> lngRet = RegQueryValueEx(lngKeyRet, DataName, 0&, Datatype, lngData, 4)<BR> If lngRet = ERROR_SUCCESS Then<BR> ReadRegVal = CVar(lngData)<BR> End If<BR> End Select<BR> RegCloseKey lngKeyRet<BR>End Function
'@~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~@<BR>' Reads all of the subkeys under strKey. NOTE VB<BR>' users, you can change this function to return<BR>' a string array!<BR>'@~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~@<BR>Public Function GetSubKeys(strKey As String, SubKey As String, ByRef SubKeyCnt As Long) As String<BR> Dim strValues() As String<BR> Dim strTemp As String<BR> Dim lngSub As Long<BR> Dim intCnt As Integer<BR> Dim lngRet As Long<BR> Dim intKeyCnt As Integer<BR> Dim FT As FILETIME
lngRet = RegOpenKeyEx(strKey, SubKey, 0, KEY_ENUMERATE_SUB_KEYS, lngSub)
If lngRet <> ERROR_SUCCESS Then<BR> SubKeyCnt = 0<BR> Exit Function<BR> End If<BR> lngRet = RegQueryInfoKey(lngSub, vbNullString, 0, 0, SubKeyCnt, _<BR> 65, 0, 0, 0, 0, 0, FT)<BR> If (lngRet <> ERROR_SUCCESS) Or (SubKeyCnt <= 0) Then<BR> SubKeyCnt = 0<BR> End If<BR> ReDim strValues(SubKeyCnt - 1)<BR> For intCnt = 0 To SubKeyCnt - 1<BR> strValues(intCnt) = String$(65, 0)<BR> RegEnumKeyEx lngSub, intCnt, strValues(intCnt), 65, 0, vbNullString, 0, FT<BR> strValues(intCnt) = StripNulls(strValues(intCnt))<BR> Next intCnt<BR> RegCloseKey lngSub<BR> For intKeyCnt = LBound(strValues) To UBound(strValues)<BR> strTemp = strTemp & strValues(intKeyCnt) & ","<BR> Next intKeyCnt<BR> GetSubKeys = strTemp<BR>End Function<BR><A href="mailto:'@~~~~~~~~~~~~~~StripNulls" target="_blank" >'@~~~~~~~~~~~~~~StripNulls</A>~~~~~~~~~~~~~~~~~~~~@<BR>' Many API functions have null terminated strings<BR>' this handy function removes the null values<BR>' From the MS KB<BR>'@~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~@<BR>Function StripNulls(ByVal s As String) As String<BR> Dim i As Integer<BR> i = InStr(s, Chr$(0))<BR> If i > 0 Then<BR> StripNulls = Left$(s, i - 1)<BR> Else<BR> StripNulls = s<BR> End If<BR>End Function<BR><A href="mailto:'@~~~~~~~~~~~~~~ParseString" target="_blank" >'@~~~~~~~~~~~~~~ParseString</A>~~~~~~~~~~~~~~~~~~@<BR>' Use is simple: provide the delimited string, the<BR>' integer that represents the location in the string<BR>' you want to return, and the character that delimits<BR>' the string. Used in this module for VBA Users who<BR>' Need to read the string elements returned by<BR>' "GetSubKeys"<BR>'@~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~@<BR>Public Function ParseString(strIn As String, intLoc As Integer, strDelimiter As String) As String<BR> Dim intPos As Integer<BR> Dim intStrt As Integer<BR> Dim intStop As Integer<BR> Dim intCnt As Integer<BR> intCnt = intLoc<BR> Do While intCnt > 0<BR> intStop = intPos<BR> intStrt = InStr(intPos + 1, strIn, Left$(strDelimiter, 1))<BR> If intStrt > 0 Then<BR> intPos = intStrt<BR> intCnt = intCnt - 1<BR> Else<BR> intPos = Len(strIn) + 1<BR> Exit Do<BR> End If<BR> Loop<BR> ParseString = Mid$(strIn, intStop + 1, intPos - intStop - 1)<BR>End Function
'<b><FONT color=#f76809>例子</FONT></b>
<FONT color=#0033ff>'@~~~~~~~~~~~~ Sample of ReadRegVal ~~~~~~~~~~~~@<BR>Public Function WorkDirectory() As String<BR> Dim strVal As String<BR> strVal = ReadRegVal(HKEY_LOCAL_MACHINE, "SOFTWARE\DevCAD", "Location", strVal)<BR> WorkDirectory = strVal<BR>End Function</FONT>
<BR> 谢谢了!大概看了下,还未来得及仔细研究,不过已经发现GetSubKeys这个函数应该比较符合我的要求,先谢谢了! 到这里看看<A href="http://www.vbeach.net/bbs/forumdisplay.php?fid=11" target="_blank" >http://www.vbeach.net/bbs/forumdisplay.php?fid=11</A>
页:
[1]