VB 版 (精华区)
发信人: alias (白桦林), 信区: VB
标 题: Win95系统API函数大揭秘(8)
发信站: 哈工大紫丁香 (Wed Jul 26 10:59:04 2000), 转信
发信人: yucheng (Bingo), 信区: VisualBasic
发信站: BBS 水木清华站 (Fri Feb 25 17:30:03 2000)
读写注册表
Win95及NT的注册表数据库(Registry)是系统中非常重要的组成部分,它设置了Win95及
NT的参数,
包括用户信息、系统硬件配置和应用程序等信息。注册表系统代替了旧版Windows中的多
个INI文件。(警
告:如果你对注册表不熟悉,不要随意修改它。如果注册表项目出错,会使机器崩溃,
甚至破坏操作系
统本身。)
Win32 API中Reg函数处理对注册表数据库一般的读写过程如下:
1、使用RegOpenKey或RegCreateKey打开或创建一个键;
2、如果上一步成功,使用RegQueryValue(或RegQueryValueEx)读取子键的值,使用Reg
SetValue(或
RegvSetValueEx)设置子键值,使用RegEnumKey获得所有子键,使用RegDeleteKey删除一
个键;
3、完成操作后使用RegCloseKey关闭键。
下述应用程序演示了如何生成键、存放值并取得注册表数据。在HKEY_LOCAL_MACHINE键
下面生成三
个子键Test\Mastering vb5、 Windows Width、Windows Height,用于存放上次执行
时的窗体尺寸。
Private Declare Function RegCreateKey Lib "advapi32.dll" Alias "RegCreateKey
A" (ByVal hKey As
Long, ByVal lpSubKey As String, phkResult As Long) As Long
Private Declare Function RegDeleteKey Lib "advapi32.dll" Alias "RegDeleteKey
A" (ByVal hKey As
Long, ByVal lpSubKey As String) As Long
Private Declare Function RegDeleteValue Lib "advapi32.dll" Alias "RegDeleteV
alueA" (ByVal hKey
As Long, ByVal lpValueName As String) As Long
Private Declare Function RegQueryValueEx Lib "advapi32.dll" Alias "RegQueryV
alueExA" (ByVal
hKey As Long, ByVal lpValueName As String, ByVal lpReserved As Long, lpType
As Long, lpData
As Any, lpcbData As Long) As Long ' Note that if you declare the lpData para
meter as
String, you must pass it By Value.
Private Declare Function RegSetValueEx Lib "advapi32.dll" Alias "RegSetValue
ExA" (ByVal hKey
As Long, ByVal lpValueName As String, ByVal Reserved As Long, ByVal dwType A
s Long, lpData As
Any, ByVal cbData As Long) As Long ' Note that if you declare the lpData par
ameter as
String, you must pass it By Value.
Const ERROR_SUCCESS = 0&
Const ERROR_BADDB = 1009&
Const ERROR_BADKEY = 1010&
Const ERROR_CANTOPEN = 1011&
Const ERROR_CANTREAD = 1012&
Const ERROR_CANTWRITE = 1013&
Const ERROR_REGISTRY_RECOVERED = 1014&
Const ERROR_REGISTRY_CORRUPT = 1015&
Const ERROR_REGISTRY_IO_FAILED = 1016&
Const HKEY_CLASSES_ROOT = &H80000000
Const HKEY_CURRENT_USER = &H80000001
Const HKEY_LOCAL_MACHINE = &H80000002
Const regkey = "Test\Mastering vb5"
Private Sub Form_Load()
Dim retValue As Long
Dim result As Long
Dim keyValue As String
Dim keyId As Long
Dim subKey As String
Dim bufSize As Long
Label6.Caption = regkey
retValue = RegCreateKey(HKEY_LOCAL_MACHINE, regkey, keyId)
If retValue = 0 Then
subKey = "Windows Width"
retValue = RegQueryValueEx(keyId, subKey, 0&, reg_sz, 0&, bufSize)
If bufSize < 2 Then
keyValue = Me.Width
retValue = RegSetValueEx(keyId, subKey, 0&, reg_sz, ByVal keyValue, Len(keyV
alue) + 1)
Else
keyValue = String(bufSize + 1, "")
retValue = RegQueryValueEx(keyId, subKey, 0&, reg_sz, bykeyvalue, bufSize)
keyValue = Left$(keyValue, bufSize - 1)
Me.Width = keyValue
End If
Label4.Caption = subKey
Label5.Caption = Me.Width
subKey = "Widows Height"
retValue = RegQueryValueEx(keyId, subKey, 0&, reg_sz, 0&, bufSize)
If bufSize < 2 Then
keyValue = Me.Height
retValue = RegSetValueEx(keyId, subKey, 0&, reg_sz, ByVal keyValue, Len(keyV
alue) + 1)
Else
keyValue = String(bufSize + 1, "")
retValue = RegQueryValueEx(keyId, subKey, 0&, reg_sz, ByVal keyValue, bufSiz
e - 1)
Me.Height = keyValue
End If
Label8.Caption = subKey
Label7.Caption = Me.Height
End If
End Sub
Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
Dim keyValue As String
Dim retValue As Long
Dim keyId As Long
retValue = RegCreateKey(HKEY_LOCAL_MACHINE, regkey, keyId)
keyValue = Me.Width
retValue = RegSetValueEx(keyId, "Windows Width", 0&, reg_sz, ByVal keyValue,
Len(keyValue) +
1)
keyValue = Me.Height
retValue = RegSetValueEx(keyId, "Windows Height", 0&, reg_sz, ByVal keyValue
, Len(keyValue)
+ 1)
End Sub
--
有人说我捂着脸的时候特象周润发,你信不信?
Flying Fox
Flying Fox
Flying Fox
Flying Fox
Flying Fox
--
☆ 来源:.哈工大紫丁香 bbs.hit.edu.cn.[FROM: mig.bbs@smth.org]
Powered by KBS BBS 2.0 (http://dev.kcn.cn)
页面执行时间:3.411毫秒