1. 读取REG_SZ 类型的注册表键值

     // 读取 REG_SZ 类型键值的代码
    
     HKEY  hKey       = NULL;
    DWORD dwSize = ;
    DWORD dwDataType = ;
    LPBYTE lpValue = NULL;
    LPCTSTR const lpValueName = _T("TcpPort"); LONG lRet = ::RegOpenKeyEx(HKEY_LOCAL_MACHINE,
    _T("SOFTWARE\\Microsoft\\MSSQLServer\\MSSQLServer\\SuperSocketNetLib\\Tcp"),
    ,
    KEY_QUERY_VALUE,
    &hKey);
    if(ERROR_SUCCESS != lRet)
    {
    // Error handling (see this FAQ)
    // return;
    }
    // Call once RegQueryValueEx to retrieve the necessary buffer size
    ::RegQueryValueEx(hKey,
    lpValueName,
    ,
    &dwDataType,
    lpValue, // NULL
    &dwSize); // will contain the data size // Alloc the buffer
    lpValue = (LPBYTE)malloc(dwSize); // Call twice RegQueryValueEx to get the value
    lRet = ::RegQueryValueEx(hKey,
    lpValueName,
    ,
    &dwDataType,
    lpValue,
    &dwSize);
    ::RegCloseKey(hKey);
    if(ERROR_SUCCESS != lRet)
    {
    // Error handling
    // return;
    }
    // Enjoy of lpValue...
    cout << "port ----------------------- " << lpValue << endl; // free the buffer when no more necessary
    free(lpValue); // 此段代码来源:http://forums.codeguru.com/showthread.php?247020-Windows-SDK-Registry-How-can-I-read-data-from-the-registry&s=
  2. 读取REG_DWORD 类型的注册表键值

     // 读取 REG_DWORD 类型的注册表键值代码
    
     long lRet;
    HKEY hKey;
    DWORD port;
    DWORD dwType = REG_DWORD;
    DWORD dwValue;
    lRet = RegOpenKeyEx(
    HKEY_LOCAL_MACHINE,
    "SOFTWARE\\Microsoft\\MSSQLServer\\Client\\SuperSocketNetLib\\Tcp",
    ,
    KEY_QUERY_VALUE,
    &hKey
    ); //打开注册表
    if(lRet == ERROR_SUCCESS)//读操作成功
    {
    lRet = RegQueryValueEx(
    hKey,
    "DefaultPort",
    ,
    &dwType,
    (LPBYTE)&port,
    &dwValue
    ); //如果打开成功,则读
    if(lRet == ERROR_SUCCESS)
    {
    printf("\n打开成功,则读:");
    cout << "DefaultPort: " << port << endl;
    //mcfile << "DefaultPort: " << port << endl;
    mcfile << "<port>" << port << "</port>" << endl;
    }
    //以下是读取失败
    else
    {
    printf("\n读取失败:");
    } }
    //以下是打开失败
    else
    {
    printf("\n打开失败:");
    }
    RegCloseKey(hKey);//记住,一定要关闭
  3. 读取REG_MULTI_SZ 类型的注册表键值

     #include "windows.h"
    #include "tchar.h"
    #include "conio.h"
    #include "stdio.h" #define MY_KEY _T("PathToMyRegistryKey\\MyRegistryKey") // Registry key
    #define MY_VALUES _T("NameOfTheREG_MULTI_SZListOfValues") // Registry values
    #define NEW_VALUE _T("MyNewValue") // New value
    #define FIND_VALUE _T("AnExistingValue") // We will insert the new value after this one int _tmain(int argc, _TCHAR* argv[])
    {
    LONG lResult = ;
    HKEY hKey = NULL;
    LPTSTR lpValues = NULL;
    LPTSTR lpValue = NULL;
    LPTSTR lpNewValues = NULL;
    LPTSTR lpNewValue = NULL;
    DWORD cbValues = ;
    DWORD cbNewValues = ;
    DWORD cbNewValue = ;
    BOOL bFound = FALSE; __try
    {
    // OPEN THE REGISTRY KEY
    //
    _tprintf(_T("RegOpenKeyEx..."));
    lResult = RegOpenKeyEx(
    HKEY_LOCAL_MACHINE,
    MY_KEY,
    ,
    KEY_ALL_ACCESS,
    &hKey
    );
    if (ERROR_SUCCESS != lResult) { _tprintf(_T("ERROR 0x%x\n"), lResult); return ; }
    _tprintf(_T("SUCCESS\n")); // READ THE REG_MULTI_SZ VALUES
    //
    // Get size of the buffer for the values
    _tprintf(_T("RegQueryValueEx..."));
    lResult = RegQueryValueEx(
    hKey,
    MY_VALUES,
    NULL,
    NULL,
    NULL,
    &cbValues
    );
    if (ERROR_SUCCESS != lResult) { _tprintf(_T("ERROR 0x%x\n"), lResult); return ; }
    _tprintf(_T("SUCCESS\n")); // Allocate the buffer
    _tprintf(_T("malloc..."));
    lpValues = (LPTSTR)malloc(cbValues);
    if (NULL == lpValues) { _tprintf(_T("ERROR 0x%x\n"), GetLastError()); return ; }
    _tprintf(_T("SUCCESS\n")); // Get the values
    _tprintf(_T("RegQueryValueEx..."));
    lResult = RegQueryValueEx(
    hKey,
    MY_VALUES,
    NULL,
    NULL,
    (LPBYTE)lpValues,
    &cbValues
    );
    if (ERROR_SUCCESS != lResult) { _tprintf(_T("ERROR 0x%x\n"), lResult); return ; }
    _tprintf(_T("SUCCESS\n")); // SHOW THE VALUES
    //
    _tprintf(_T("\n**************************\n"));
    _tprintf(_T("OLD VALUES\n"));
    _tprintf(_T("**************************\n\n"));
    lpValue = lpValues;
    for (; '\0' != *lpValue; lpValue += _tcslen(lpValue) + )
    {
    // Show one value
    _tprintf(_T("%s\n"), lpValue);
    }
    _tprintf(_T("\n**************************\n\n")); // INSERT A NEW VALUE AFTER A SPECIFIC VALUE IN THE LIST OF VALUES
    //
    // Allocate a new buffer for the old values plus the new one
    _tprintf(_T("malloc..."));
    cbNewValue = (_tcslen(NEW_VALUE) + ) * sizeof(TCHAR);
    cbNewValues = cbValues + cbNewValue;
    lpNewValues = (LPTSTR)malloc(cbNewValues);
    if (NULL == lpNewValues) { _tprintf(_T("ERROR 0x%x\n"), GetLastError()); return ; }
    _tprintf(_T("SUCCESS\n")); // Find the value after which we will insert the new one
    lpValue = lpValues;
    lpNewValue = lpNewValues;
    bFound = FALSE;
    for (; '\0' != *lpValue; lpValue += _tcslen(lpValue) + )
    {
    // Copy the current value to the target buffer
    memcpy(lpNewValue, lpValue, (_tcslen(lpValue) + ) * sizeof(TCHAR)); if ( == _tcscmp(lpValue, FIND_VALUE))
    {
    // The current value is the one we wanted to find
    bFound = TRUE; // Copy the new value to the target buffer
    lpNewValue += _tcslen(lpValue) + ;
    memcpy(lpNewValue, NEW_VALUE, (_tcslen(NEW_VALUE) + ) * sizeof(TCHAR));
    lpNewValue += _tcslen(NEW_VALUE) + ;
    }
    else
    {
    // This is not the value we want, continue to the next one
    lpNewValue += _tcslen(lpValue) + ;
    }
    }
    if (!bFound)
    {
    // We didn't find the value we wanted. Insert the new value at the end
    memcpy(lpNewValue, NEW_VALUE, (_tcslen(NEW_VALUE) + ) * sizeof(TCHAR));
    lpNewValue += _tcslen(NEW_VALUE) + ;
    }
    *lpNewValue = *lpValue; // SHOW THE NEW VALUES
    //
    _tprintf(_T("\n**************************\n"));
    _tprintf(_T("NEW VALUES\n"));
    _tprintf(_T("**************************\n\n"));
    lpNewValue = lpNewValues;
    for (; '\0' != *lpNewValue; lpNewValue += _tcslen(lpNewValue) + )
    {
    // Show one value
    _tprintf(_T("%s\n"), lpNewValue);
    }
    _tprintf(_T("\n**************************\n\n")); // WRITE THE NEW VALUES BACK TO THE KEY
    //
    _tprintf(_T("RegSetValueEx..."));
    lResult = RegSetValueEx(
    hKey,
    MY_VALUES,
    NULL,
    REG_MULTI_SZ,
    (LPBYTE)lpNewValues,
    cbNewValues
    );
    if (ERROR_SUCCESS != lResult) { _tprintf(_T("ERROR 0x%x\n"), lResult); return ; }
    _tprintf(_T("SUCCESS\n"));
    }
    __finally
    {
    // Clean up
    //
    if (NULL != lpValues) { free(lpValues); }
    if (NULL != lpNewValues) { free(lpNewValues); }
    if (NULL != hKey) { RegCloseKey(hKey); } //_tprintf(_T("\n<<PRESS ANY KEY>>\n"));
    //_getch();
    } return ;
    } // 此模板来自http://blogs.msdn.com/b/alejacma/archive/2009/11/12/how-to-manipulate-reg-multi-sz-values-from-the-registry-c.aspx

C++ 读取REG_SZ 、REG_DWORD 、REG_MULTI_SZ 类型注册表值的更多相关文章

  1. c++读取REG_MULTI_SZ类型注册表

    First: run RegQueryValueEx to get type and necessary memory size: Single byte code: DWORD type, size ...

  2. SQL读取注册表值

    最近写一个自动检查SQL Serve安全配置的检查脚本,需要查询注册表,下面是使用SQL查询注册表值的方法. ) ) ) ) --For Named instance --SET @Instance ...

  3. c# 解决读取Excel混合文本类型,数据读取失败的解决方法

    错误重现: ----------------------------------------------------------------------- 在导入Excel读取数据时,其中的一个字段保 ...

  4. Autofac全面解析系列(版本:3.5) – [使用篇(推荐篇):1.类型注册]

    前言 Autofac Autofac是一套高效的依赖注入框架. Autofac官方网站:http://autofac.org/ Autofac在Github上的开源项目:https://github. ...

  5. 利用 Process Monitor 找出某个 Windows 选项所对应的注册表值

    多 时候我们要调整一项 Windows 的功能时只需更改一下注册表即可实现.而很多大家眼中所谓的高手,对 Windows 注册表更是玩得出神入化.难道这些高手把 Windows 注册表都记下来了?答案 ...

  6. 【反射】——Autofac 类型注册

    Autofac是.net界一款轻量化的IOC组件,使用Autofac可以帮助完成代码中很多依赖注入工作.在以前文章中,介绍过Autofac的配置过程(http://www.cnblogs.com/Jn ...

  7. IoC容器Autofac正篇之类型注册(四)

    Autofac类型注册 类型注册简单的从字面去理解就可以了,不必复杂化,只是注册的手段比较丰富. (一)类型/泛型注册 builder.RegisterType<Class1>(); 这种 ...

  8. IoC容器Autofac正篇之类型注册(五)

    Autofac类型注册 类型注册简单的从字面去理解就可以了,不必复杂化,只是注册的手段比较丰富. (一)类型/泛型注册 builder.RegisterType<Class1>(); 这种 ...

  9. java中读取特殊文件的类型

    java中读取特殊文件的类型: 第一种方法(字符拼接读取): public static String getType(String s){ String s1=s.substring(s.index ...

随机推荐

  1. FastDFS安装配置

    FastDFS FastDFS为互联网量身定制,充分考虑了冗余备份.负载均衡.线性扩容等机制,并注重高可用.高性能等指标,使用FastDFS很容易搭建一套高性能的文件服务器集群提供文件上传.下载等服务 ...

  2. 快速排序QuickSort

    前几天实现了直接插入排序.冒泡排序和直接选择排序这三个基础排序.今天看了一下冒泡排序的改进算法,快速排序.单独记录一下,后面还有归并和基数排序等 快速排序 1.选择一个支点默认为数组第一个元素及arr ...

  3. Java存储密码用字符数组

    字符数组和字符串都可以用于存储文本数据,但是在选择具体哪一种时,如果你没有针对具体的情况是很难回答这个问题的.但是任何与字符串相关的问题一定有线索可以在字符串的属性里面找到,比如不可变性.他就用这种方 ...

  4. Kibana4学习<二>

    生产环境部署 Kibana4 是是一个完整的 web 应用.使用时,你需要做的只是打开浏览器,然后输入你运行 Kibana 的机器地址然后加上端口号.比如说:localhost:5601 或者 htt ...

  5. 14、到底改如何区分android的平板、电视、手机

    在没有出现android电视之前,如果要区分平板和手机有很多种方法: 方法1:看是否有通话功能 public boolean isTabletDevice() { TelephonyManager t ...

  6. Zabbix实现微信告警

    zabbix实现微信告警可以分为以下两个步骤: 在百度告警告警平台实现微信告警 将Zabbix接入百度告警平台 微信告警 实现微信告警只需要如下四步: 个人主页关注微信 升级策略配置微信告警 服务管理 ...

  7. 记codevs第一次月赛

    第一次参加这种有奖励的比赛(没错,我就是为猴子而去的 一年没怎么碰代码果然手生,还是用没写多久的C++,差点全跪了 T1数学奇才琪露诺: 首先定义一个函数F(x),F(x)=x的各个数位上的数字和 然 ...

  8. mac os 10.10上安装my eclipse显示virtual memory不足,解决方案

    mac os 10.10上安装my eclipse显示virtual memory不足,安装失败. 自从把OS 升级到10.10 之后, 各种问题, 安装的时候向导提示提示我们说没有足够的虚拟内存, ...

  9. 删除vlan的方法

    方法一: 删除VLAN     先删接口     switch(config)#int ra f0/1 - 5     switch(config-if)#no switchport access v ...

  10. GS线程

    void GameServer::ProcessThread() { try {//在ui线程里面搞个大try不是说try效率不好吗,难道只是为了出现错误发现在GS线程里面出现的吗 ProcessTh ...