1 #include <iostream> 2 #include <windows.h> 3 using namespace std; 4 5 int main() 6 { 7 HKEY hKEY; 8 HKEY hKEY1; 9 10 char *RegSz = "This is a test!"; 11 DWORD RegDword = 16; 12 BYTE RegBin[4] = {0x54, 0x65, 0x73, 0x74}; 13 14 // 以写方式打开注册表 15 if(ERROR_SUCCESS == RegOpenKeyEx(HKEY_LOCAL_MACHINE, "SYSTEM", 0, KEY_SET_VALUE, &hKEY)) 16 { 17 // 写注册表子键 Test 18 if(ERROR_SUCCESS == RegCreateKey(hKEY, "Test", &hKEY1)) 19 { 20 // 在子键 Test 下写 REG_SZ 类型键值 TestRegSz 21 if(ERROR_SUCCESS == RegSetValueEx(hKEY1, "TestRegSz", 0, REG_SZ, (BYTE*)RegSz, strlen(RegSz) + 1)) 22 { 23 // 在子键 Test 下写 REG_DWORD 类型键值 TestRegDword 24 if(ERROR_SUCCESS == RegSetValueEx(hKEY1, "TestRegDword", 0, REG_DWORD, (BYTE*)&RegDword, sizeof(DWORD))) 25 { 26 // 在子键 Test 下写 REG_BINARY 类型键值 TestRegBin 27 if(ERROR_SUCCESS == RegSetValueEx(hKEY1, "TestRegBin", 0, REG_BINARY, RegBin, sizeof(RegBin))) 28 { 29 MessageBox(NULL, "写注册表键值成功!", "提示:", MB_ICONEXCLAMATION); 30 } 31 } 32 } 33 else 34 { 35 MessageBox(NULL, "写注册表键值失败!", "提示:", MB_ICONERROR); 36 } 37 } 38 else 39 { 40 MessageBox(NULL, "写注册表子键失败!", "提示:", MB_ICONERROR); 41 } 42 43 RegCloseKey(hKEY1); 44 } 45 else 46 { 47 MessageBox(NULL, "打开注册表失败!", "提示:", MB_ICONERROR); 48 } 49 RegCloseKey(hKEY); 50 return 0; 51 }