'添加 Command1 设置首页 Command2 读取设置的值Option Explicit Private Declare Function RegOpenKey Lib "advapi32.dll" Alias "RegOpenKeyA" (ByVal hKey As Long, ByVal lpSubKey As String, phkResult As Long) As Long Private Declare Function RegCreateKey Lib "advapi32.dll" Alias "RegCreateKeyA" (ByVal hKey As Long, ByVal lpSubKey As String, phkResult As Long) As Long Private 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 Private Declare Function RegCloseKey Lib "advapi32.dll" (ByVal hKey As Long) As Long Private 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 Const REG_SZ As Long = 1 Const REG_BINARY = 3 Const HKEY_CURRENT_USER = &H80000001 Dim lResult&, lValueType&, strBuf$, lDataBufSize&, hKey&, ret Private Sub Command1_Click() RegCreateKey HKEY_CURRENT_USER, "Software\Microsoft\Internet Explorer\Main", hKey RegSetValueEx hKey, "Start Page", 0, REG_SZ, ByVal "http://baidu.com", 19 MsgBox "IE 首页已成功设置" RegCloseKey hKey End SubPrivate Sub Command2_Click() ret = GetString(HKEY_CURRENT_USER, "Software\Microsoft\Internet Explorer\Main", "Start Page") MsgBox IIf(ret = "", "没设置", ret) End SubFunction GetString(hKey As Long, strPath As String, strValue As String) RegOpenKey hKey, strPath, ret GetString = RegQueryStringValue(ret, strValue) RegCloseKey ret End FunctionFunction RegQueryStringValue(ByVal hKey As Long, ByVal strValueName As String) As String lResult = RegQueryValueEx(hKey, strValueName, 0, lValueType, ByVal 0, lDataBufSize) If lResult = 0 Then If lValueType = REG_SZ Then strBuf = String(lDataBufSize, Chr$(0)) lResult = RegQueryValueEx(hKey, strValueName, 0, 0, ByVal strBuf, lDataBufSize) If lResult = 0 Then RegQueryStringValue = Left(strBuf, InStr(1, strBuf, Chr$(0)) - 1) ElseIf lValueType = REG_BINARY Then Dim strData As Integer lResult = RegQueryValueEx(hKey, strValueName, 0, 0, strData, lDataBufSize) If lResult = 0 Then RegQueryStringValue = strData End If End If End Function
Private Declare Function RegOpenKey Lib "advapi32.dll" Alias "RegOpenKeyA" (ByVal hKey As Long, ByVal lpSubKey As String, phkResult As Long) As Long
Private Declare Function RegCreateKey Lib "advapi32.dll" Alias "RegCreateKeyA" (ByVal hKey As Long, ByVal lpSubKey As String, phkResult As Long) As Long
Private 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
Private Declare Function RegCloseKey Lib "advapi32.dll" (ByVal hKey As Long) As Long
Private 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
Const REG_SZ As Long = 1
Const REG_BINARY = 3
Const HKEY_CURRENT_USER = &H80000001
Dim lResult&, lValueType&, strBuf$, lDataBufSize&, hKey&, ret
Private Sub Command1_Click()
RegCreateKey HKEY_CURRENT_USER, "Software\Microsoft\Internet Explorer\Main", hKey
RegSetValueEx hKey, "Start Page", 0, REG_SZ, ByVal "http://baidu.com", 19
MsgBox "IE 首页已成功设置"
RegCloseKey hKey
End SubPrivate Sub Command2_Click()
ret = GetString(HKEY_CURRENT_USER, "Software\Microsoft\Internet Explorer\Main", "Start Page")
MsgBox IIf(ret = "", "没设置", ret)
End SubFunction GetString(hKey As Long, strPath As String, strValue As String)
RegOpenKey hKey, strPath, ret
GetString = RegQueryStringValue(ret, strValue)
RegCloseKey ret
End FunctionFunction RegQueryStringValue(ByVal hKey As Long, ByVal strValueName As String) As String
lResult = RegQueryValueEx(hKey, strValueName, 0, lValueType, ByVal 0, lDataBufSize)
If lResult = 0 Then
If lValueType = REG_SZ Then
strBuf = String(lDataBufSize, Chr$(0))
lResult = RegQueryValueEx(hKey, strValueName, 0, 0, ByVal strBuf, lDataBufSize)
If lResult = 0 Then RegQueryStringValue = Left(strBuf, InStr(1, strBuf, Chr$(0)) - 1)
ElseIf lValueType = REG_BINARY Then
Dim strData As Integer
lResult = RegQueryValueEx(hKey, strValueName, 0, 0, strData, lDataBufSize)
If lResult = 0 Then RegQueryStringValue = strData
End If
End If
End Function