C++ 读取REG_SZ 、REG_DWORD 、REG_MULTI_SZ 类型注册表值

该博客介绍了如何使用C++代码读取Windows注册表中REG_SZ(字符串)、REG_DWORD(双字节整数)和REG_MULTI_SZ(多字符串)类型的键值,提供了具体的示例代码,包括读取TCP端口和默认端口等示例。
摘要由CSDN通过智能技术生成
  1. 读取REG_SZ 类型的注册表键值

     1 // 读取 REG_SZ 类型键值的代码
     2 
     3 HKEY  hKey       = NULL;
     4 DWORD dwSize     = 0;
     5 DWORD dwDataType = 0;
     6 LPBYTE lpValue   = NULL;
     7 LPCTSTR const lpValueName = _T("TcpPort");
     8 
     9 LONG lRet = ::RegOpenKeyEx(HKEY_LOCAL_MACHINE,
    10    _T("SOFTWARE\\Microsoft\\MSSQLServer\\MSSQLServer\\SuperSocketNetLib\\Tcp"),
    11    0,
    12    KEY_QUERY_VALUE,
    13    &hKey);
    14 if(ERROR_SUCCESS != lRet)
    15 {
    16    // Error handling (see this FAQ)
    17    // return;
    18 }
    19 // Call once RegQueryValueEx to retrieve the necessary buffer size
    20 ::RegQueryValueEx(hKey, 
    21    lpValueName,
    22    0,
    23    &dwDataType,
    24    lpValue,  // NULL
    25    &dwSize); // will contain the data size
    26 
    27 // Alloc the buffer
    28 lpValue = (LPBYTE)malloc(dwSize);
    29 
    30 // Call twice RegQueryValueEx to get the value
    31 lRet = ::RegQueryValueEx(hKey, 
    32    lpValueName,
    33    0,
    34    &dwDataType,
    35    lpValue,
    36    &dwSize);
    37 ::RegCloseKey(hKey);
    38 if(ERROR_SUCCESS != lRet)
    39 {
    40    // Error handling
    41    // return;
    42 }
    43 // Enjoy of lpValue...
    44 cout << "port ----------------------- " << lpValue << endl;
    45 
    46 // free the buffer when no more necessary
    47 free(lpValue);
    48 
    49 // 此段代码来源:http://forums.codeguru.com/showthread.php?247020-Windows-SDK-Registry-How-can-I-read-data-from-the-registry&s=
    View Code

     

  2. 读取REG_DWORD 类型的注册表键值

     1 // 读取 REG_DWORD 类型的注册表键值代码
     2 
     3 long lRet;
     4 HKEY hKey;
     5 DWORD port;
    
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值