Kelp-Space 是記錄一些生活雜事的Blog
如有任何程式設計的問題歡迎到 飛特技術論壇 討論

2008-07-26

抓取SubKey的資料(string)

呼叫範例:
複製程式碼(Copy to clipboard)
WinRARPath = Mid(GetKeyString(HKEY_CLASSES_ROOT, "Applications\WinRAR.exe\shell\open\command", ""), 2)
WinRARPath = Left(WinRARPath, InStr(1, WinRARPath, Chr(34), vbBinaryCompare) - 1)   '取得WinRAR安裝路徑


以下程式碼請複製到模組。
複製程式碼(Copy to clipboard)

Option Explicit

'''''''''''''''''''''''''''''''key'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
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 HKEY_CURRENT_CONFIG = &H80000005
Public Const HKEY_DYN_DATA = &H80000006

Public Const REG_NONE = 0
Public Const REG_SZ = 1
Public Const REG_EXPAND_SZ = 2
Public Const REG_BINARY = 3
Public Const REG_DWORD = 4
Public Const REG_DWORD_BIG_ENDIAN = 5
Public Const REG_MULTI_SZ = 7

Declare Function RegCreateKey Lib "advapi32.dll" Alias "RegCreateKeyA" (ByVal hKey As Long, ByVal lpSubKey As String, phkResult As Long) 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 RegCloseKey Lib "advapi32.dll" (ByVal hKey As Long) As Long
Declare Function RegQueryValue Lib "advapi32.dll" Alias "RegQueryValueA" (ByVal hKey As Long, ByVal lpSubKey As String, ByVal lpValue As String, lpcbValue 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 RegSetValue Lib "advapi32.dll" Alias "RegSetValueA" (ByVal hKey As Long, ByVal lpSubKey As String, ByVal dwType As Long, ByVal lpData As String, ByVal cbData 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 Long
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

Public Function GetKeyString(hKey As Long, Key As String, SubKey As String) As String
    Dim LKey As Long
    Dim SData As String
    Dim lenData As Long
    Dim typeData As Long

    If RegOpenKey(hKey, Key, LKey) = 0 Then '判斷key是否存在
        If RegQueryValueEx(LKey, SubKey, 0, typeData, ByVal vbNullString, lenData) = 0 Then '判斷subkey是否存在
            SData = String(lenData, Chr(0))
            RegQueryValueEx LKey, SubKey, 0, typeData, ByVal SData, lenData
            GetKeyString = Left(SData, InStr(SData, Chr(0)) - 1)
        End If
    End If
   
    RegCloseKey LKey
End Function

沒有留言:

張貼留言