中使用系统注册表 ini 文件大多用来寸放自己界面或者初始化信息,而应用程序要使用系统信息或者说要得知已在系统注册过的其他程序的一些信息就要读去" name="description" />

VB中使用系统注册表

发表于:2007-05-25来源:作者:点击数: 标签:系统宋体注册表使用
VB MI LY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">中使用系统注册表 ini 文件大多用来寸放自己界面或者初始化信息,而应用程序要使用系统信息或者说要得知已在系统注册过的其他程序的一些信息就要读去
 

VBMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">中使用系统注册表

ini文件大多用来寸放自己界面或者初始化信息,而应用程序要使用系统信息或者说要得知已在系统注册过的其他程序的一些信息就要读去系统注册表了,比如有些应用需要使用调用MS OutLook Express就需要从HKEY_LOCAL_MACHINE\Software\Microsoft\Outlook Express\中的InstallRoot中读取路径。

 

申明部分:

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 "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

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

Select 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", "Software\MicroSoft\Outlook Express\", "InstallRoot", 2)

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

Shell shstring

原文转自:http://www.ltesting.net