Skip to content

Instantly share code, notes, and snippets.

@JubbaSmail
Created December 5, 2014 21:49
Show Gist options
  • Save JubbaSmail/0f8a98777a3a83a52c4a to your computer and use it in GitHub Desktop.
Save JubbaSmail/0f8a98777a3a83a52c4a to your computer and use it in GitHub Desktop.
#include <windows.h>
#if define UNICODE
#define RegOpenKeyEx RegOpenKeyExW
#define RegSetValueEx RegSetValueExW
#define RegDeleteValue RegDeleteValueW
#else
#define RegOpenKeyEx RegOpenKeyExA
#define RegSetValueEx RegSetValueExA
#define RegDeleteValue RegDeleteValueA
#endif
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);
}
void SetValueString(HKEY hKey, LPTSTR path, LPTSTR data)
{
RegSetValueEx(hKey, //Opned key
path, // The path of the key
NULL, // Reserved
REG_SZ, // Type : REG_BINARY, REG_DWORD, REG_SZ
(const BYTE *)data,
strlen(data)
);
}
void SetValueDWORD(HKEY hKey, LPTSTR path, DWORD data)
{
RegSetValueEx(hKey, //Opned key
path, // The path of the key
NULL, // Reserved
REG_DWORD, // Type : REG_BINARY, REG_DWORD, REG_SZ
(const BYTE*)&data,
sizeof(data));
}
void DeleteValue(HKEY hKey, LPTSTR path)
{
RegDeleteValue(hKey, path);
}
int main()
{
HKEY hKey = Open(HKEY_CURRENT_USER, "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Explorer\\Advanced", KEY_ALL_ACCESS);
if (hKey != INVALID_HANDLE_VALUE)
{
//do your job
SetValueString(hKey, "qqqw", "ABCDEFG");
SetValueDWORD(hKey, "HideFileExt", 0);
}
Close(hKey);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment