帮我看看这段代码还可以精简些吗^-^

MonkeyLin 2002-02-18 01:53:24
Option Explicit

Public Const REG_SZ = 1 ' Unicode nul terminated string
Public Const REG_BINARY = 3 ' Free form binary
Public Const HKEY_CURRENT_USER = &H80000001
Public Const HKEY_LOCAL_MACHINE = &H80000002
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 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 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 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
Dim hkey As Long
Dim lResult As Long, lValueType As Long, strBuf As String, lDataBufSize As Long

Function GetStringValue(ByVal MainKeyHandle As Long, ByVal Subkey As String, strValueName As String)
'Get string value from reg
lResult = RegOpenKey(MainKeyHandle, Subkey, hkey) 'open the key
If lResult <> 0 Then GetStringValue = "?": RegCloseKey hkey: Exit Function 'if the key not exists
'Get information about the key
lResult = RegQueryValueEx(hkey, strValueName, 0, lValueType, ByVal 0, lDataBufSize)
If lResult = 0 And lValueType = REG_SZ Then
strBuf = String(lDataBufSize, Chr(0)) 'Create a buffer
'retrieve the key's content
lResult = RegQueryValueEx(hkey, strValueName, 0, 0, ByVal strBuf, lDataBufSize)
If lResult = 0 Then
'Remove the unnecessary chr(0)'s
GetStringValue = Left(strBuf, InStr(1, strBuf, Chr(0)) - 1)
Else
GetStringValue = "?"
End If
Else
GetStringValue = "?"
End If
RegCloseKey hkey
End Function
这里用了两次RegQueryValueEx,若第一次返回成功,第二次是否一定成功
若一定成功,
If lResult = 0 Then
'Remove the unnecessary chr(0)'s
GetStringValue = Left(strBuf, InStr(1, strBuf, Chr(0)) - 1)
Else
GetStringValue = "?"
End If
这段就变成一行了:GetStringValue = Left(strBuf, InStr(1, strBuf, Chr(0)) - 1)
...全文
31 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
progame 2002-02-18
  • 打赏
  • 举报
回复
小猴子,你可以自己再用函数封装一下注册表读写

这样就不会这么乱了
MonkeyLin 2002-02-18
  • 打赏
  • 举报
回复
那个没有办法,原来是有缩进的,但一粘贴……就没了
我没把" "换成" "
shawls 2002-02-18
  • 打赏
  • 举报
回复


可以的,但是对于我来说,可读性也是一个重要的方面

Chice_wxg 2002-02-18
  • 打赏
  • 举报
回复
还可以吧,就是太乱了,呵呵

7,765

社区成员

发帖
与我相关
我的任务
社区描述
VB 基础类
社区管理员
  • VB基础类社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧