#include #include #include #include #pragma comment(lib, "ktmw32") int main() { LSTATUS st; // // Create a transaction. // HANDLE hTrans = CreateTransaction(NULL, NULL, 0, 0, 0, 0, NULL); if (hTrans == INVALID_HANDLE_VALUE) { printf("CreateTransaction failed with error %u\n", GetLastError()); return 1; } // // Create a HKLM\Software\Test key within the transaction. // HKEY hTestKey; st = RegCreateKeyTransactedA(HKEY_LOCAL_MACHINE, "Software\\Test", 0, NULL, REG_OPTION_VOLATILE, KEY_ALL_ACCESS, NULL, &hTestKey, NULL, hTrans, NULL); if (st != ERROR_SUCCESS) { printf("RegCreateKeyTransactedA failed with error %d\n", st); return 1; } // // Set a new value in the transactionally created key. // st = RegSetValueExA(hTestKey, "Value", 0, REG_SZ, (const BYTE*)"SECRET", 6); if (st != ERROR_SUCCESS) { printf("RegSetValueExA failed with error %d\n", st); return 1; } // // Create another sub key. // HKEY hSubKey; st = RegCreateKeyTransactedA(hTestKey, "SubKey", 0, NULL, REG_OPTION_VOLATILE, KEY_ALL_ACCESS, NULL, &hSubKey, NULL, hTrans, NULL); if (st != ERROR_SUCCESS) { printf("RegCreateKeyTransactedA failed with error %d\n", st); return 1; } // // Set a custom security descriptor on the parent key to only allow access to // administrators. // PSECURITY_DESCRIPTOR lpSecurityDescriptor; CONST WCHAR* szSD = L"D:PAI(A;CI;KA;;;BA)"; // Allow KEY_ALL_ACCESS for Administrators. if (!ConvertStringSecurityDescriptorToSecurityDescriptorW(szSD, SDDL_REVISION_1, &lpSecurityDescriptor, NULL)) { printf("ConvertStringSecurityDescriptorToSecurityDescriptorW failed with " "error %u\n", GetLastError()); return 1; } st = RegSetKeySecurity(hTestKey, DACL_SECURITY_INFORMATION, lpSecurityDescriptor); if (st != ERROR_SUCCESS) { printf("RegSetKeySecurity failed with error %d\n", st); return 1; } // // Commit the transaction. // if (!CommitTransaction(hTrans)) { printf("CommitTransaction failed with error %u\n", GetLastError()); return 1; } RegCloseKey(hSubKey); RegCloseKey(hTestKey); CloseHandle(hTrans); return 0; }