Skip to content

Instantly share code, notes, and snippets.

@JubbaSmail
Created December 5, 2014 21:47
Show Gist options
  • Save JubbaSmail/94d06ed365bc4f912664 to your computer and use it in GitHub Desktop.
Save JubbaSmail/94d06ed365bc4f912664 to your computer and use it in GitHub Desktop.
#include <windows.h>
#if define UNICODE
#define RegOpenKeyEx RegOpenKeyExW
#define RegCreateKeyEx RegCreateKeyExW
#define RegDeleteKey RegDeleteKeyW
#else
#define RegOpenKeyEx RegOpenKeyExA
#define RegCreateKeyEx RegCreateKeyExA
#define RegDeleteKey RegDeleteKeyA
#endif
#define MAX_KEY_LENGTH 255
HKEY Open(HKEY root,LPTSTR path, REGSAM permission)
{
HKEY hKey;
LONG lResult = RegOpenKeyEx(
root, //HKEY_LOCAL_MACHINE, HKEY_USERS, HKEY_CURRENT_CONFIG, HKEY_CLASSES_ROOT, HKEY_CURRENT_USER
path, // the path of the key
0, // must be 0
permission, //KEY_WRITE, KEY_READ, KEY_ALL_ACCESS, KEY_QUERY_VALUE, KEY_ENUMERATE_SUB_KEYS
&hKey // OUT : The result [pass by refrence]
);
if (lResult == ERROR_SUCCESS)
return hKey;
return INVALID_HANDLE_VALUE;
}
void Close(HKEY hKey)
{
RegCloseKey(hKey);
}
HKEY CreateKey(HKEY hKey, LPTSTR path, REGSAM permission)
{
DWORD result;
HKEY hKeyResult;
RegCreateKeyEx(hKey, // the opened key
path, // the path of the key
NULL, //Reserved
NULL, // Class Name
0, // 0 || REG_OPTION_NON_VOLATILE || REG_OPTION_VOLATILE
permission, // KEY_WRITE, KEY_READ, KEY_ALL_ACCESS, KEY_QUERY_VALUE, KEY_ENUMERATE_SUB_KEYS
NULL, // Security Attribute
&hKeyResult, // OUT : return the created key
&result // OUT : return REG_CREATED_NEW_KEY || REG_OPENED_EXISTING_KEY
);
if (result == REG_CREATED_NEW_KEY)
return hKeyResult;
return INVALID_HANDLE_VALUE;
}
void DeleteKey(HKEY hKey, LPTSTR path)
{
RegDeleteKey(hKey, path);
}
int main()
{
HKEY hKey = Open(HKEY_CURRENT_USER, "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Explorer", KEY_ALL_ACCESS);
if (hKey != INVALID_HANDLE_VALUE)
{
//do your job
if (CreateKey(hKey, "xyz",KEY_ALL_ACCESS) == INVALID_HANDLE_VALUE)
DeleteKey(hKey, "xyz");
}
Close(hKey);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment