目前共有6篇帖子。 內容轉換:不轉換▼
 
點擊 回復
675 5
【方法】C++读取注册表字符串值的方法(按字符串长度动态分配内存空间)
一派護法 十九級
1樓 發表于:2016-2-25 22:25
【程序】
// regtest.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include <iostream>
#include <Windows.h>

using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
    HKEY hkey;
    RegOpenKeyEx(HKEY_CURRENT_USER, TEXT("Software\\Microsoft\\Internet Explorer\\Suggested Sites"), NULL, KEY_READ, &hkey);

    DWORD cValues;
    RegQueryInfoKey(hkey, NULL, NULL, NULL, NULL, NULL, NULL, &cValues, NULL, NULL, NULL, NULL);
    cout << "该键下共有" << cValues << "个值 (不含默认值)" << endl;

    DWORD cbData;
    RegGetValueA(hkey, NULL, "SlicePath", RRF_RT_REG_SZ, NULL, NULL, &cbData);
    cout << "读取字符串时应该分配的内存空间大小为: " << cbData << "字节" << endl;

    char *szValue = (char *)malloc(cbData); // 注意: cbData的单位是字节
    RegGetValueA(hkey, NULL, "SlicePath", RRF_RT_REG_SZ, NULL, szValue, &cbData);
    cout << szValue << endl; // 由于在控制台中输出宽字符串比较麻烦,所以这里为了简单起见用了ANSI版本
    cout << "该字符串的实际长度为: " << strlen(szValue) << endl;
    free(szValue);
    
    RegCloseKey(hkey);
    system("pause");
    return 0;
}
【输出】
该键下共有10个值 (不含默认值)
读取字符串时应该分配的内存空间大小为: 47字节
C:\Users\Octopus\Favorites\Links\建议网站.url
该字符串的实际长度为: 45
请按任意键继续. . .
一派護法 十九級
2樓 發表于:2016-2-25 22:29
这里要注意的是,由于cbData的单位为字节而非数组的元素个数,所以在用Unicode版本的API函数时要特别注意。最好用C语言的malloc函数分配空间,不要用new WCHAR[cbData],因为这样的话实际上就分配了cbData * sizeof(WCHAR)字节的内存,造成浪费。
一派護法 十九級
3樓 發表于:2016-2-25 22:48
在注册表中写入字符串非常简单:
// RegTest2.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include <Windows.h>

int _tmain(int argc, _TCHAR* argv[])
{
    // 打开注册表键
    // 如果键不存在,则自动创建(这个非常常用)
    HKEY hkey;
    RegCreateKey(HKEY_CURRENT_USER, TEXT("Software\\应用程序向导生成的本地应用程序\\test"), &hkey);

    TCHAR szValue[] = TEXT("这是一个中文字符串");
    RegSetValueEx(hkey, NULL, NULL, REG_SZ, (BYTE *)szValue, sizeof(szValue));

    TCHAR szValue2[] = TEXT("简体中文abc");
    RegSetValueEx(hkey, TEXT("string"), NULL, REG_SZ, (BYTE *)szValue2, sizeof(szValue2));
   
    DWORD dwValue = 0x12345678;
    RegSetValueEx(hkey, TEXT("number"), NULL, REG_DWORD, (BYTE *)&dwValue, sizeof(DWORD));

    RegCloseKey(hkey);
    return 0;
}
一派護法 十九級
5樓 發表于:2016-2-25 23:12
可以将1楼所述的方法做成一个返回std::string字符串的函数:
// regtest.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include <iostream>
#include <string>
#include <Windows.h>

using namespace std;

string RegReadStringA(HKEY hkey, char *szSubKey)
{
    DWORD cbData;
    RegGetValueA(hkey, NULL, szSubKey, RRF_RT_REG_SZ, NULL, NULL, &cbData);
   
    char *szData = new char[cbData];
    RegGetValueA(hkey, NULL, szSubKey, RRF_RT_REG_SZ, NULL, szData, &cbData);
    string str(szData);
    delete[] szData;
    return str;
}

int _tmain(int argc, _TCHAR* argv[])
{
    HKEY hkey;
    RegOpenKeyEx(HKEY_CURRENT_USER, TEXT("Software\\Microsoft\\Internet Explorer\\Suggested Sites"), NULL, KEY_READ, &hkey);

    string value = RegReadStringA(hkey, "SlicePath");
    cout << value << endl;
   
    RegCloseKey(hkey);
    system("pause");
    return 0;
}
一派護法 十九級
6樓 發表于:2016-2-25 23:13
对应的宽字符串版本:
// regtest.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include <string>
#include <Windows.h>

using namespace std;

wstring RegReadStringW(HKEY hkey, wchar_t *szSubKey)
{
    DWORD cbData;
    RegGetValueW(hkey, NULL, szSubKey, RRF_RT_REG_SZ, NULL, NULL, &cbData);
   
    wchar_t *szData = (wchar_t *)malloc(cbData);
    RegGetValueW(hkey, NULL, szSubKey, RRF_RT_REG_SZ, NULL, szData, &cbData);
    wstring str(szData);
    free(szData);
    return str;
}

int _tmain(int argc, _TCHAR* argv[])
{
    HKEY hkey;
    RegOpenKeyEx(HKEY_CURRENT_USER, TEXT("Software\\Microsoft\\Internet Explorer\\Suggested Sites"), NULL, KEY_READ, &hkey);

    wstring value = RegReadStringW(hkey, L"SlicePath");
    MessageBoxW(NULL, value.c_str(), L"字符串", MB_ICONINFORMATION);
   
    RegCloseKey(hkey);
    return 0;
}
一派護法 十九級
7樓 發表于:2016-2-29 23:34
如果要获取一个键下的所有字符串值,可以先通过RegQueryInfoKey函数获取最长字符串值的长度,然后一次性分配空间,提高效率。

回復帖子

內容:
用戶名: 您目前是匿名發表
驗證碼:
(快捷鍵:Ctrl+Enter)
 

本帖信息

點擊數:675 回複數:5
評論數: ?
作者: 巨大八爪鱼
最後回復:巨大八爪鱼
最後回復時間:2016-2-29 23:34
 
©2010-2024 Arslanbar Ver2.0
除非另有聲明,本站採用創用CC姓名標示-相同方式分享 3.0 Unported許可協議進行許可。