// File:game_registry.h #pragma once #include <Windows.h> #ifndef GAME_REGISTRY_H #define GAME_REGISTRY_H // 注册表类 class game_registry { private: HKEY mRootKey; BOOL mIsOpened; public: game_registry(void); ~game_registry(void); BOOL Open(HKEY rootKey, LPCTSTR subKey); BOOL Close(void); BOOL CreateKey(HKEY rootKey, LPCTSTR subKey); BOOL DeleteKey(HKEY rootKey, LPCTSTR subKey); BOOL SetValue(LPCTSTR valueName, LPCTSTR value); BOOL SetValue(LPCTSTR valueName, DWORD value); BOOL SetValueForMultiSz(LPCTSTR valueName, LPCTSTR value, DWORD ByteLength); BOOL GetValue(LPCTSTR valueName, LPCTSTR value, LPDWORD bufferLength); BOOL GetValue(LPCTSTR valueName, LPDWORD value); BOOL DeleteValue(LPCTSTR valueName); }; #endif // File:game_registry.cpp #include "game_registry.h" game_registry::game_registry(void) { mRootKey = NULL; mIsOpened = FALSE; } game_registry::~game_registry(void) { } BOOL game_registry::Open(HKEY rootKey, LPCTSTR subKey) { if (!subKey) { return FALSE; } if (mIsOpened) { return FALSE; } if (RegOpenKey(rootKey, subKey, &mRootKey)!=ERROR_SUCCESS) { return FALSE; } mIsOpened = TRUE; return TRUE; } BOOL game_registry::Close(void) { if (RegCloseKey(mRootKey)!=ERROR_SUCCESS) { return FALSE; } mIsOpened = FALSE; return TRUE; } BOOL game_registry::CreateKey(HKEY rootKey, LPCTSTR subKey) { if (!subKey) { return FALSE; } if (mIsOpened) { return FALSE; } if (RegCreateKey(rootKey, subKey, &mRootKey)!=ERROR_SUCCESS) { return FALSE; } mIsOpened = TRUE; return TRUE; } BOOL game_registry::DeleteKey(HKEY rootKey, LPCTSTR subKey) { if (!subKey) { return FALSE; } if (RegDeleteKey(rootKey, subKey)!=ERROR_SUCCESS) { return FALSE; } return TRUE; } BOOL game_registry::SetValue(LPCTSTR valueName, LPCTSTR value) { if (!valueName||!value) { return FALSE; } if (!mIsOpened) { return FALSE; } if (RegSetValueEx(mRootKey, valueName, 0, REG_SZ, (BYTE*)value, (DWORD)lstrlen(value)*sizeof(TCHAR))!=ERROR_SUCCESS) { return FALSE; } return TRUE; } BOOL game_registry::SetValue(LPCTSTR valueName, DWORD value) { if (!valueName||!value) { return FALSE; } if (!mIsOpened) { return FALSE; } if (RegSetValueEx(mRootKey, valueName, 0, REG_DWORD, (BYTE*)&value, sizeof(DWORD))!=ERROR_SUCCESS) { return FALSE; } return TRUE; } BOOL game_registry::SetValueForMultiSz(LPCTSTR valueName, LPCTSTR value, DWORD ByteLength) { if (!valueName||!value) { return FALSE; } if (!mIsOpened) { return FALSE; } if (RegSetValueEx(mRootKey, valueName, 0, REG_MULTI_SZ, (BYTE*)value, ByteLength)!=ERROR_SUCCESS) { return FALSE; } return TRUE; } BOOL game_registry::GetValue(LPCTSTR valueName, LPCTSTR value, LPDWORD bufferLength) { DWORD ValueType = 0; if (!valueName||!value||bufferLength) { return FALSE; } if (!mIsOpened) { return FALSE; } if (RegQueryValueEx(mRootKey, valueName, 0, &ValueType, (BYTE*)value, bufferLength)!=ERROR_SUCCESS) { return FALSE; } return TRUE; } BOOL game_registry::GetValue(LPCTSTR valueName, LPDWORD value) { DWORD ValueType = 0; DWORD BufferLength = sizeof(DWORD); if (!valueName||!value) { return FALSE; } if (!mIsOpened) { return FALSE; } if (RegQueryValueEx(mRootKey, valueName, 0, &ValueType, (BYTE*)value, &BufferLength)) { return FALSE; } return TRUE; } BOOL game_registry::DeleteValue(LPCTSTR valueName) { if (!valueName) { return FALSE; } if (!mIsOpened) { return FALSE; } if (RegDeleteValue(mRootKey, valueName)!=ERROR_SUCCESS) { return FALSE; } return TRUE; } // File:Main.cpp #include <Windows.h> #include <iostream> using namespace std; #include "game_registry.h" int main() { DWORD gLastIndex =10; game_registry g; g.Open(HKEY_LOCAL_MACHINE,L"SOFTWARE//Microsoft//Windows NT//CurrentVersion//Perflib"); g.GetValue(L"Last Counter", &gLastIndex); g.Close(); cout<<gLastIndex; }