• C++ 读写注册表


    最近自己搞了一下操作注册表的程序,自己对于windows编程真的不了解,于是上网找博客,结果网上的博客,发现没有一个对的,路径好多的符号写反了。。。于是把自己能用的代码放上来。

    代码:

    #include <iostream>  
    #include <algorithm>  
    #include <cmath>  
    #include <vector>  
    #include <string>  
    #include <cstring>
    #include <atlbase.h>
    #include <Windows.h>
    #pragma warning(disable:4996)  
    using namespace std;
    
    void read_dword()//读取操作表,其类型为DWORD
    {
        HKEY hKEY;//定义有关的键,在查询结束时关闭
        //打开与路径data_Set相关的hKEY
    
        LPCTSTR data_set = _T("Software\Chicony\Lenovo1"); 
        
        //访问注册表,hKEY则保存此函数所打开的键的句柄
        if (ERROR_SUCCESS == ::RegOpenKeyEx(HKEY_CURRENT_USER, data_set, 0, KEY_READ, &hKEY))
        {
            DWORD dwValue;//长整型数据,如果是字符串数据用char数组
            DWORD dwSize = sizeof(DWORD);
            DWORD dwType = REG_DWORD;
    
            if (::RegQueryValueEx(hKEY, _T("123"), 0, &dwType, (LPBYTE)&dwValue, &dwSize) != ERROR_SUCCESS)
            {
                cout << "错误:无法查询有关的注册表信息" << endl;
            }
    
            cout << dwValue << endl;
        }
        ::RegCloseKey(hKEY);
    }
    
    void read_reg_sz()//读取操作表,其类型为REG_SZ
    {
        HKEY hkey;
        LPCTSTR data_set = _T("Software\Chicony\Lenovo1");
    
        if (ERROR_SUCCESS == ::RegOpenKeyEx(HKEY_CURRENT_USER, data_set, 0, KEY_READ, &hkey))
        {
            char dwValue[256];
            DWORD dwSzType = REG_SZ;
            DWORD dwSize = sizeof(dwValue);
            if (::RegQueryValueEx(hkey, _T("wangchong"), 0, &dwSzType, (LPBYTE)&dwValue, &dwSize) != ERROR_SUCCESS)
            {
                cout << "无法查询有关的注册表信息" << endl;
            }
            cout << dwValue<< endl;
        }
        ::RegCloseKey(hkey);
    }
    
    void write_dword()//在Software\Chicony\Lenovo1文件夹下写入一个test111的子键,设置其名称为Name,其值为6
    {
        HKEY hkey;//定义有关的hkey,在查询结束时要关闭
        HKEY hTempKey;
        
        DWORD dwValue = 6;
        DWORD dwSize = sizeof(DWORD);
        DWORD dwType = REG_DWORD;
    
        LPCTSTR data_set= _T("Software\Chicony\Lenovo1");
        if (ERROR_SUCCESS == RegOpenKeyEx(HKEY_CURRENT_USER, data_set, 0, KEY_SET_VALUE, &hkey))
        {
            if (ERROR_SUCCESS == ::RegCreateKey(hkey, _T("test111"), &hTempKey))
            {
                if (ERROR_SUCCESS != ::RegSetValueEx(hTempKey, _T("Name"), 0, REG_DWORD, (CONST BYTE*)&dwValue, sizeof(DWORD)))
                {
                    cout <<"写入注册表失败"<< endl;
                }
            }
        }
        ::RegCloseKey(hkey);
    }
    
    void write_reg_sz()
    {
        HKEY hkey;
        HKEY hTempKey;   
        char m_name_set[256]="China";
    
        DWORD len = strlen(m_name_set) + 1;
        LPCTSTR data_set = _T("Software\Chicony\Lenovo1");
        if (ERROR_SUCCESS == ::RegOpenKeyEx(HKEY_CURRENT_USER, data_set, 0, KEY_SET_VALUE, &hkey))
        {
            if (ERROR_SUCCESS == ::RegCreateKey(hkey,_T("test1112"),&hTempKey))
            {
                if (ERROR_SUCCESS!=::RegSetValueEx(hTempKey,_T("Name"),0,REG_SZ,(const BYTE*)m_name_set,len))
                {
                    cout << "写入错误" << endl;
                }
            }
        }
        ::RegCloseKey(hkey);
    }
    
    void write_binary()
    {
        HKEY hkey;
        HKEY hTempKey;
        BYTE m_name[10];
        memset(m_name, 0, sizeof(m_name));
        m_name[0] = 0xff;
        m_name[1] = 0xac;
        m_name[2] = 0x05;
        m_name[3] = 0x4e;
    
        LPCTSTR data_set= _T("Software\Chicony\Lenovo1");
        if (ERROR_SUCCESS == ::RegOpenKeyEx(HKEY_CURRENT_USER, data_set, 0, KEY_SET_VALUE, &hkey))
        {
            if (ERROR_SUCCESS==::RegCreateKey(hkey,_T("test111"),&hTempKey))
            {
                if (ERROR_SUCCESS != ::RegSetValueEx(hTempKey, _T("Name"), 0, REG_BINARY, (unsigned char *)m_name, 5))
                {
                    cout << "写入错误" << endl;
                }
            }
        }
        ::RegCloseKey(hkey);
    }
    
    void delete_value()
    {
        HKEY hkey;
        LPCTSTR data_set = _T("Software\Chicony\Lenovo1\test1112");
    
        if (ERROR_SUCCESS == ::RegOpenKeyEx(HKEY_CURRENT_USER, data_set, 0, KEY_SET_VALUE, &hkey))
        {
            if (ERROR_SUCCESS != ::RegDeleteValue(hkey, _T("Name")))
            {
                cout << "删除错误" << endl;
            }
        }
        ::RegCloseKey(hkey);
    }
    
    void delete_key()
    {
        HKEY hkey;
        LPCTSTR data_set = _T("Software\Chicony\Lenovo1");
    
        if (ERROR_SUCCESS == ::RegOpenKeyEx(HKEY_CURRENT_USER, data_set, 0, KEY_SET_VALUE, &hkey))
        {
            if (ERROR_SUCCESS != ::RegDeleteKey(hkey,"test1112"))
            {
                cout << "删除错误" << endl;
            }
        }
        ::RegCloseKey(hkey);
    }
    
    int main()
    {    
        read_dword();
        read_reg_sz();
        write_reg_sz();
        write_binary();
        delete_value();
        delete_key();
        system("pause");  
        return 0;
    }

    读写注册表之后的效果如图,自己windows编程这块还要努力学习,好多的地方都不懂,希望能和大家多多交流。


    版权声明:本文为博主原创文章,未经博主允许不得转载。

  • 相关阅读:
    javascript基金会——鼠标事件,系统对话框,等等。
    面向对象的方式进行数据交换网络之间的差异
    A*寻路算法lua实现
    鸟哥Linux私房菜 基础学习篇读书笔记(10):Linux磁盘和文件系统管理(3)
    JEECG移动解决方案
    JavaScript事件收集
    OSChina 的URL类的源代码重写过程
    正则表达式注意事项
    随着通信和编程,它是一门艺术系列3(沟通的目的)
    Snmp常用oid
  • 原文地址:https://www.cnblogs.com/lightspeedsmallson/p/4928100.html
Copyright © 2020-2023  润新知