|
| 1 | +module stdext.registry; |
| 2 | + |
| 3 | +import stdext.com; |
| 4 | + |
| 5 | +import sdk.win32.winreg; |
| 6 | +import sdk.port.base; |
| 7 | + |
| 8 | +enum { SECURE_ACCESS = ~(WRITE_DAC | WRITE_OWNER | GENERIC_ALL | ACCESS_SYSTEM_SECURITY) } |
| 9 | + |
| 10 | +/*--------------------------------------------------------- |
| 11 | +Registry helpers |
| 12 | +-----------------------------------------------------------*/ |
| 13 | +HRESULT RegQueryValue(HKEY root, in wstring keyname, in wstring valuename, ref wstring value) |
| 14 | +{ |
| 15 | + HKEY key; |
| 16 | + HRESULT hr = hrRegOpenKeyEx(root, keyname, 0, KEY_READ, &key); |
| 17 | + if(FAILED(hr)) |
| 18 | + return hr; |
| 19 | + scope(exit) RegCloseKey(key); |
| 20 | + |
| 21 | + wchar[260] buf; |
| 22 | + DWORD cnt = 260 * wchar.sizeof; |
| 23 | + wchar* szName = _toUTF16zw(valuename); |
| 24 | + DWORD type; |
| 25 | + hr = RegQueryValueExW(key, szName, null, &type, cast(ubyte*) buf.ptr, &cnt); |
| 26 | + if(hr == S_OK && cnt > 0) |
| 27 | + value = to_wstring(buf.ptr); |
| 28 | + if(hr != ERROR_MORE_DATA) |
| 29 | + return HRESULT_FROM_WIN32(hr); |
| 30 | + if (type != REG_SZ) |
| 31 | + return ERROR_DATATYPE_MISMATCH; |
| 32 | + |
| 33 | + scope wchar[] pbuf = new wchar[cnt/2 + 1]; |
| 34 | + hr = RegQueryValueExW(key, szName, null, &type, cast(ubyte*) pbuf.ptr, &cnt); |
| 35 | + if(hr == S_OK) |
| 36 | + value = to_wstring(pbuf.ptr); |
| 37 | + return HRESULT_FROM_WIN32(hr); |
| 38 | +} |
| 39 | + |
| 40 | +HRESULT RegCreateValue(HKEY key, in wstring name, in wstring value) |
| 41 | +{ |
| 42 | + wstring szName = name ~ cast(wchar)0; |
| 43 | + wstring szValue = value ~ cast(wchar)0; |
| 44 | + DWORD dwDataSize = value is null ? 0 : wchar.sizeof * (value.length+1); |
| 45 | + LONG lRetCode = RegSetValueExW(key, szName.ptr, 0, REG_SZ, cast(ubyte*)(szValue.ptr), dwDataSize); |
| 46 | + return HRESULT_FROM_WIN32(lRetCode); |
| 47 | +} |
| 48 | + |
| 49 | +HRESULT RegCreateDwordValue(HKEY key, in wstring name, in DWORD value) |
| 50 | +{ |
| 51 | + wstring szName = name ~ cast(wchar)0; |
| 52 | + LONG lRetCode = RegSetValueExW(key, szName.ptr, 0, REG_DWORD, cast(ubyte*)(&value), value.sizeof); |
| 53 | + return HRESULT_FROM_WIN32(lRetCode); |
| 54 | +} |
| 55 | + |
| 56 | +HRESULT RegCreateQwordValue(HKEY key, in wstring name, in long value) |
| 57 | +{ |
| 58 | + wstring szName = name ~ cast(wchar)0; |
| 59 | + LONG lRetCode = RegSetValueExW(key, szName.ptr, 0, REG_QWORD, cast(ubyte*)(&value), value.sizeof); |
| 60 | + return HRESULT_FROM_WIN32(lRetCode); |
| 61 | +} |
| 62 | + |
| 63 | +HRESULT RegCreateBinaryValue(HKEY key, in wstring name, in void[] data) |
| 64 | +{ |
| 65 | + wstring szName = name ~ cast(wchar)0; |
| 66 | + LONG lRetCode = RegSetValueExW(key, szName.ptr, 0, REG_BINARY, cast(ubyte*)data.ptr, data.length); |
| 67 | + return HRESULT_FROM_WIN32(lRetCode); |
| 68 | +} |
| 69 | + |
| 70 | +HRESULT RegDeleteRecursive(HKEY keyRoot, wstring path) |
| 71 | +{ |
| 72 | + HRESULT hr; |
| 73 | + HKEY key; |
| 74 | + ULONG subKeys = 0; |
| 75 | + ULONG maxKeyLen = 0; |
| 76 | + ULONG currentKey = 0; |
| 77 | + wstring[] keyNames; |
| 78 | + |
| 79 | + hr = hrRegOpenKeyEx(keyRoot, path, 0, (KEY_READ & SECURE_ACCESS), &key); |
| 80 | + if (!FAILED(hr)) |
| 81 | + { |
| 82 | + LONG lRetCode = RegQueryInfoKeyW(key, null, null, null, &subKeys, &maxKeyLen, |
| 83 | + null, null, null, null, null, null); |
| 84 | + if (ERROR_SUCCESS != lRetCode) |
| 85 | + { |
| 86 | + hr = HRESULT_FROM_WIN32(lRetCode); |
| 87 | + } |
| 88 | + else if (subKeys > 0) |
| 89 | + { |
| 90 | + wchar[] keyName = new wchar[maxKeyLen+1]; |
| 91 | + for (currentKey = 0; currentKey < subKeys; currentKey++) |
| 92 | + { |
| 93 | + ULONG keyLen = maxKeyLen+1; |
| 94 | + lRetCode = RegEnumKeyExW(key, currentKey, keyName.ptr, &keyLen, null, null, null, null); |
| 95 | + if (ERROR_SUCCESS == lRetCode) |
| 96 | + keyNames ~= to_wstring(keyName.ptr, keyLen); |
| 97 | + } |
| 98 | + foreach(wstring subkey; keyNames) |
| 99 | + RegDeleteRecursive(key, subkey); |
| 100 | + } |
| 101 | + } |
| 102 | +fail: |
| 103 | + wstring szPath = path ~ cast(wchar)0; |
| 104 | + LONG lRetCode = RegDeleteKeyW(keyRoot, szPath.ptr); |
| 105 | + if (SUCCEEDED(hr) && (ERROR_SUCCESS != lRetCode)) |
| 106 | + hr = HRESULT_FROM_WIN32(lRetCode); |
| 107 | + if (key) RegCloseKey(key); |
| 108 | + return hr; |
| 109 | +} |
| 110 | + |
| 111 | +HRESULT hrRegOpenKeyEx(HKEY root, wstring regPath, int reserved, REGSAM samDesired, HKEY* phkResult) |
| 112 | +{ |
| 113 | + wchar* szRegPath = _toUTF16zw(regPath); |
| 114 | + LONG lRes = RegOpenKeyExW(root, szRegPath, 0, samDesired, phkResult); |
| 115 | + return HRESULT_FROM_WIN32(lRes); |
| 116 | +} |
| 117 | + |
| 118 | +HRESULT hrRegCreateKeyEx(HKEY keySub, wstring regPath, int reserved, wstring classname, DWORD opt, DWORD samDesired, |
| 119 | + SECURITY_ATTRIBUTES* security, HKEY* key, DWORD* disposition) |
| 120 | +{ |
| 121 | + wchar* szRegPath = _toUTF16zw(regPath); |
| 122 | + wchar* szClassname = _toUTF16zw(classname); |
| 123 | + LONG lRes = RegCreateKeyExW(keySub, szRegPath, 0, szClassname, opt, samDesired, security, key, disposition); |
| 124 | + return HRESULT_FROM_WIN32(lRes); |
| 125 | +} |
0 commit comments