CSDN首页 空间 新闻 论坛 Blog 下载 读书 网摘 搜索 .NET Java 视频 接项目 求职 在线学习 买书 程序员 通知
山寨机中的战斗机! 程序优化工程师到底对IT界有没有贡献
CSDN社区
搜索 收藏 打印 关闭
CSDN社区 >  VB >  基础类

关于修改注册表键值的问题????

楼主lndlwwh830(笑天星)2003-11-02 10:32:40 在 VB / 基础类 提问

如和修改注册表的键制和,写入,读出,删除请给出事例,(我知道使用api)!!!  
  问题点数:0、回复次数:6Top

1 楼mingtian2008(明天)回复于 2003-11-02 10:42:52 得分 0

upTop

2 楼palmkey(原水)回复于 2003-11-02 11:29:31 得分 0

'一个注册表操作的模块,Windows   2000测试  
   
  Option   Explicit  
   
  Global   Const   REG_SZ   As   Long   =   1  
  Global   Const   REG_DWORD   As   Long   =   4  
   
  Global   Const   HKEY_CLASSES_ROOT   =   &H80000000  
  Global   Const   HKEY_CURRENT_USER   =   &H80000001  
  Global   Const   HKEY_LOCAL_MACHINE   =   &H80000002  
  Global   Const   HKEY_USERS   =   &H80000003  
   
  Global   Const   ERROR_NONE   =   0  
  Global   Const   ERROR_BADDB   =   1  
  Global   Const   ERROR_BADKEY   =   2  
  Global   Const   ERROR_CANTOPEN   =   3  
  Global   Const   ERROR_CANTREAD   =   4  
  Global   Const   ERROR_CANTWRITE   =   5  
  Global   Const   ERROR_OUTOFMEMORY   =   6  
  Global   Const   ERROR_INVALID_PARAMETER   =   7  
  Global   Const   ERROR_ACCESS_DENIED   =   8  
  Global   Const   ERROR_INVALID_PARAMETERS   =   87  
  Global   Const   ERROR_NO_MORE_ITEMS   =   259  
   
  Global   Const   KEY_ALL_ACCESS   =   &H3F  
   
  Global   Const   REG_OPTION_NON_VOLATILE   =   0  
   
  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)  
   
  Public   Function   DeleteKey(lPredefinedKey   As   Long,   sKeyName   As   String)  
   
          Dim   lRetVal   As   Long                   'result   of   the   SetValueEx   function  
          Dim   hKey   As   Long                   'handle   of   open   key  
           
          'open   the   specified   key  
           
          'lRetVal   =   RegOpenKeyEx(lPredefinedKey,   sKeyName,   0,   KEY_ALL_ACCESS,   hKey)  
          lRetVal   =   RegDeleteKey(lPredefinedKey,   sKeyName)  
          'RegCloseKey   (hKey)  
  End   Function  
   
  Public   Function   DeleteValue(lPredefinedKey   As   Long,   sKeyName   As   String,   sValueName   As   String)  
   
                Dim   lRetVal   As   Long                   'result   of   the   SetValueEx   function  
                Dim   hKey   As   Long                   'handle   of   open   key  
   
                'open   the   specified   key  
   
                lRetVal   =   RegOpenKeyEx(lPredefinedKey,   sKeyName,   0,   KEY_ALL_ACCESS,   hKey)  
                lRetVal   =   RegDeleteValue(hKey,   sValueName)  
                RegCloseKey   (hKey)  
  End   Function  
   
  Public   Function   SetValueEx(ByVal   hKey   As   Long,   sValueName   As   String,   lType   As   Long,   vValue   As   Variant)   As   Long  
          Dim   lValue   As   Long  
          Dim   sValue   As   String  
   
          Select   Case   lType  
                  Case   REG_SZ  
                          sValue   =   vValue  
                          SetValueEx   =   RegSetValueExString(hKey,   sValueName,   0&,   lType,   sValue,   Len(sValue))  
                  Case   REG_DWORD  
                          lValue   =   vValue  
                          SetValueEx   =   RegSetValueExLong(hKey,   sValueName,   0&,   lType,   lValue,   4)  
                  End   Select  
   
  End   Function  
   
   
   
   
   
  Function   QueryValueEx(ByVal   lhKey   As   Long,   ByVal   szValueName   As   String,   vValue   As   Variant)   As   Long  
          Dim   cch   As   Long  
          Dim   lrc   As   Long  
          Dim   lType   As   Long  
          Dim   lValue   As   Long  
          Dim   sValue   As   String  
   
          On   Error   GoTo   QueryValueExError  
   
   
   
          '   Determine   the   size   and   type   of   data   to   be   read  
   
          lrc   =   RegQueryValueExNULL(lhKey,   szValueName,   0&,   lType,   0&,   cch)  
          If   lrc   <>   ERROR_NONE   Then   Error   5  
   
          Select   Case   lType  
                  '   For   strings  
                  Case   REG_SZ:  
                          sValue   =   String(cch,   0)  
                          lrc   =   RegQueryValueExString(lhKey,   szValueName,   0&,   lType,   sValue,   cch)  
                          If   lrc   =   ERROR_NONE   Then  
                                  vValue   =   Left$(sValue,   cch)  
                          Else  
                                  vValue   =   Empty  
                          End   If  
   
                  '   For   DWORDS  
                  Case   REG_DWORD:  
                          lrc   =   RegQueryValueExLong(lhKey,   szValueName,   0&,   lType,   lValue,   cch)  
                          If   lrc   =   ERROR_NONE   Then   vValue   =   lValue  
                  Case   Else  
                          'all   other   data   types   not   supported  
                          lrc   =   -1  
          End   Select  
   
  QueryValueExExit:  
   
          QueryValueEx   =   lrc  
          Exit   Function  
   
  QueryValueExError:  
   
          Resume   QueryValueExExit  
   
  End   Function  
  Public   Function   CreateNewKey(lPredefinedKey   As   Long,   sNewKeyName   As   String)  
           
          Dim   hNewKey   As   Long                   'handle   to   the   new   key  
          Dim   lRetVal   As   Long                   'result   of   the   RegCreateKeyEx   function  
           
          lRetVal   =   RegCreateKeyEx(lPredefinedKey,   sNewKeyName,   0&,   vbNullString,   REG_OPTION_NON_VOLATILE,   KEY_ALL_ACCESS,   0&,   hNewKey,   lRetVal)  
          RegCloseKey   (hNewKey)  
  End   Function  
   
   
  Sub   Main_MOD()  
          'Examples   of   each   function:  
          '创建新键  
          CreateNewKey   HKEY_CURRENT_USER,   "TestKey\SubKey1\SubKey2"  
          '设置键值  
          SetKeyValue   HKEY_CURRENT_USER,   "TestKey\SubKey1",   "Test",   "Testing,   Testing",   REG_SZ  
          MsgBox   QueryValue(HKEY_CURRENT_USER,   "TestKey\SubKey1",   "Test")  
          '删除键  
          DeleteKey   HKEY_CURRENT_USER,   "TestKey\SubKey1\SubKey2"  
          '删除键值  
          DeleteValue   HKEY_CURRENT_USER,   "TestKey\SubKey1",   "Test"  
  End   Sub  
   
   
  Public   Function   SetKeyValue(lPredefinedKey   As   Long,   sKeyName   As   String,   sValueName   As   String,   vValueSetting   As   Variant,   lValueType   As   Long)  
   
                Dim   lRetVal   As   Long                   'result   of   the   SetValueEx   function  
                Dim   hKey   As   Long                   'handle   of   open   key  
   
                'open   the   specified   key  
   
                lRetVal   =   RegOpenKeyEx(lPredefinedKey,   sKeyName,   0,   KEY_ALL_ACCESS,   hKey)  
                lRetVal   =   SetValueEx(hKey,   sValueName,   lValueType,   vValueSetting)  
                RegCloseKey   (hKey)  
   
  End   Function  
   
  Public   Function   QueryValue(lPredefinedKey   As   Long,   sKeyName   As   String,   sValueName   As   String)  
   
                Dim   lRetVal   As   Long                   'result   of   the   API   functions  
                Dim   hKey   As   Long                   'handle   of   opened   key  
                Dim   vValue   As   Variant             'setting   of   queried   value  
   
   
                lRetVal   =   RegOpenKeyEx(lPredefinedKey,   sKeyName,   0,   KEY_ALL_ACCESS,   hKey)  
                lRetVal   =   QueryValueEx(hKey,   sValueName,   vValue)  
                'MsgBox   vValue  
                QueryValue   =   vValue  
                RegCloseKey   (hKey)  
  End   Function  
   
   
   
   
  Top

3 楼lndlwwh830(笑天星)回复于 2003-11-02 12:12:56 得分 0

能不能解释一下,最好有个事例!!Top

4 楼sysmaster(为什么我还不懂)回复于 2003-11-09 13:15:36 得分 0

你要给我EMAIL ,我发一个为我女朋友写的例子!  
  master_sys@msn.comTop

5 楼trite(追风少年)回复于 2003-11-09 13:47:27 得分 0

我也想要这个例子  
  email:trite2000@sina.comTop

6 楼objectprogram(星魂)回复于 2003-11-09 14:17:11 得分 0

yaolintm@163.comTop

相关问题

  • 如何即时刷新刚修改过的注册表键值
  • 直接写注册表来注册DCOM Server,要修改那些键、值? 急、急
  • 如何修改REG_DWORD的注册表键值。。。我的方法老是不对
  • 实现隐藏c 盘d盘~ 是修改了注册表里哪的键值?
  • 请教图标的问题:修改了注册表的 DefaultIcon 键值...
  • 求一注册表键值
  • 在注册表中修改REG_DWORD类型键值,修改后不可用。这是为什么?
  • 在注册表中修改REG_MULTI_SZ类型键值,修改后出现乱码。这是为什么?
  • 如何向远程计算机的注册表中添加、删除或修改键值?
  • 如何修改REG_DWORD的注册表键值。。。我的方法老是不对,希望帮一下忙。

关键词

  • 注册表
  • global const
  • hkey
  • error
  • reg
  • as long

得分解答快速导航

  • 帖主:lndlwwh830

相关链接

  • Visual Basic类图书
  • Visual Basic类源码下载

广告也精彩

反馈

请通过下述方式给我们反馈
反馈
提问
网站简介|广告服务|VIP资费标准|银行汇款帐号|网站地图|帮助|联系方式|诚聘英才|English|问题报告
北京创新乐知广告有限公司 版权所有, 京 ICP 证 070598 号
世纪乐知(北京)网络技术有限公司 提供技术支持
Copyright © 2000-2008, CSDN.NET, All Rights Reserved
GongshangLogo