两个问题:
1、请写一读写ini文件的例子,
2、请写一读写注册表的例子。
我打算把数据库连接信息写到文件或者注册表里面:
contextDb = "DSN=DBName;uid=sa;pwd=sa;database=DBName"
1、请写一读写ini文件的例子,
2、请写一读写注册表的例子。
我打算把数据库连接信息写到文件或者注册表里面:
contextDb = "DSN=DBName;uid=sa;pwd=sa;database=DBName"
解决方案 »
- windowsmediaplayer控件控制的问题.急!!!!!!?
- 求《Visual Basic 6.0实战讲座》
- VB中如何将模态窗体转换成普通窗体
- 在datagrid中直接输入的数据如何用代码来保存?请各位指点,谢谢!
- 用data控件如何关联远程access
- 关于任子行 只有70分了 不好意思
- 同志们何不转学C#?
- 大家帮忙看看,下面的代码能在串口读字符串,可是如何实十六进制的读取
- 怎样改变图片的影像分辨率?
- 对等网内windows是怎么知道别的机器上了局域网的?
- 谁能提供一些帮助文件(chm格式)的编写工具,请多多帮忙回复就给分!!!!!!!!
- 请问这条语句有什么错误..........
建立与读取.ini文件
虽然进入win95之後,一般读写ini文件被读写Registry所取代,但我们还是可以透过
win31的传统方式读写ini文件,以存程式目前的相关设定,而於下一次程式执行时再
读回来。目前建议使用GetSetting SaveSetting的方式存於Registry中,不用目前
的方式。 储存程式的设定
'请於form中放3个TextBox,一个CommandBox
Private Declare Function GetPrivateProfileString Lib "kernel32" _
Alias "GetPrivateProfileStringA" (ByVal lpApplicationName As String, _
ByVal lpKeyName As Any, ByVal lpDefault As String, _
ByVal lpReturnedString As String, ByVal nSize As Long, _
ByVal lpFileName As String) As Long
Private Declare Function WritePrivateProfileString Lib "kernel32" _
Alias "WritePrivateProfileStringA" (ByVal lpApplicationName As String, _
ByVal lpKeyName As Any, ByVal lpString As Any, _
ByVal lpFileName As String) As LongPrivate Sub Command1_Click()
Dim success As Long
success = WritePrivateProfileString("MyApp", "text1", Text1.Text, "c:\aa.ini")
'叁数一 Section Name
'叁数二 於.ini中的项目
'叁数三 项目的内容
'叁数四 .ini文件的名称
success = WritePrivateProfileString("MyApp", "text2", Text2.Text, "c:\aa.ini")
success = WritePrivateProfileString("MyApp2", "text3", Text3.Text, "c:\aa.ini")
End SubPrivate Sub Form_load()
Dim ret As Long
Dim buff As String
buff = String(255, 0)
ret = GetPrivateProfileString("Myapp", "text1", "text1", buff, 256, "c:\aa.ini")
'若.ini MyApp中无text1,则采用叁数三的值
Text1.Text = buff
buff = String(255, 0)
ret = GetPrivateProfileString("Myapp", "text2", "text2", buff, 256, "c:\aa.ini")
Text2.Text = buff
buff = String(255, 0)
ret = GetPrivateProfileString("Myapp2", "text3", "text3", buff, 256, "c:\aa.ini")
Text3.Text = buff
End Sub
VB中如何实现注册表的读写
---- “API”中专门提供了处理注册表的函数,不用说也知道,它们肯定功能强大(因为它们是API),但是它们的难度也是不小的。如果你是一个入门者的话,这大概会令你头痛一阵子。不过VB本身还提供了几个函数,专门用来处理注册表,相对来说它们就简单多了,所以我只对它们做一下说明: SaveSetting appname, section, key, value
---- 这句话的作用是将应用程序的信息存入注册表中。其中四个参数都是必要的,其含义分别为:应用程序名,区段,键,值。比如我们要为我们的程序(名为“测试”)的主窗体的位置进行存储,就可以写成: SaveSetting “测试”,”main”,”
top”,frmMain.Top
SaveSetting “测试”,”main”,”
Left”,frmMain.Left
SaveSetting “测试”,”main”,”
Width”,frmMain.Width
SaveSetting “测试”,”main”,”
Height”,frmMain.Height
---- 这样,就把当前的主窗体的位置进行了记录,在下次打开这个窗体的时候,我们就可以在把这些值取出来。那么如何才能把我们所存储的值取出来呢?这就要用到另一个函数:GetSetting,它的语法是: GetSetting(appname, section, key[, default])
---- 该函数的各部分的意义均和上面的函数的意义相同,所不同的是多了一个default而少了一个value。这个函数最后返回的结果即value值,而default指的是如果所指定的键不存在时,则以default指定的值为返回结果。如果没指定default,则default的默认值是””,即0长度的字符串。接上例,在程序刚开始的时候,我们应该取出上一次所存储的主窗体的位置,可以写成: frmMain.Top = GetSetting
(“测试”,”main”,”top”)
frmMain.Left = GetSetting
(“测试”,”main”,”Left”)
frmMain.Height = GetSetting
(“测试”,”main”,”Height”)
frmMain.Width = GetSetting
(“测试”,”main”,”Width”)
---- 好了,到此为止,可以暂且告一段落,让我们打开注册表,看一看我们把信息是记录在什么地方了。按照下面的路径打开: HKEY_CURRENT_USER\Software\VB And VBA Program Settings 你会看到你的“测试”就在里边放着。 ---- 当你的应用程序最终被卸载时,你应该考虑(而且你应该这样做)把你写入注册表中的内容清除掉,因为离开了你的应用程序,它们无疑就是一推垃圾而已。要想将它们删掉,我们可以用函数:DeleteSetting,它的语法如下:DeleteSetting appname, [section[, key]] 在几个参数中,只有appname是必须地,其它两项均可选。比如我们要删除窗体的“高”这键,则可以写成:DeleteSetting “测试”,”main”,”Height”如果我们要将”main”这个段整个去掉,则可以写成DeleteSetting “测试”,”main” 如果我们再彻底一些,想将整个应用程序去掉的话,就干脆直接写成: DeleteSetting “测试”
---- 这种方法的不足之处是只能把信息记录在指定的位置,即 HKEY_CURRENT_USER\Software\VB
And VBA Program Settings
---- 如果要写入到别的地方,就要借助于API了。
Const REG_SZ = 1 ' Unicode nul terminated string
Const REG_BINARY = 3 ' Free form binary
Const HKEY_CURRENT_USER = &H80000001
Private Declare Function RegCloseKey Lib "advapi32.dll" (ByVal hKey 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 RegDeleteValue Lib "advapi32.dll" Alias "RegDeleteValueA" (ByVal hKey As Long, ByVal lpValueName As String) As Long
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 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 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
Function RegQueryStringValue(ByVal hKey As Long, ByVal strValueName As String) As String
Dim lResult As Long, lValueType As Long, strBuf As String, lDataBufSize As Long
'retrieve nformation about the key
lResult = RegQueryValueEx(hKey, strValueName, 0, lValueType, ByVal 0, lDataBufSize)
If lResult = 0 Then
If lValueType = REG_SZ Then
'Create a buffer
strBuf = String(lDataBufSize, Chr$(0))
'retrieve the key's content
lResult = RegQueryValueEx(hKey, strValueName, 0, 0, ByVal strBuf, lDataBufSize)
If lResult = 0 Then
'Remove the unnecessary chr$(0)'s
RegQueryStringValue = Left$(strBuf, InStr(1, strBuf, Chr$(0)) - 1)
End If
ElseIf lValueType = REG_BINARY Then
Dim strData As Integer
'retrieve the key's value
lResult = RegQueryValueEx(hKey, strValueName, 0, 0, strData, lDataBufSize)
If lResult = 0 Then
RegQueryStringValue = strData
End If
End If
End If
End Function
Function GetString(hKey As Long, strPath As String, strValue As String)
Dim Ret
'Open the key
RegOpenKey hKey, strPath, Ret
'Get the key's content
GetString = RegQueryStringValue(Ret, strValue)
'Close the key
RegCloseKey Ret
End Function
Sub SaveString(hKey As Long, strPath As String, strValue As String, strData As String)
Dim Ret
'Create a new key
RegCreateKey hKey, strPath, Ret
'Save a string to the key
RegSetValueEx Ret, strValue, 0, REG_SZ, ByVal strData, Len(strData)
'close the key
RegCloseKey Ret
End Sub
Sub SaveStringLong(hKey As Long, strPath As String, strValue As String, strData As String)
Dim Ret
'Create a new key
RegCreateKey hKey, strPath, Ret
'Set the key's value
RegSetValueEx Ret, strValue, 0, REG_BINARY, CByte(strData), 4
'close the key
RegCloseKey Ret
End Sub
Sub DelSetting(hKey As Long, strPath As String, strValue As String)
Dim Ret
'Create a new key
RegCreateKey hKey, strPath, Ret
'Delete the key's value
RegDeleteValue Ret, strValue
'close the key
RegCloseKey Ret
End Sub
Private Sub Command1_Click()
Dim strString As String
'Ask for a value
strString = InputBox("Please enter a value between 0 and 255 to be saved as a binary value in the registry.", App.Title)
If strString = "" Or Val(strString) > 255 Or Val(strString) < 0 Then
MsgBox "Invalid value entered ...", vbExclamation + vbOKOnly, App.Title
Exit Sub
End If
'Save the value to the registry
SaveStringLong HKEY_CURRENT_USER, "KPD-Team", "BinaryValue", CByte(strString)
End Sub
Private Sub Command2_Click()
'Get a string from the registry
Ret = GetString(HKEY_CURRENT_USER, "KPD-Team", "BinaryValue")
If Ret = "" Then MsgBox "No value found !", vbExclamation + vbOKOnly, App.Title: Exit Sub
MsgBox "The value is " + Ret, vbOKOnly + vbInformation, App.Title
End Sub
Private Sub Command3_Click()
'Delete the setting from the registry
DelSetting HKEY_CURRENT_USER, "KPD-Team", "BinaryValue"
MsgBox "The value was deleted ...", vbInformation + vbOKOnly, App.Title
End Sub
Private Sub Form_Load() Command1.Caption = "Set Value"
Command2.Caption = "Get Value"
Command3.Caption = "Delete Value"
End Sub
Public Function GetServerName() As String
'功能: 数据库服务器的名称
Dim strName As String
Dim nNameLen As Integer
On Error GoTo errs
strName = Space(65)
nNameLen = GetPrivateProfileString("DataSource", "ServerName", "Server", strName, 64, strIniFileName)
GetServerName = Mid(Trim(strName), 1, nNameLen)
Exit Function
errs:
GetServerName = "Server"
End Function文件格式:
[DataSource]
ServerName = Server
DatabaseName = DATABASENAME读写注册表用API:
SAVESETTING
GETSETTING
Option Explicit
Private Declare Function GetPrivateProfileString Lib "kernel32" Alias "GetPrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As Any, ByVal lpDefault As String, ByVal lpReturnedString As String, ByVal nSize As Long, ByVal lpFileName As String) As Long
Private Declare Function WritePrivateProfileString Lib "kernel32" Alias "WritePrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As Any, ByVal lpString As Any, ByVal lpFileName As String) As Long'功能简介:对INI文件进行读操作
'参数一:文件路径
'参数二:条目的小节名称
'参数三:项名或条目名
Function GetProfileString(StrFileName As String, StrAppName As String, StrKeyName As String) As String
GetProfileString = String(255, 0)
GetPrivateProfileString StrAppName, StrKeyName, "", GetProfileString, 255, StrFileName
GetProfileString = Left(GetProfileString, InStr(GetProfileString, Chr(0)) - 1)
End Function'功能简介:对INI文件进行写操作
'参数一:文件路径
'参数二:条目的小节名称
'参数三:项名或条目名
'参数四:写操作字符串
Function WriteProfilestring(StrFileName As String, StrAppName As String, StrKeyName As String, StrWrite As String) As Boolean
On Error GoTo WriteErr
WritePrivateProfileString StrAppName, StrKeyName, StrWrite, StrFileName
WriteProfilestring = True
Exit Function
WriteErr:
End Function
INI文件读写:Private Declare Function GetPrivateProfileString Lib "kernel32" Alias "GetPrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As Any, ByVal lpDefault As String, ByVal lpReturnedString As String, ByVal nSize As Long, ByVal lpFileName As String) As Long
Private Declare Function WritePrivateProfileString Lib "kernel32" Alias "WritePrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As Any, ByVal lpString As Any, ByVal lpFileName As String) As Long
Private Sub Form_Load() Dim Ret As String, NC As Long
'Write the setting to the file (c:\test.ini) under
' Project1 -> Keyname
WritePrivateProfileString App.Title, "KeyName", "This is the value", "c:\test.ini"
'Create a buffer
Ret = String(255, 0)
'Retrieve the string
NC = GetPrivateProfileString(App.Title, "KeyName", "Default", Ret, 255, "C:\test.ini")
'NC is the number of characters copied to the buffer
If NC <> 0 Then Ret = Left$(Ret, NC)
'Show our string
MsgBox Ret
'Delete the file
Kill "c:\test.ini"
End Sub
INI文件读写:'require variable declaration
Option Explicit'declares for ini controlling
Private Declare Function GetPrivateProfileSection Lib "kernel32" Alias "GetPrivateProfileSectionA" (ByVal lpAppName As String, ByVal lpReturnedString As String, ByVal nSize As Long, ByVal lpFileName As String) As Long
Private Declare Function GetPrivateProfileString Lib "kernel32" Alias "GetPrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As Any, ByVal lpDefault As String, ByVal lpReturnedString As String, ByVal nSize As Long, ByVal lpFileName As String) As Long
Private Declare Function WritePrivateProfileSection Lib "kernel32" Alias "WritePrivateProfileSectionA" (ByVal lpAppName As String, ByVal lpString As String, ByVal lpFileName As String) As Long
Private Declare Function WritePrivateProfileString Lib "kernel32" Alias "WritePrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As Any, ByVal lpString As Any, ByVal lpFileName As String) As Long'when form is loaded
Private Sub Form_Load()'if error occures resume still
On Error Resume Next'local variables
Dim File As String, OFLen As Double, _
Str As String'set our varibles
File = "C:\temp.txt"
OFLen = FileLen(File)'write few example sections:
WriteIniSection File, "Test1", ""
WriteIniSection File, "Test2", "Here shoud be found some text"'write few ini strings
WriteIni File, "Test3", "Ini1", "This is ini 1"
WriteIni File, "Test1", "Ini2", "This is ini 2"'inform we're written the data
MsgBox Format((FileLen(File) - OFLen) / 1024, "0.00") & " KB data written to " & Chr(34) & File & Chr(34)'read the ini file
Str = Str & "Test2 section: " & vbTab & ReadIniSection(File, "Test2") & vbCrLf
Str = Str & "Test1 section: " & vbTab & ReadIniSection(File, "Test1") & vbCrLf
Str = Str & "Ini1 string: " & vbTab & ReadIni(File, "Test3", "Ini1") & vbCrLf
Str = Str & "Ini2 string: " & vbTab & ReadIni(File, "Test1", "Ini2") & vbCrLf'show data
MsgBox Str'end application
EndEnd Sub'// INI CONTROLLING PROCEDURES'reads ini string
Public Function ReadIni(Filename As String, Section As String, Key As String) As String
Dim RetVal As String * 255, v As Long
v = GetPrivateProfileString(Section, Key, "", RetVal, 255, Filename)
ReadIni = Left(RetVal, v - 1)
End Function'reads ini section
Public Function ReadIniSection(Filename As String, Section As String) As String
Dim RetVal As String * 255, v As Long
v = GetPrivateProfileSection(Section, RetVal, 255, Filename)
ReadIniSection = Left(RetVal, v - 1)
End Function'writes ini
Public Sub WriteIni(Filename As String, Section As String, Key As String, Value As String)
WritePrivateProfileString Section, Key, Value, Filename
End Sub'writes ini section
Public Sub WriteIniSection(Filename As String, Section As String, Value As String)
WritePrivateProfileSection Section, Value, Filename
End Sub
Option Explicit
Private Declare Function RegCloseKey Lib "advapi32" (ByVal hKey As Long) As Long
Private Declare Function RegCreateKeyEx Lib "advapi32" Alias "RegCreateKeyExA" (ByVal hKey As Long, ByVal lpSubKey As String, ByVal Reserved As Long, ByVal lpClass As String, ByVal dwOptions As Long, ByVal samDesired As Long, ByRef lpSecurityAttributes As SECURITY_ATTRIBUTES, ByRef phkResult As Long, ByRef lpdwDisposition As Long) As Long
Private Declare Function RegOpenKeyEx Lib "advapi32" Alias "RegOpenKeyExA" (ByVal hKey As Long, ByVal lpSubKey As String, ByVal ulOptions As Long, ByVal samDesired As Long, ByRef phkResult As Long) As Long
Private Declare Function RegQueryValueEx Lib "advapi32" Alias "RegQueryValueExA" (ByVal hKey As Long, ByVal lpValueName As String, ByVal lpReserved As Long, ByRef lpType As Long, ByVal lpData As String, ByRef lpcbData As Long) As Long
Private Declare Function RegSetValueEx Lib "advapi32" Alias "RegSetValueExA" (ByVal hKey As Long, ByVal lpValueName As String, ByVal Reserved As Long, ByVal dwType As Long, ByVal lpData As String, ByVal cbData As Long) As Long
Private Declare Function RegDeleteKey Lib "advapi32.dll" Alias "RegDeleteKeyA" (ByVal hKey As Long, ByVal lpSubKey As String) As Long
Private Declare Function RegDeleteValue Lib "advapi32.dll" Alias "RegDeleteValueA" (ByVal hKey As Long, ByVal lpValueName As String) As Long
Const REG_SZ = 1 ' Unicode空终结字符串
Const REG_EXPAND_SZ = 2 ' Unicode空终结字符串
Const REG_DWORD = 4 ' 32-bit 数字
Const REG_OPTION_NON_VOLATILE = 0 ' 当系统重新启动时,关键字被保留
Const READ_CONTROL = &H20000
Const KEY_QUERY_VALUE = &H1
Const KEY_SET_VALUE = &H2
Const KEY_CREATE_SUB_KEY = &H4
Const KEY_ENUMERATE_SUB_KEYS = &H8
Const KEY_NOTIFY = &H10
Const KEY_CREATE_LINK = &H20
Const KEY_READ = KEY_QUERY_VALUE + KEY_ENUMERATE_SUB_KEYS + KEY_NOTIFY + READ_CONTROL
Const KEY_WRITE = KEY_SET_VALUE + KEY_CREATE_SUB_KEY + READ_CONTROL
Const KEY_EXECUTE = KEY_READ
Const KEY_ALL_ACCESS = KEY_QUERY_VALUE + KEY_SET_VALUE + _
KEY_CREATE_SUB_KEY + KEY_ENUMERATE_SUB_KEYS + _
KEY_NOTIFY + KEY_CREATE_LINK + READ_CONTROL
Const HKEY_CLASSES_ROOT = &H80000000
Const HKEY_CURRENT_USER = &H80000001
Const HKEY_LOCAL_MACHINE = &H80000002
Const HKEY_USERS = &H80000003
Const HKEY_PERFORMANCE_DATA = &H80000004
Const ERROR_NONE = 0
Const ERROR_BADKEY = 2
Const ERROR_ACCESS_DENIED = 8
Const ERROR_SUCCESS = 0
Private Type SECURITY_ATTRIBUTES
nLength As Long
lpSecurityDescriptor As Long
bInheritHandle As Boolean
End TypePublic Function UpdateKey(KeyRoot As Long, KeyName As String, SubKeyName As String, SubKeyValue As String) As Boolean
Dim rc As Long ' 返回代码
Dim hKey As Long ' 处理一个注册表关键字
Dim hDepth As Long '
Dim lpAttr As SECURITY_ATTRIBUTES ' 注册表安全类型
lpAttr.nLength = 50 ' 设置安全属性为缺省值...
lpAttr.lpSecurityDescriptor = 0 ' ...
lpAttr.bInheritHandle = True ' ...
rc = RegCreateKeyEx(KeyRoot, KeyName, _
0, REG_SZ, _
REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, lpAttr, _
hKey, hDepth) ' 创建/打开//KeyRoot//KeyName
If (rc <> ERROR_SUCCESS) Then GoTo CreateKeyError ' 错误处理...
If (SubKeyValue = "") Then SubKeyValue = " " ' 要让RegSetValueEx() 工作需要输入一个空格...
' 创建/修改关键字值
rc = RegSetValueEx(hKey, SubKeyName, _
0, REG_SZ, _
SubKeyValue, LenB(StrConv(SubKeyValue, vbFromUnicode)))
If (rc <> ERROR_SUCCESS) Then GoTo CreateKeyError ' 错误处理
rc = RegCloseKey(hKey) ' 关闭关键字
UpdateKey = True ' 返回成功
Exit Function ' 退出
CreateKeyError:
UpdateKey = False ' 设置错误返回代码
rc = RegCloseKey(hKey) ' 试图关闭关键字
End FunctionPublic Function GetKeyValue(KeyRoot As Long, KeyName As String, SubKeyRef As String) As String
Dim i As Long ' 循环计数器
Dim rc As Long ' 返回代码
Dim hKey As Long ' 处理打开的注册表关键字
Dim hDepth As Long '
Dim sKeyVal As String
Dim lKeyValType As Long ' 注册表关键字数据类型
Dim tmpVal As String ' 注册表关键字的临时存储器
Dim KeyValSize As Long ' 注册表关键字变量尺寸
rc = RegOpenKeyEx(KeyRoot, KeyName, 0, KEY_ALL_ACCESS, hKey) ' 打开注册表关键字
If (rc <> ERROR_SUCCESS) Then GoTo GetKeyError ' 处理错误...
tmpVal = String$(1024, 0) ' 分配变量空间
KeyValSize = 1024 ' 标记变量尺寸
rc = RegQueryValueEx(hKey, SubKeyRef, 0, _
lKeyValType, tmpVal, KeyValSize) ' 获得/创建关键字的值
If (rc <> ERROR_SUCCESS) Then GoTo GetKeyError ' 错误处理
tmpVal = Left$(tmpVal, InStr(tmpVal, Chr(0)) - 1)
Select Case lKeyValType ' 搜索数据类型...
Case REG_SZ, REG_EXPAND_SZ ' 字符串注册表关键字数据类型
sKeyVal = tmpVal ' 复制字符串的值
Case REG_DWORD ' 四字节注册表关键字数据类型
For i = Len(tmpVal) To 1 Step -1 ' 转换每一位
sKeyVal = sKeyVal + Hex(Asc(Mid(tmpVal, i, 1))) ' 一个字符一个字符地生成值。
Next
sKeyVal = Format$("&h" + sKeyVal) ' 转换四字节为字符串
End Select
GetKeyValue = sKeyVal ' 返回值
rc = RegCloseKey(hKey) ' 关闭注册表关键字
Exit Function ' 退出
GetKeyError: ' 错误发生过后进行清除...
GetKeyValue = vbNullString ' 设置返回值为空字符串
rc = RegCloseKey(hKey) ' 关闭注册表关键字
End FunctionPublic Function DeleteKey(KeyRoot As Long, KeyName As String, SubKeyRef As String) As Boolean
Dim rc As Long ' 返回代码
Dim hKey As Long ' 处理打开的注册表关键字
Dim lpAttr As SECURITY_ATTRIBUTES ' 注册表安全类型
lpAttr.nLength = 50 ' 设置安全属性为缺省值...
lpAttr.lpSecurityDescriptor = 0 ' ...
lpAttr.bInheritHandle = True ' ...
rc = RegOpenKeyEx(KeyRoot, KeyName, 0, KEY_ALL_ACCESS, hKey) ' 打开注册表关键字
If (rc <> ERROR_SUCCESS) Then GoTo DeleteKeyError ' 处理错误...
rc = RegDeleteValue(hKey, SubKeyRef) ' 删除//KeyRoot//KeyName
If (rc <> ERROR_SUCCESS) Then GoTo DeleteKeyError ' 错误处理...
DeleteKey = True
rc = RegCloseKey(hKey) ' 关闭关键字
Exit Function
DeleteKeyError:
DeleteKey = False ' 设置错误返回代码
rc = RegCloseKey(hKey) ' 试图关闭关键字
End Function