登录  
 加关注
查看详情
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

miaozk2006

点滴积累与收藏:关于技术,关于生活

 
 
 

日志

 
 

几行VB代码拿下注册表  

2010-12-12 20:03:38|  分类: 编程-VB |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |

'****************************注册表操作函数**********************
'声明:以下代码由轻风工作室REDICE编写,引用时请作一说明。
'****************************************************************

'*****下面先声明一些常量******************************************
Public Const HKEY_CLASSES_ROOT = &H80000000
Public Const HKEY_CURRENT_CONFIG = &H80000005
Public Const HKEY_CURRENT_USER = &H80000001
Public Const HKEY_DYN_DATA = &H80000006
Public Const HKEY_LOCAL_MACHINE = &H80000002
Public Const HKEY_USERS = &H80000003
Public Const REG_OPTION_NON_VOLATILE = 0
Public Const KEY_ALL_ACCESS = (&H20000 Or &H1 Or &H2 Or &H4 Or &H8 Or &H10 Or &H20) And (Not &H100000)
Public Const REG_SZ = 1
Public Const REG_DWORD = 4
'*****************************************************************

'*****下面声明注册表操作中用到的API函数****************************
Public Declare Function RegCreateKey Lib "advapi32.dll" Alias "RegCreateKeyA" (ByVal hKey As Long, ByVal lpSubKey As String, phkResult As Long) As Long
Public Declare Function RegCloseKey Lib "advapi32.dll" (ByVal hKey As Long) As Long
Public 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
Public Declare Function RegDeleteKey Lib "advapi32.dll" Alias "RegDeleteKeyA" (ByVal hKey As Long, ByVal lpSubKey As String) As Long
Public 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
Public 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
Public 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
Public Declare Function RegDeleteValue Lib "advapi32.dll" Alias "RegDeleteValueA" (ByVal hKey As Long, ByVal lpValueName As String) As Long
'*****************************************************************

'*****下面是我自己写的一些注册表操作中常用的一些函数**************
'*****新键注册表项
Public Function createnewkey(ip As Long, snewkeyname As String)
     Dim hnewkey As Long
     Dim retval As Long
     retval = RegCreateKey(ip, snewkeyname, hnewkey)
     If retval = 0 Then
       RegCloseKey (hnewkey)   '关闭上面建立或打开的项
     End If
End Function
'实例:在HKEY_CURRENT_USER下建立项"xiaopeng"
'代码为 createnewkey HKEY_CURRENT_USER ,"xiaopeng"
'******************************************************************

'*******删除注册表项***********************************************
Public Function deletekey(ip As Long, skeyname As String)
     Dim hKey As Long
     Dim retval As Long
     retval = RegOpenKeyEx(ip, skeyname, 0, KEY_ALL_ACCESS, hKey)
     If retval = 0 Then
       RegDeleteKey ip, skeyname
     End If
End Function
'实例:删除上面建立的HKEY_CURRENT_USER下的项"xiaopeng"
'代码为 deletekey HKEY_CURRENT_USER ,"xiaopeng"
'******************************************************************

'********新建,设置数值名称*****************************************
Public Function setkeyvalue(ByVal ip As Long, ByVal keyname As String, ByVal valuename As String, ByVal valuesetting As Variant, ByVal valuetype As Long)
     Dim retval As Long
     Dim hKey As Long
     If RegOpenKeyEx(ip, keyname, 0, KEY_ALL_ACCESS, hKey) > 0 Then Exit Function
     Select Case valuetype
           Case REG_SZ
             RegSetValueExString hKey, valuename, 0&, REG_SZ, valuesetting, Len(valuesetting)
           Case REG_DWORD
             RegSetValueExLong hKey, valuename, 0, valuetype, valuesetting, 4
     End Select
     RegCloseKey (hKey)
End Function
'实例:在HKEY_CURRENT_USER下的项"xiaopeng"中建立名为"redice",键值为"is xiaopeng",类型为REG_SZ的新键
'代码为 setkeyvalue HKEY_CURRENT_USER ,"xiaopeng" ,"redice","is xiaopeng",REG_SZ
'又如:在HKEY_CURRENT_USER下的项"xiaopeng"中建立名为"ceshi",键值为2,类型为REG_DWORD的新键
'代码为"setkeyvalue HKEY_CURRENT_USER,"xiaopeng","ceshi",2,REG_DWORD
'******************************************************************

'*********删除数值名称*********************************************
Public Function deletevalue(ByVal ip As Long, ByVal keyname As String, ByVal valuename As String)
     Dim retval As Long
     Dim hKey As Long
     retval = RegOpenKeyEx(ip, keyname, 0, KEY_ALL_ACCESS, hKey)
     If retval > 0 Then
         Exit Function
     End If
     RegDeleteValue hKey, valuename
     RegCloseKey hKey
End Function
'实例:删除HKEY_CURRENT_USER下的项"xiaopeng"中名为"redice"的新键
'代码为 deletevalue HKEY_CURRENT_USER ,"xiaopeng","redice"
'******************************************************************
'**********查询已存在的数值内容************************************
Public Function getvalue(ByVal ip As Long, keyname As String, valuename As String) As String
     Dim retval As Long
     Dim hKey As Long
     Dim valuesetting As Variant
     Dim cddata As Long
     Dim lvalue As Long
     Dim svalue As String
     Dim lvaluetye As Long
     retval = RegOpenKeyEx(ip, keyname, 0, KEY_ALL_ACCESS, hKey)
     If retval > 0 Then
       getvalue = ""
       Exit Function
     End If
     retval = RegQueryValueEx(hKey, valuename, 0, lvaluetype, ByVal VBNullString, cddata)
     If retval <> 0 Then
       RegCloseKey hKey
       Exit Function
     End If
     Select Case lvaluetype
           Case REG_SZ
                 svalue = String(cddata, Chr(0))
                 RegQueryValueEx hKey, valuename, 0, lvaluetype, ByVal svalue, cddata
                 valuesetting = Left$(svalue, cddata)
                 getvalue = CStr(valuesetting)
           Case REG_DWORD
                 RegQueryValueEx hKey, valuename, 0, lvaluetype, lvalue, cddata
                 valuesetting = lvalue
                 getvalue = CStr(valuesetting)
     End Select
End Function
'实例:获取HKEY_CURRENT_USER下的项"xiaopeng"中名为"redice"的新键的键值
'代码为 getvalue HKEY_CURRENT_USER ,"xiaopeng","redice"
'*********************************************************************
 

摘自:网络整理


相关参考


VB查找替代字符串的函数

VB换行气泡提示类

VB/VBA通用路径选择对话框

ASCII码表0-255完整版 附详细注释

VBKeyAscii

VB取得TextBoxRichTextBox光标所在的行和列(支持汉字)

VB取得TextBoxRichTextBox光标所在的行和列(支持汉字)

VB如何实现Undo(撤消)功能

VB计算文本文件的行数

VB获取快捷方式原文件路径

微软 Small Basic 简体中文版 已经发布了

VB操作Excel 非常详细 [网摘]

VB如何判断文件正被占用/已被打开

VB添加listbox 的水平卷动轴

VB打开资源管理器并指定文件

VB根据窗体自动调整窗体内控件大小 注:实用,可以直接引用

VB中的指针技术

Visual Basic编程常见问题及解答(1

Visual Basic编程常见问题及解答(2

Visual Basic编程常见问题及解答(3

VisualBasic变量、常数和数据类型及过程概述

VB6的后期绑定和前期绑定

VB用户控件制作讲解与实例

VB制作OCX控件的步骤

VBFSO的调用的两种方法

VB操作EXCEL

VB判断文件及目录的存在性

VB网站(最新、经典源代码、技术文章、基础知识)

VB得到指定文件夹下的文件列表

VB产生随机任意大小文件挤满硬盘

VB文件的读写操作

VB创建超链接 打开指定网站的几种方法

VB 源码 删除重复行程序 函数

VB 计算自己程序段所用时间

VB 获取路径名各部分 (获取文件路径,获取文件名,获取文件扩展名)自编

几行VB代码拿下注册表

VB 在浏览器中打开指定网址

VB窗口置顶

vb ListBox 之中点击右键弹出菜单

  评论这张
 
阅读(800)| 评论(0)

历史上的今天

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2018