您的位置:首页 > 其它

vc读写注册表实例

2015-02-05 17:14 357 查看
//获取HKEY_CURRENT_USER\\Software\\UI_Repeater表下面的值,并且修改

HKEY hKey;

std::unique_ptr<BYTE[]> AccChg_value(new BYTE[80]);

DWORD dwType = REG_DWORD;

DWORD sizedwType = 80;

CString m_strOwner;

//打开注册表

long rc = RegOpenKeyEx(HKEY_CURRENT_USER, TEXT("Software\\UI_Repeater"), NULL, KEY_READ | KEY_WRITE, &hKey);

if (rc != ERROR_SUCCESS)

{

//MessageBoxA(NULL, "错误:无法打开有关的注册表", "提示", MB_OK);

return;

}

else

{

//读取注册表

rc = RegQueryValueExA(hKey, "AccChg", NULL, &dwType, AccChg_value.get(), &sizedwType);

if (rc != ERROR_SUCCESS)

{

//MessageBoxA(NULL, "错误:无法查询有关的注册表信息", "提示", MB_OK | MB_ICONWARNING);

return;

}

if (dwType == REG_DWORD) {

ULONG value = *(PULONG)AccChg_value.get();

if (value == 1)

{

DWORD user_config = 0;

DWORD size_config = sizeof(user_config);

//AccCmp

//修改注册表

long ret1 = ::RegSetValueEx(hKey, TEXT("AccChg"), NULL, dwType, (CONST BYTE*)&user_config, size_config);

if (ret1 != ERROR_SUCCESS)

{

//MessageBoxA(NULL, "错误:无法设置有关的注册表信息", "提示", MB_OK | MB_ICONWARNING);

return;

}

boost::thread threads(boost::bind(&CMainDlg::thread_proc, this));

threads.detach();

}

}

::RegCloseKey(hKey);

}

第二种方法:

void save_info_to_regedit(tstring ips, tstring user_id, tstring pwd ,tstring is_checked)

{

std_string str_is_checked = is_checked;

std_string str_ips = ips;

std_string str_user_id = user_id;

std_string str_pwd = pwd;

HKEY hKey;

//HEKY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Run

long re = RegOpenKeyExA(HKEY_CURRENT_USER, "Software\\Microsoft\\Windows\\CurrentVersion\\Run", 0, KEY_WRITE, &hKey);

//新增一个值,名称随意命名,值为要开机运行的文件的完整路径

RegSetValueExA(hKey, "is_save", 0, REG_SZ, (const unsigned char*)str_is_checked.c_str(), strlen(str_is_checked.c_str()));

RegSetValueExA(hKey, "ip_prot", 0, REG_SZ, (const unsigned char*)str_ips.c_str(), strlen(str_ips.c_str()));

RegSetValueExA(hKey, "user_id", 0, REG_SZ, (const unsigned char*)str_user_id.c_str(), strlen(str_user_id.c_str()));

RegSetValueExA(hKey, "pwd", 0, REG_SZ, (const unsigned char*)str_pwd.c_str(), strlen(str_pwd.c_str()));

//关闭注册表:

RegCloseKey(hKey);

}

BOOL load_from_regedit(std::map<tstring, tstring> & buf)

{

HKEY hKey;

// tstring is_keep_info, ip, id, pwd;

BYTE buff[51]={0},buff1[51]={0},buff2[51]={0},buff3[51]={0};

//TCHAR is_keep_info[50] = {0};

DWORD dwLen = 51 * sizeof(BYTE);

DWORD dwType = REG_SZ;

RegOpenKeyExA(HKEY_CURRENT_USER,"Software\\Microsoft\\Windows\\CurrentVersion\\Run",0,KEY_READ, &hKey);

RegQueryValueExA(hKey, "ip_prot", NULL, &dwType, buff1, &dwLen); //读取右侧的字符串值

std_wstring ip((char*)buff1);

RegQueryValueExA(hKey,"user_id",NULL,&dwType,buff2,&dwLen); //读取右侧的字符串值

std_wstring id((char*)buff2);

RegQueryValueExA(hKey,"pwd",NULL,&dwType,buff3,&dwLen); //读取右侧的字符串值

std_wstring pwd((char*)buff3);

RegQueryValueExA(hKey, "is_save", NULL, &dwType, buff, &dwLen); //读取右侧的字符串值

std_wstring is_keep_info((char*)buff);

RegCloseKey(hKey);

buf[_T("is_keep_info")] = is_keep_info;

buf[_T("ip")] = ip;

buf[_T("id")] = id;

buf[_T("pwd")] = pwd;

boost::posix_time::ptime now = boost::posix_time::second_clock::local_time();

buf[_T("time")] = CA2T(boost::posix_time::to_iso_string(now).c_str());

return TRUE;

}

第三种方法:

std_string strIsProtect("1");
HKEY hKey;
//HEKY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Run
RegOpenKeyExA(HKEY_CURRENT_USER,"Software\\Microsoft", 0, KEY_SET_VALUE, &hKey);
RegSetValueExA(hKey, "isProtect",0, REG_SZ, (const unsigned char*)strIsProtect.c_str(), strlen(strIsProtect.c_str()));
RegCloseKey(hKey);
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: