vb用api读开机启动项问题

那个api里的RegEnumValue我一直不太会用谁能用api写个vb读开机启动项的例子啊,要求读在一个list里,高分送上!谢谢这个读的是自动运行程序的值名,可否改成... 那个api里的RegEnumValue我一直不太会用
谁能用api写个vb读开机启动项的例子啊,要求读在一个list里,高分送上!
谢谢
这个读的是自动运行程序的值名,可否改成读值呢?就是值后面的那些程序的自启动文件路径.
展开
 我来答
newkedison
2008-07-06 · TA获得超过734个赞
知道小有建树答主
回答量:234
采纳率:0%
帮助的人:416万
展开全部
程序如下,窗体上放一个ListBox,和一个按钮

Const HKEY_LOCAL_MACHINE = &H80000002
Private Declare Function RegCloseKey Lib "advapi32.dll" (ByVal hKey As Long) 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 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

Private Sub Command1_Click()
Dim hKey As Long, Cnt As Long, sSave As String
'打开指定的注册表项,这个位置保存的就是启动项
RegOpenKey HKEY_LOCAL_MACHINE, "SOFTWARE\Microsoft\Windows\CurrentVersion\Run", hKey
Cnt = 0
List1.Clear
Do
'创建一个缓冲区,用以保存每个开机启动项
sSave = String$(255, 0)
'枚举第Cnt个启动项,如果返回值不为0,说明已经枚举完成
If RegEnumValue(hKey, Cnt, sSave, 255, 0, ByVal 0&, ByVal 0&, ByVal 0&) <> 0 Then Exit Do
'添加到list1, StripTerminator函数的作用是去除多余的字符
List1.AddItem StripTerminator(sSave)
Cnt = Cnt + 1
Loop
'关闭注册表项
RegCloseKey hKey
End Sub

'去除缓冲区中多余的Chr(0)
Private Function StripTerminator(sInput As String) As String
Dim ZeroPos As Integer
'找到第一个Chr(0)
ZeroPos = InStr(1, sInput, vbNullChar)
If ZeroPos > 0 Then '如果存在,则去掉后面所有的内容
StripTerminator = Left$(sInput, ZeroPos - 1)
Else '如果不存在,不做任何操作
StripTerminator = sInput
End If
End Function

修改如下:
Const HKEY_LOCAL_MACHINE = &H80000002
Const REG_SZ = 1
Const REG_BINARY = 3
Private Declare Function RegCloseKey Lib "advapi32.dll" (ByVal hKey As Long) 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 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
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 Sub Command1_Click()
Dim hKey As Long, Cnt As Long, sSave As String
'打开指定的注册表项,这个位置保存的就是启动项
RegOpenKey HKEY_LOCAL_MACHINE, "SOFTWARE\Microsoft\Windows\CurrentVersion\Run", hKey
Cnt = 0
List1.Clear
Do
'创建一个缓冲区,用以保存每个开机启动项
sSave = String$(255, 0)
'枚举第Cnt个启动项,如果返回值不为0,说明已经枚举完成
If RegEnumValue(hKey, Cnt, sSave, 255, 0, ByVal 0&, ByVal 0&, ByVal 0&) <> 0 Then Exit Do
'添加到list1, StripTerminator函数的作用是去除多余的字符
List1.AddItem RegQueryStringValue(hKey, StripTerminator(sSave))
Cnt = Cnt + 1
Loop
'关闭注册表项
RegCloseKey hKey
End Sub

'去除缓冲区中多余的Chr(0)
Private Function StripTerminator(sInput As String) As String
Dim ZeroPos As Integer
'找到第一个Chr(0)
ZeroPos = InStr(1, sInput, vbNullChar)
If ZeroPos > 0 Then '如果存在,则去掉后面所有的内容
StripTerminator = Left$(sInput, ZeroPos - 1)
Else '如果不存在,不做任何操作
StripTerminator = sInput
End If
End Function

'读取注册表某项的值
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
'获取值的类型
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
'去掉多余的Chr(0)
RegQueryStringValue = Left$(strBuf, InStr(1, strBuf, Chr$(0)) - 1)
End If
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 If
End Function
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式