如何在VB中用windows API函数读注册表的某个键值?

freedomgogo1 2007-08-14 10:08:02
请教各位,

如何在VB中用windows API函数读注册表的这个键值?
缓存路径:
HKEY_CURRENT_USER\Software\Microsoft\Office\12.0\MS Project\Setting
读该路径下的CacheLocation的键值

谢谢!
...全文
538 点赞 收藏 13
写回复
13 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
freedomgogo1 2007-08-16
已经搞定了,多谢各位指点
回复
freedomgogo1 2007-08-16
楼上的大侠好像未提到对路径中包含 “.”的处理,我这里就是不行,为什么?
回复
lin_style 2007-08-15
//HKEY hKey;
//RegOpenKeyEx(HKEY_LOCAL_MACHINE, "Software\\...", 0, KEY_READ, &hKey) ;
//CString s;
//DWORD dwValue;
//DWORD dwValuelen;
//DWORD dwType;
//RegQueryValueEx(hKey, "YourKeyName" , 0, &dwType, (LPBYTE)&dwValue, &dwValuelen);
//s.Format( "%d",dwValue );
//MessageBox(s);
//RegCloseKey(hKey);


VC下的。不过调用API都差不多
回复
fante2007 2007-08-14
Declare Function RegCloseKey Lib "advapi32.dll" (ByVal hKey As Long) As Long
Declare Function RegCreateKeyEx Lib "advapi32.dll" 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, ByVal lpSecurityAttributes As Long, phkResult As Long, lpdwDisposition As Long) As Long
Declare Function RegOpenKeyEx Lib "advapi32.dll" Alias "RegOpenKeyExA" (ByVal hKey As Long, ByVal lpSubKey As String, ByVal ulOptions As Long, ByVal samDesired As Long, phkResult As Long) As Long
Declare Function RegQueryValueExString Lib "advapi32.dll" Alias "RegQueryValueExA" (ByVal hKey As Long, ByVal lpValueName As String, ByVal lpReserved As Long, lpType As Long, ByVal lpData As String, lpcbData As Long) As Long
Declare Function RegQueryValueExLong Lib "advapi32.dll" Alias "RegQueryValueExA" (ByVal hKey As Long, ByVal lpValueName As String, ByVal lpReserved As Long, lpType As Long, lpData As Long, lpcbData As Long) As Long
Declare Function RegQueryValueExNULL Lib "advapi32.dll" Alias "RegQueryValueExA" (ByVal hKey As Long, ByVal lpValueName As String, ByVal lpReserved As Long, lpType As Long, ByVal lpData As Long, lpcbData As Long) As Long
Declare Function RegSetValueExString Lib "advapi32.dll" Alias "RegSetValueExA" (ByVal hKey As Long, ByVal lpValueName As String, ByVal Reserved As Long, ByVal dwType As Long, ByVal lpValue As String, ByVal cbData As Long) As Long
Declare Function RegSetValueExLong Lib "advapi32.dll" Alias "RegSetValueExA" (ByVal hKey As Long, ByVal lpValueName As String, ByVal Reserved As Long, ByVal dwType As Long, lpValue As Long, ByVal cbData As Long) As Long
Private Declare Function RegDeleteKey& Lib "advapi32.dll" Alias "RegDeleteKeyA" (ByVal hKey As Long, ByVal lpSubKey As String)
Private Declare Function RegDeleteValue& Lib "advapi32.dll" Alias "RegDeleteValueA" (ByVal hKey As Long, ByVal lpValueName As String)
-----------------做参考
回复
fante2007 2007-08-14
Declare Function RegCloseKey Lib "advapi32.dll" (ByVal hKey As Long) As Long
Declare Function RegCreateKeyEx Lib "advapi32.dll" 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, ByVal lpSecurityAttributes As Long, phkResult As Long, lpdwDisposition As Long) As Long
Declare Function RegOpenKeyEx Lib "advapi32.dll" Alias "RegOpenKeyExA" (ByVal hKey As Long, ByVal lpSubKey As String, ByVal ulOptions As Long, ByVal samDesired As Long, phkResult As Long) As Long
Declare Function RegQueryValueExString Lib "advapi32.dll" Alias "RegQueryValueExA" (ByVal hKey As Long, ByVal lpValueName As String, ByVal lpReserved As Long, lpType As Long, ByVal lpData As String, lpcbData As Long) As Long
Declare Function RegQueryValueExLong Lib "advapi32.dll" Alias "RegQueryValueExA" (ByVal hKey As Long, ByVal lpValueName As String, ByVal lpReserved As Long, lpType As Long, lpData As Long, lpcbData As Long) As Long
Declare Function RegQueryValueExNULL Lib "advapi32.dll" Alias "RegQueryValueExA" (ByVal hKey As Long, ByVal lpValueName As String, ByVal lpReserved As Long, lpType As Long, ByVal lpData As Long, lpcbData As Long) As Long
Declare Function RegSetValueExString Lib "advapi32.dll" Alias "RegSetValueExA" (ByVal hKey As Long, ByVal lpValueName As String, ByVal Reserved As Long, ByVal dwType As Long, ByVal lpValue As String, ByVal cbData As Long) As Long
Declare Function RegSetValueExLong Lib "advapi32.dll" Alias "RegSetValueExA" (ByVal hKey As Long, ByVal lpValueName As String, ByVal Reserved As Long, ByVal dwType As Long, lpValue As Long, ByVal cbData As Long) As Long
Private Declare Function RegDeleteKey& Lib "advapi32.dll" Alias "RegDeleteKeyA" (ByVal hKey As Long, ByVal lpSubKey As String)
Private Declare Function RegDeleteValue& Lib "advapi32.dll" Alias "RegDeleteValueA" (ByVal hKey As Long, ByVal lpValueName As String)
-----------------做参考
回复
tjficcbw 2007-08-14
一下二下的说不清楚,你下载一个编辑注册表的例程,自己在修改就行了,
回复
freedomgogo1 2007-08-14
每人帮我啊,自己顶一下。
回复
freedomgogo1 2007-08-14
在线等待各位高手的最后指点,谢谢
回复
freedomgogo1 2007-08-14
谢谢各位,还是有点小问题,如果是正常路径(不包含.号),则可以
但是我现在路径中“12.0”中有"."的符号,好像不行。
再次麻烦各位了,
回复
vbman2003 2007-08-14
有点问题:
Call RegOpenKey(HKEY_CURRENT_USER, "\Software\Microsoft\Office\12.0\MS Project\Setting", nKeyHandle)
改为:
Call RegOpenKey(HKEY_CURRENT_USER, "Software\Microsoft\Office\12.0\MS Project\Setting", nKeyHandle)
回复
vbman2003 2007-08-14
这样的问题论坛上许多,搜索一下就有了,如下



'根键常数
Const HKEY_CLASSES_ROOT = -2147483648#
Const HKEY_CURRENT_USER = -2147483647#
Const HKEY_LOCAL_MACHINE = -2147483646#
Const HKEY_USERS = -2147483645#

'键值类型
Const REG_SZ = 1& '字符串值
Const REG_BINARY = 3& '二进制值
Const REG_DWORD = 4& 'DWORD值

'打开一个主键
Private Declare Function RegOpenKey Lib "advapi32.dll" Alias "RegOpenKeyA" _
( _
ByVal hKey As Long, _
ByVal lpSubKey As String, _
ByRef phkResult As Long _
) As Long

'查询一个键值,lpData应由缺省的ByRef型改为ByVal型
Private Declare Function RegQueryValueEx Lib "advapi32.dll" Alias "RegQueryValueExA" _
( _
ByVal hKey As Long, _
ByVal lpValueName As String, _
ByVal lpReserved As Long, _
ByRef lpType As Long, _
ByVal lpData As Any, _
ByRef lpcbData As Long _
) As Long

'关闭一个主键
Private Declare Function RegCloseKey Lib "advapi32.dll" _
( _
ByVal hKey As Long _
) As Long

Private Sub Command1_Click()

Dim nKeyHandle As Long, nValueType As Long, nLength As Long
Dim sValue As String

Call RegOpenKey(HKEY_CURRENT_USER, "\Software\Microsoft\Office\12.0\MS Project\Setting", nKeyHandle)
sValue = Space(255)
nLength = 255
Call RegQueryValueEx(nKeyHandle, "CacheLocation", 0, nValueType, sValue, nLength)
MsgBox sValue
Call RegCloseKey(nKeyHandle)

End Sub
回复
fante2007 2007-08-14
【VB声明】
Private Declare Function RegQueryValue Lib "advapi32.dll" Alias "RegQueryValueA" (ByVal hKey As Long, ByVal lpSubKey As String, ByVal lpValue As String, lpcbValue As Long) As Long

【别名】
RegQueryValueA

【说明】
取得指定项或子项的默认(未命名)值

【返回值】
Long,零(ERROR_SUCCESS)表示成功。其他任何值都代表一个错误代码

【备注】
win32应用程序应该使用RegQueryValueEx。lpValue被定义成一个字串,以维持同win16的兼容性(在win16中,值全都是字串)

【参数表】
hKey ----------- Long,一个已打开项的句柄,或者指定一个标准项名

lpSubKey ------- String,要获取一个值的子项。可设为vbNullString,表示获取hKey的值

lpValue -------- String,用于容纳指定项值的一个字串

lpcbValue ------ Long,指定一个变量,用于装载lpValue缓冲区的长度。一旦返回,它会设为实际载入缓冲区的字节数量

回复
freedomgogo1 2007-08-14
能否针对我的问题给出代码,如果哪位有空,麻烦了。我看了些注册表的网上的资料,因为是入门者,看的很晕呢hehe
回复
相关推荐
发帖
VB基础类
创建于2007-09-28

7518

社区成员

VB 基础类
申请成为版主
帖子事件
创建了帖子
2007-08-14 10:08
社区公告
暂无公告