代碼:ios
#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; }
版權聲明:本文爲博主原創文章,未經博主容許不得轉載。編程