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
vc聲明:,說明:,參數 類型及說明:,註解:,VC部分特別註解:,VC註冊表操作聲明及示例:,VB註冊表操作聲明及示例:,
vc聲明:
LONG RegQueryValue(
HKEY hkey,
LPCTSTR lpSubkey,
LPTSTR lpValue,
PLONG lpcbValue
);
HKEY hkey,
LPCTSTR lpSubkey,
LPTSTR lpValue,
PLONG lpcbValue
);
說明:
取得指定項或子項的默認(未命名)值
返回值
【Long】 0(ERROR_SUCCESS)表示成功。其他任何值都代表一個錯誤代碼。
參數 類型及說明:
hKey | 一個已打開表項的句柄,或者指定一個標準項名(即註冊表中的幾個根註冊表項) |
lpSubKey | 指向一個空終止的字元串指針,指示這個函式將打開或創建的表項的名稱。這個表項必須是由hkey參數所標識的子項。 |
lpValue | 一個返回值,指向一個快取區,用來獲得與指定子項默認值相關的一個字元串。 |
lpcbValue | 指定一個變數,用於裝載lpValue緩衝區的長度。一旦返回,它會設為實際載入緩衝區的位元組數量。該大小包含了數據長度還加上了終止符的空字元串。 |
註解:
win32應用程式應該使用RegQueryValueEx。lpValue被定義成一個字串,以維持同win16的兼容性(在win16中,值全都是字串)
VC部分特別註解:
如果在調用RegQueryValue函式時,將其第三個參數設定為NULL,第四個參數設定為非NULL,那么該函式將返回存儲數據的大小。這樣,可以讓應用程式確定一個最佳的方式來存儲將要獲取到的數據,然後再去獲取數據,也就是說,程式可以根據該函式此時返回的數據長度動態分配一塊記憶體,用來保存將要獲取的數據的大小,即,就可以採用這種方式,依次調用兩次RegQueryValue函式,第一次調用該函式得到將要獲取數據的長度,然後動態分配記憶體,接著再次調用該函式得到數據。
VC註冊表操作聲明及示例:
void C某函式::On某成員函式()
{
LONG lValue;
RegQueryValue(HKEY_LOCAL_MACHINE,"註冊表子項路徑",NULL,&lValue);
char *pBuf =new char[lValue];
RegQueryValue(HKEY_LOCAL_MACHINE,"註冊表子項路徑",pBuf,&lValue);
AfxMessageBox(pBuf);
}
{
LONG lValue;
RegQueryValue(HKEY_LOCAL_MACHINE,"註冊表子項路徑",NULL,&lValue);
char *pBuf =new char[lValue];
RegQueryValue(HKEY_LOCAL_MACHINE,"註冊表子項路徑",pBuf,&lValue);
AfxMessageBox(pBuf);
}
VB註冊表操作聲明及示例:
Public Declare Function RegOpenKey Lib "advapi32.dll" Alias "RegOpenKeyA" (ByVal hKey As Long, ByVal lpSubKey As String, phkResult As Long) As Long
Public 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 Byte, lpcbData As Long) As Long
Public Declare Function RegCloseKey Lib "advapi32.dll" (ByVal hKey As Long) As Long
Public Declare Function RegCreateKey Lib "advapi32.dll" Alias "RegCreateKeyA" (ByVal hKey As Long, ByVal lpSubKey As String, phkResult As Long) As Long
Public 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
Public 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
Public 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 Declare Function RegDeleteValue Lib "advapi32.dll" Alias "RegDeleteValueA" (ByVal hKey As Long, ByVal lpValueName As String) As Long
Public Declare Function RegDeleteKey Lib "advapi32.dll" Alias "RegDeleteKeyA" (ByVal hKey As Long, ByVal lpSubKey As String) As Long
Public Const HKEY_CLASSES_ROOT = &H80000000
Public Const HKEY_CURRENT_USER = &H80000001
Public Const HKEY_LOCAL_MACHINE = &H80000002
Public Const HKEY_USERS = &H80000003
Private Const REG_SZ = 1&
Private Const REG_EXPAND_SZ = 2&
Private Const REG_BINARY = 3&
Private Const REG_DWORD = 4&
Private Const ERROR_SUCCESS = 0&
'==================================================
'================================
'註冊表操作函式
'================================
'讀取註冊表字元串鍵值
Public Function GetString(hKey As Long, strPath As String, strValue As String)
Dim keyhand As Long
Dim lResult As Long
Dim strBuf As String
Dim lDataBufSize As Long
Dim intZeroPos As Integer
Dim lValueType As Long 'new add
RegOpenKey hKey, strPath, keyhand
lResult = RegQueryValueEx(keyhand, strValue, 0&, lValueType, ByVal 0&, lDataBufSize)
If lValueType = REG_SZ Or lValueType = REG_EXPAND_SZ Then
strBuf = String(lDataBufSize, " ")
lResult = RegQueryValueEx(keyhand, strValue, 0&, lValueType, 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 If
End Function
'寫入註冊表字元串鍵值
Public Sub SetString(hKey As Long, strPath As String, strValue As String, strdata As String)
Dim keyhand As Long
RegCreateKey hKey, strPath, keyhand
RegSetValueEx keyhand, strValue, 0, REG_SZ, ByVal strdata, Len(strdata)
RegCloseKey keyhand
End Sub
'讀取註冊表 DWORD 鍵值
Function GetDword(ByVal hKey As Long, ByVal strPath As String, ByVal strValueName As String) As Long
Dim lResult As Long
Dim lValueType As Long
Dim lBuf As Long
Dim lDataBufSize As Long
Dim r As Long
Dim keyhand As Long
r = RegOpenKey(hKey, strPath, keyhand)
' Get length/data type
lDataBufSize = 4
lResult = RegQueryValueEx(keyhand, strValueName, 0&, lValueType, lBuf, lDataBufSize)
If lResult = ERROR_SUCCESS Then
If lValueType = REG_DWORD Then
GetDword = lBuf
End If
'Else
' Call errlog("GetDWORD-" & strPath, False)
End If
r = RegCloseKey(keyhand)
End Function
'寫入註冊表 DWORD 鍵值
Function SetDword(ByVal hKey As Long, ByVal strPath As String, ByVal strValueName As String, ByVal lData As Long)
Dim keyhand As Long
RegCreateKey hKey, strPath, keyhand
RegSetValueEx keyhand, strValueName, 0&, REG_DWORD, lData, 4
RegCloseKey keyhand
End Function
'讀取註冊表二進制鍵值
Function GetBinary(ByVal hKey As Long, ByVal strPath As String, ByVal strValueName As String) As Long
Dim lResult As Long
Dim lValueType As Long
Dim lBuf As Long
Dim lDataBufSize As Long
Dim r As Long
Dim keyhand As Long
r = RegOpenKey(hKey, strPath, keyhand)
' Get length/data type
lDataBufSize = 4
lResult = RegQueryValueEx(keyhand, strValueName, 0&, lValueType, lBuf, lDataBufSize)
If lResult = ERROR_SUCCESS Then
If lValueType = REG_BINARY Then
GetBinary = lBuf
End If
End If
r = RegCloseKey(keyhand)
End Function
'寫入註冊表二進制鍵值
Function SetBinary(ByVal hKey As Long, ByVal strPath As String, ByVal strValueName As String, ByVal lData As Long, ByVal BitNumber As Long)
Dim keyhand As Long
RegCreateKey hKey, strPath, keyhand
RegSetValueEx keyhand, strValueName, 0&, REG_BINARY, lData, BitNumber
RegCloseKey keyhand
End Function
'刪除一個註冊表鍵值
Public Function DeleteValue(ByVal hKey As Long, ByVal strPath As String, ByVal strValue As String)
Dim keyhand As Long
RegOpenKey hKey, strPath, keyhand
RegDeleteValue keyhand, strValue
RegCloseKey keyhand
End Function
'創建一個主鍵
Public Function CreateKey(ByVal hKey As Long, ByVal strKey As String)
Dim keyhand&
RegCreateKey hKey, strKey, keyhand
RegCloseKey keyhand&
End Function