访问注册表 【_WIN32_WINNT_NT4】 (例)

#ifndef _WIN32_WINNT
#define _WIN32_WINNT _WIN32_WINNT_NT4
#endif

#include <stdio.h>
#include <tchar.h>
#include <windows.h>

int _tmain(int argc, _TCHAR* argv[])
{
HKEY hKey;
DWORD dwType, dwGetSize, dwSetSize;


// Set String KeyValue
if (ERROR_SUCCESS == RegCreateKeyEx(HKEY_LOCAL_MACHINE, _T("SOFTWARE\TestKey\TestSubKey\"), 0, 0, REG_OPTION_NON_VOLATILE, KEY_SET_VALUE, 0, &hKey, 0))
{
TCHAR *pSetString = _T("Hello Registry");
dwSetSize = _tcslen(pSetString) * sizeof(TCHAR);

RegSetValueEx(hKey, _T("TestString"), 0, REG_SZ, (LPBYTE)pSetString, dwSetSize);

RegCloseKey(hKey);
}


// Get String KeyValue
if (ERROR_SUCCESS == RegOpenKeyEx(HKEY_LOCAL_MACHINE, _T("SOFTWARE\TestKey\TestSubKey\"), 0, KEY_QUERY_VALUE, &hKey))
{
dwGetSize = dwSetSize + sizeof(TCHAR);
TCHAR *pGetString = (TCHAR *)malloc(dwGetSize);

RegQueryValueEx(hKey, _T("TestString"), 0, &dwType, (LPBYTE)pGetString, &dwGetSize);

_tprintf(_T("TestString: %sn"), pGetString);
free(pGetString);

RegCloseKey(hKey);
}


// Delete String KeyValue
if (ERROR_SUCCESS == RegOpenKeyEx(HKEY_LOCAL_MACHINE, _T("SOFTWARE\TestKey\TestSubKey\"), 0, KEY_SET_VALUE, &hKey))
{
RegDeleteValue(hKey, _T("TestString"));

RegCloseKey(hKey);
}


// Set DWORD KeyValue
if (ERROR_SUCCESS == RegCreateKeyEx(HKEY_LOCAL_MACHINE, _T("SOFTWARE\TestKey\TestSubKey\"), 0, 0, REG_OPTION_NON_VOLATILE, KEY_SET_VALUE, 0, &hKey, 0))
{
DWORD dwSetValue = 3;
dwSetSize = sizeof(dwSetValue);

RegSetValueEx(hKey, _T("TestDWORD"), 0, REG_DWORD, (LPBYTE)&dwSetValue, dwSetSize);

RegCloseKey(hKey);
}


// Get DWORD KeyValue
if (ERROR_SUCCESS == RegOpenKeyEx(HKEY_LOCAL_MACHINE, _T("SOFTWARE\TestKey\TestSubKey\"), 0, KEY_QUERY_VALUE, &hKey))
{
DWORD dwGetValue;
dwGetSize = sizeof(dwGetValue);

RegQueryValueEx(hKey, _T("TestDWORD"), 0, &dwType, (LPBYTE)&dwGetValue, &dwGetSize);

_tprintf(_T("TestDWORD: %dn"), dwGetValue);

RegCloseKey(hKey);
}


// Delete DWORD KeyValue
if (ERROR_SUCCESS == RegOpenKeyEx(HKEY_LOCAL_MACHINE, _T("SOFTWARE\TestKey\TestSubKey\"), 0, KEY_SET_VALUE, &hKey))
{
RegDeleteValue(hKey, _T("TestDWORD"));

RegCloseKey(hKey);
}


// Set Binary KeyValue
if (ERROR_SUCCESS == RegCreateKeyEx(HKEY_LOCAL_MACHINE, _T("SOFTWARE\TestKey\TestSubKey\"), 0, 0, REG_OPTION_NON_VOLATILE, KEY_SET_VALUE, 0, &hKey, 0))
{
TCHAR szSetBinary[32] = {_T('H'), _T('e'), _T('l'), _T('l'), _T('o'), _T(' '), _T('R'), _T('e'), _T('g'), _T('i'), _T('s'), _T('t'), _T('r'), _T('y'), _T('')};
dwSetSize = 32 * sizeof(TCHAR);

RegSetValueEx(hKey, _T("TestBinary"), 0, REG_BINARY, (LPBYTE)szSetBinary, dwSetSize);

RegCloseKey(hKey);
}


// Get Binary KeyValue
if (ERROR_SUCCESS == RegOpenKeyEx(HKEY_LOCAL_MACHINE, _T("SOFTWARE\TestKey\TestSubKey\"), 0, KEY_QUERY_VALUE, &hKey))
{
TCHAR szGetBinary[32] = {0};
dwGetSize = 32 * sizeof(TCHAR);

RegQueryValueEx(hKey, _T("TestBinary"), 0, &dwType, (LPBYTE)szGetBinary, &dwGetSize);

_tprintf(_T("TestBinary: %sn"), szGetBinary);

RegCloseKey(hKey);
}


// Delete Binary KeyValue
if (ERROR_SUCCESS == RegOpenKeyEx(HKEY_LOCAL_MACHINE, _T("SOFTWARE\TestKey\TestSubKey\"), 0, KEY_SET_VALUE, &hKey))
{
RegDeleteValue(hKey, _T("TestBinary"));

RegCloseKey(hKey);
}


// Delete SubKey
RegDeleteKey(HKEY_LOCAL_MACHINE, _T("SOFTWARE\TestKey\TestSubKey\"));
RegDeleteKey(HKEY_LOCAL_MACHINE, _T("SOFTWARE\TestKey\"));

return 0;
}

留下评论

您的邮箱地址不会被公开。 必填项已用 * 标注