VB中使用系統登錄檔 (轉)

worldblog發表於2007-12-08
VB中使用系統登錄檔 (轉)[@more@] 

VB中使用登錄檔

ini大多用來寸放自己介面或者初始化資訊,而應用要使用系統資訊或者說要得知已在系統註冊過的其他程式的一些資訊就要讀去系統登錄檔了,比如有些應用需要使用MS Express就需要從HKEY_LOCAL_MACHINESoftwareOutlook Express中的Install中讀取路徑。:namespace prefix = o ns = "urn:schemas-microsoft-com::office" />

 

申明部分:

Option Explicit

Public Const READ_CONTROL = &H20000

Public Const STANDARD_RIGHTS_READ = (READ_CONTROL)

Public Const STANDARD_RIGHTS_WRITE = (READ_CONTROL)

Public Const KEY_QUERY_VALUE = &H1

Public Const KEY_SET_VALUE = &H2

Public Const KEY_CREATE_SUB_KEY = &H4

Public Const KEY_ENUMERATE_SUB_KEYS = &H8

Public Const KEY_NOTIFY = &H10

Public Const KEY_CREATE_LINK = &H20

Public Const SYNCHRONIZE = &H100000

Public Const STANDARD_RIGHTS_ALL = &H1F0000

Public Const KEY_READ = ((STANDARD_RIGHTS_READ Or KEY_QUERY_VALUE Or KEY_ENUMERATE_SUB_KEYS Or KEY_NOTIFY) And (Not SYNCHRONIZE))

Public Const KEY_WRITE = ((STANDARD_RIGHTS_WRITE Or KEY_SET_VALUE Or KEY_CREATE_SUB_KEY) And (Not SYNCHRONIZE))

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 KEY_EXECUTE = ((KEY_READ) And (Not SYNCHRONIZE))

Public Const ERROR_SUCCESS = 0&

Declare Function RegOpenKeyEx Lib "adv32.dll" Alias "RegOpenKeyExA" (ByVal hKey As Long, ByVal lpSubKey As String, ByVal ulOptions As Long, ByVal sesired As Long, 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 RegCloseKey Lib "advapi32.dll" (ByVal hKey As Long) As Long

Function sdaGetRegEntry(strKey As String, strSubKeys As String, strValName As String, lngType As Long) As String

On Error GoTo sdaGetRegEntry_Err

Dim lngResult As Long, lngKey As Long

Dim lngHandle As Long, lngcbData As Long

Dim strRet As String

Case strKey

Case "HKEY_CLASSES_ROOT": lngKey = &H80000000

Case "HKEY_CURRENT_CONFIG": lngKey = &H80000005

Case "HKEY_CURRENT_USER": lngKey = &H80000001

Case "HKEY_DYN_DATA": lngKey = &H80000006

Case "HKEY_LOCAL_MACHINE": lngKey = &H80000002

Case "HKEY_PERFORMANCE_DATA": lngKey = &H80000004

Case "HKEY_USERS": lngKey = &H80000003

Case Else: Exit Function

End Select

If Not ERROR_SUCCESS = RegOpenKeyEx(lngKey, strSubKeys, 0&, KEY_READ, lngHandle) Then Exit Function

lngResult = RegQueryValueEx(lngHandle, strValName, 0&, lngType, ByVal strRet, lngcbData)

strRet = Space(lngcbData)

lngResult = RegQueryValueEx(lngHandle, strValName, 0&, lngType, ByVal strRet, lngcbData)

If Not ERROR_SUCCESS = RegCloseKey(lngHandle) Then lngType = -1&

sdaGetRegEntry = strRet

sdaGetRegEntry_Exit:

On Error GoTo 0

Exit Function

sdaGetRegEntry_Err:

lngType = -1&

MsgBox Err & "> " & Error$, 16, "GenUtils/sdaGetRegEntry"

Resume sdaGetRegEntry_Exit

End Function

 

看起來很複雜,用起來卻簡單的緊,這就達到了複雜的目的了。

例如:使用OutLook Express

Dim outLook_Path As String

Dim shstring As String

outLook_Path = sdaGetRegEntry("HKEY_LOCAL_MACHINE", "SoftwareMicroSoftOutlook Express", "InstallRoot", 2)

shstring = Mid(Trim(outLook_Path), 1, Len(outLook_Path) - 1) & "msimn.exe"

shstring

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/10752043/viewspace-989754/,如需轉載,請註明出處,否則將追究法律責任。

相關文章