registry.hpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. #pragma once
  2. #include <nall/platform.hpp>
  3. #include <nall/string.hpp>
  4. #include <shlwapi.h>
  5. #undef interface
  6. #ifndef KEY_WOW64_64KEY
  7. #define KEY_WOW64_64KEY 0x0100
  8. #endif
  9. #ifndef KEY_WOW64_32KEY
  10. #define KEY_WOW64_32KEY 0x0200
  11. #endif
  12. #ifndef NWR_FLAGS
  13. #define NWR_FLAGS KEY_WOW64_64KEY
  14. #endif
  15. #ifndef NWR_SIZE
  16. #define NWR_SIZE 4096
  17. #endif
  18. namespace nall {
  19. struct registry {
  20. static auto exists(const string& name) -> bool {
  21. auto part = name.split("\\");
  22. HKEY handle, rootKey = root(part.takeLeft());
  23. string node = part.takeRight();
  24. string path = part.merge("\\");
  25. if(RegOpenKeyExW(rootKey, utf16_t(path), 0, NWR_FLAGS | KEY_READ, &handle) == ERROR_SUCCESS) {
  26. wchar_t data[NWR_SIZE] = L"";
  27. DWORD size = NWR_SIZE * sizeof(wchar_t);
  28. LONG result = RegQueryValueExW(handle, utf16_t(node), nullptr, nullptr, (LPBYTE)&data, (LPDWORD)&size);
  29. RegCloseKey(handle);
  30. if(result == ERROR_SUCCESS) return true;
  31. }
  32. return false;
  33. }
  34. static auto read(const string& name) -> string {
  35. auto part = name.split("\\");
  36. HKEY handle, rootKey = root(part.takeLeft());
  37. string node = part.takeRight();
  38. string path = part.merge("\\");
  39. if(RegOpenKeyExW(rootKey, utf16_t(path), 0, NWR_FLAGS | KEY_READ, &handle) == ERROR_SUCCESS) {
  40. wchar_t data[NWR_SIZE] = L"";
  41. DWORD size = NWR_SIZE * sizeof(wchar_t);
  42. LONG result = RegQueryValueExW(handle, utf16_t(node), nullptr, nullptr, (LPBYTE)&data, (LPDWORD)&size);
  43. RegCloseKey(handle);
  44. if(result == ERROR_SUCCESS) return (const char*)utf8_t(data);
  45. }
  46. return "";
  47. }
  48. static auto write(const string& name, const string& data = "") -> void {
  49. auto part = name.split("\\");
  50. HKEY handle, rootKey = root(part.takeLeft());
  51. string node = part.takeRight(), path;
  52. DWORD disposition;
  53. for(uint n = 0; n < part.size(); n++) {
  54. path.append(part[n]);
  55. if(RegCreateKeyExW(rootKey, utf16_t(path), 0, nullptr, 0, NWR_FLAGS | KEY_ALL_ACCESS, nullptr, &handle, &disposition) == ERROR_SUCCESS) {
  56. if(n == part.size() - 1) {
  57. RegSetValueExW(handle, utf16_t(node), 0, REG_SZ, (BYTE*)(wchar_t*)utf16_t(data), (data.length() + 1) * sizeof(wchar_t));
  58. }
  59. RegCloseKey(handle);
  60. }
  61. path.append("\\");
  62. }
  63. }
  64. static auto remove(const string& name) -> bool {
  65. auto part = name.split("\\");
  66. HKEY rootKey = root(part.takeLeft());
  67. string node = part.takeRight();
  68. string path = part.merge("\\");
  69. if(!node) return SHDeleteKeyW(rootKey, utf16_t(path)) == ERROR_SUCCESS;
  70. return SHDeleteValueW(rootKey, utf16_t(path), utf16_t(node)) == ERROR_SUCCESS;
  71. }
  72. static auto contents(const string& name) -> vector<string> {
  73. vector<string> result;
  74. auto part = name.split("\\");
  75. HKEY handle, rootKey = root(part.takeLeft());
  76. part.removeRight();
  77. string path = part.merge("\\");
  78. if(RegOpenKeyExW(rootKey, utf16_t(path), 0, NWR_FLAGS | KEY_READ, &handle) == ERROR_SUCCESS) {
  79. DWORD folders, nodes;
  80. RegQueryInfoKey(handle, nullptr, nullptr, nullptr, &folders, nullptr, nullptr, &nodes, nullptr, nullptr, nullptr, nullptr);
  81. for(uint n = 0; n < folders; n++) {
  82. wchar_t name[NWR_SIZE] = L"";
  83. DWORD size = NWR_SIZE * sizeof(wchar_t);
  84. RegEnumKeyEx(handle, n, (wchar_t*)&name, &size, nullptr, nullptr, nullptr, nullptr);
  85. result.append(string{(const char*)utf8_t(name), "\\"});
  86. }
  87. for(uint n = 0; n < nodes; n++) {
  88. wchar_t name[NWR_SIZE] = L"";
  89. DWORD size = NWR_SIZE * sizeof(wchar_t);
  90. RegEnumValueW(handle, n, (wchar_t*)&name, &size, nullptr, nullptr, nullptr, nullptr);
  91. result.append((const char*)utf8_t(name));
  92. }
  93. RegCloseKey(handle);
  94. }
  95. return result;
  96. }
  97. private:
  98. static auto root(const string& name) -> HKEY {
  99. if(name == "HKCR") return HKEY_CLASSES_ROOT;
  100. if(name == "HKCC") return HKEY_CURRENT_CONFIG;
  101. if(name == "HKCU") return HKEY_CURRENT_USER;
  102. if(name == "HKLM") return HKEY_LOCAL_MACHINE;
  103. if(name == "HKU" ) return HKEY_USERS;
  104. return nullptr;
  105. }
  106. };
  107. }