注册表操作(VC_Win32)

来源:互联网 发布:软件怎么加注册码 编辑:程序博客网 时间:2024/06/11 04:38

注册表操作(VC_Win32)

数据类型

注册表的数据类型主要有以下四种:
显示类型(在编辑器中)   数据类型    说明                   
REG_SZ           字符串     文本字符串
REG_MULTI_SZ         多字符串    含有多个文本值的字符串
REG_BINARY          二进制数    二进制值,以十六进制显示.
REG_DWORD           双字      一个32位的二进制值,显示为8位的十六进制值.

各主键的简单介绍

  • HKEY_LOCAL_MACHINE  是一个显示控制系统和软件的处理键.HKLM键保存着计算机的系统信息.它包括网络和硬件上所有的软件设置.
  • HKEY_CLASSES_ROOT  是系统中控制所有数据文件的项.
  • HKEY_USERS  将缺省用户和目前登陆用户的信息输入到注册表编辑器
  • HKEY_CURRENT_USER  包含着在HKEY_USERS安全辨别里列出的同样信息
  • HKEY_CURRENT_CONFIG  包括了系统中现有的所有配置文件的细节.HKEY_CURRENT_CONFIG允许软件和设备驱动程序员很方便的更新注册表,而不涉及到多个配置文件信息. HKEY_LOCAL_MACHINE中同样的数据和任何注册表的变化都会同时的变化.

 

相关函数

创建键 RegCreateKeyEx(次函数主要用于生成键(目录))

函数原型

LONG RegCreateKeyEx(

  HKEYhKey,                                 // handle to open key

  LPCTSTRlpSubKey,                           // subkey name

  DWORDReserved,                             // reserved

  LPTSTRlpClass,                             // class string

  DWORD dwOptions,                            // special options

  REGSAMsamDesired,                          // desired security access

 LPSECURITY_ATTRIBUTES lpSecurityAttributes, //inheritance

  PHKEYphkResult,                            // key handle

  LPDWORD lpdwDisposition                     //disposition value buffer

);

参数说明

  • hKey:   要打开键的句柄或以下预定义句柄
    • HKEY_CLASSES_ROOT
    • HKEY_CURRENT_USER
    • HKEY_LOCAL_MACHINE
    • HKEY_USERS
  • lpSubKey:   指向一个用于定义子键路径的字符串
  • Reserved,dwOptions,samDesired:   置0
  • lpClass,lpSecurityAttributes:   置NULL
  • phkResult:   用于接收键句柄
  • lpdwDisposition:   接收的相关信息,取值如下
    • REG_CREATED_NEW_KEY   创建成功
    • REG_OPENED_EXISTING_KEY    键已存在

返回值:If the function succeeds, the return value is ERROR_SUCCESS.

打开键 RegOpenKeyEx

函数原型

LONG RegOpenKeyEx(

  HKEYhKey,         // handle to open key

  LPCTSTRlpSubKey,  //subkey name

  DWORDulOptions,   //reserved

  REGSAMsamDesired, // security access mask

  PHKEYphkResult    //handle to open key

);

参数说明

  • hKey:     要打开键的句柄或以下预定义句柄
    • HKEY_CLASSES_ROOT
    • HKEY_CURRENT_USER
    • HKEY_LOCAL_MACHINE
    • HKEY_USERS
  • lpSubKey:   指向一个用于定义子键路径的字符串
  • ulOptions:   保留位,置0
  • samDesired:   打开键后键的操作权限
  • phResult:   接收打开的键的句柄

返回值:If the function succeeds, the return value is ERROR_SUCCESS

删除键 RegDeleteKey

函数原型

LONG RegDeleteKey(

  HKEYhKey,         // handle to open key

  LPCTSTRlpSubKey   //subkey name

);

参数说明

  • hKey:   要打开键的句柄或以下预定义句柄
    • HKEY_CLASSES_ROOT
    • HKEY_CURRENT_USER
    • HKEY_LOCAL_MACHINE
    • HKEY_USERS
  • lpSubKey:   指向一个用于定义子键路径的字符串

返回值:If the function succeeds, the return value is ERROR_SUCCESS

修改/添加键值 RegSetValueEx

函数原型

LONG RegSetValueEx(

  HKEYhKey,           // handle to key

  LPCTSTRlpValueName, // value name

  DWORDReserved,      //reserved

  DWORDdwType,        //value type

  CONST BYTE*lpData,  //value data

  DWORD cbData         // size ofvalue data

);

参数说明

  • hKey:   打开键的句柄或以下预定义句柄
    • HKEY_CLASSES_ROOT
    • HKEY_CURRENT_USER
    • HKEY_LOCAL_MACHINE
    • HKEY_USERS
  • lpValueName:   键值的名称
  • Reserved:   保留位,置0
  • dwType:   键值的类型
  • lpData:   键值
  • cbData:   键值数据长度

返回值:If the function succeeds, thereturn value is ERROR_SUCCESS

删除键值 RegDeleteValue

函数原型

LONG RegDeleteValue(

  HKEYhKey,            // handle to key

  LPCTSTRlpValueName   //value name

);

参数说明

  • hKey:   打开键的句柄或以下预定义句柄
    • HKEY_CLASSES_ROOT
    • HKEY_CURRENT_USER
    • HKEY_LOCAL_MACHINE
    • HKEY_USERS
  • lpValueName:   键值的名称

返回值:If the function succeeds, the return value is ERROR_SUCCESS

读取键值 RegQueryValueEx

函数原型

LONG RegQueryValueEx(

  HKEYhKey,            // handle to key

  LPCTSTRlpValueName,  //value name

  LPDWORDlpReserved,   //reserved

  LPDWORD lpType,       // type buffer

  LPBYTElpData,        //data buffer

  LPDWORDlpcbData      //size of data buffer

);

参数说明

  • hKey:   打开键的句柄或以下预定义句柄
    • HKEY_CLASSES_ROOT
    • HKEY_CURRENT_USER
    • HKEY_LOCAL_MACHINE
    • HKEY_USERS
  • lpValueName:   键值的名称
  • Reserved:   保留位,置0
  • lpType:   接收键值的类型
  • lpData:   接收键值
  • lpcbData:   接收键值数据长度

#include "stdafx.h"

#include <windows.h>

#include <iostream>

using namespacestd;

 

int main()

{

     HKEY hKey;    

     LPCTSTR lpRun =L"Software\\_MyTest";

     DWORD state,dwtype,sizeBuff;

     long lRet;

     char reBuff[10] ={0};

 

     //lRet =RegCreateKeyEx(HKEY_CURRENT_USER,lpRun,0,NULL,0,0,NULL,&hKey,&state);

     //if(lRet == ERROR_SUCCESS)

     //{

     //    if(state ==REG_CREATED_NEW_KEY)

     //       cout<<"表项创建成功"<<endl;

 

     //

     //    //关闭键

     //   RegCloseKey(hKey);

     //}

     //else if (state == REG_OPENED_EXISTING_KEY)

     //{

     //   cout<<"表项已存在"<<endl;

     //}

 

     //lRet = RegDeleteKey(HKEY_CURRENT_USER,lpRun);

     //if (ERROR_SUCCESS == lRet)

     //{

     //   cout<<"删除键成功"<<endl;

     //}

     //else

     //   cout<<"删除键失败"<<endl;

 

     lRet = RegOpenKeyEx(HKEY_CURRENT_USER,lpRun,0,KEY_ALL_ACCESS,&hKey);

     if (ERROR_SUCCESS==lRet)

     {

         cout<<"打开键成功"<<endl;

     }

     else

         cout<<"打开键失败"<<endl;

 

     LPCTSTR KeyName= L"KeyName";

     char KeyValue[20];

     DWORD type;

     DWORD len = sizeof(KeyValue);

     lRet = RegQueryValueEx(hKey,KeyName,0,&type,(BYTE*)KeyValue,&len);

     if (ERROR_SUCCESS==lRet)

     {

         cout<<"查询键值成功------"<<KeyValue<<endl;

     }

     else

         cout<<"查询键值失败"<<endl;

 

 //  LPCTSTR KeyName =L"KeyName";

     //char KeyValue[] = "KeyValue";

     //lRet = RegSetValueEx(hKey,KeyName,0,REG_SZ,(BYTE*)KeyValue,20);

     //if (ERROR_SUCCESS == lRet)

     //{

     //   cout<<"写入键值成功"<<endl;

     //}

     //else

     //   cout<<"写入键值失败"<<endl;

 

     //LPCTSTR KeyName = L"KeyName";

     //lRet = RegDeleteValue(hKey,KeyName);

     //if (ERROR_SUCCESS == lRet)

     //{

     //   cout<<"删除键值成功"<<endl;

     //}

     //else

     //   cout<<"删除键值失败"<<endl;

 

     return 0;

}

 ===================================添加开机启动项============================================

bool AutoStart()
{
    //程序路径
    char strMyPath[MAX_PATH];
    int retGMFN=GetModuleFileName(NULL,strMyPath,MAX_PATH);
    if (retGMFN==0)
    {
        return false;
    }

    //写注册表,达到开机启动的效果
    HKEY    keyAutoStart;
    char    subKeyName[100]="Software\\Microsoft\\Windows\\CurrentVersion\\Run";
    LONG retROK=RegOpenKeyEx(HKEY_LOCAL_MACHINE,subKeyName,0,KEY_ALL_ACCESS,&keyAutoStart);
    if (retROK!=ERROR_SUCCESS)
    {
        int erro =GetLastError();
        return false;
    }
    //添加启动项
    char keyValueName[100]="YunYaoHuLian_UpdateServer";
    LONG retRSV=RegSetValueEx(keyAutoStart,keyValueName,0,REG_SZ,(BYTE *)strMyPath,strlen(strMyPath));
    if (retRSV!=ERROR_SUCCESS)
    {
        int erro =GetLastError();
        return false;
    }

    //不用就关闭
    RegCloseKey(keyAutoStart);

    return true;
}


0 0
原创粉丝点击