1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
| #include "stdafx.h" #include <Windows.h> #include <iostream> using namespace std; /************************************ @ Brief: 打开注册表,读取Key对应value @ Author: woniu201 @ Created: 2018/09/07 @ Return: ************************************/ int ReadReg(char* path, char* key, char* value) { HKEY hKey; int ret = RegOpenKeyEx(HKEY_CURRENT_USER, path, 0, KEY_EXECUTE, &hKey); if (ret != ERROR_SUCCESS) { cout << "打开注册表失败" << endl; return 1; } //读取KEY DWORD dwType = REG_SZ; //数据类型 DWORD cbData = 256; ret = RegQueryValueEx(hKey, key, NULL, &dwType, (LPBYTE)value, &cbData); if (ret == ERROR_SUCCESS) { cout << value << endl; } else { cout << "读取注册表中KEY 失败" << endl; RegCloseKey(hKey); return 1; } RegCloseKey(hKey); return 0; } /************************************ @ Brief: 写注册表,如不存在自动创建 @ Author: woniu201 @ Created: 2018/09/07 @ Return: ************************************/ int WriteReg(char* path, char* key, char* value) { HKEY hKey; DWORD dwDisp; DWORD dwType = REG_SZ; //数据类型 int ret = RegCreateKeyEx(HKEY_CURRENT_USER, path,0, NULL, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &hKey, &dwDisp); if (ret != ERROR_SUCCESS) { cout << "创建注册表失败" << endl; return 1; } ret == RegSetValueEx(hKey, key, 0, dwType, (BYTE*)value, strlen(value)); if (ret != ERROR_SUCCESS) { cout << "注册表中创建KEY VALUE失败" << endl; RegCloseKey(hKey); return 1; } RegCloseKey(hKey); return 0; } /************************************ @ Brief: 删除注册表 @ Author: woniu201 @ Created: 2018/09/07 @ Return: ************************************/ int DelReg(char* path) { int ret = RegDeleteKey(HKEY_CURRENT_USER, path); if (ret == ERROR_SUCCESS) { cout << "删除成功" << endl; } else { cout << "删除失败" << endl; return 1; } return 0; } int _tmain(int argc, _TCHAR* argv[]) { char value[32] = {0}; ReadReg("Software\\Woniu", "aaa", value); WriteReg("Software\\Woniu", "aaa", "bbb"); DelReg("Software\\Woniu"); getchar(); return 0; }
|