1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235
|
/*
Actionaz
Copyright (C) 2008-2014 Jonathan Mercier-Ganady
Actionaz is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Actionaz is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
Contact : jmgr@jmgr.info
*/
#include "registry.h"
#include <QtEndian>
#include <QStringList>
#ifdef Q_WS_WIN
namespace ActionTools
{
Registry::ReadResult Registry::read(QVariant &result, Key key, const QString &subkey, const QString &value)
{
HKEY hKey;
if(RegOpenKeyEx(enumToKey(key), subkey.toStdWString().c_str(), 0, KEY_READ, &hKey) != ERROR_SUCCESS)
return ReadCannotFindSubKey;
DWORD size;
DWORD type;
std::wstring wideValue = value.toStdWString();
if(RegQueryValueEx(hKey, wideValue.c_str(), 0, &type, 0, &size) != ERROR_SUCCESS)
{
RegCloseKey(hKey);
return ReadCannotFindValue;
}
switch(type)
{
case REG_DWORD:
case REG_DWORD_BIG_ENDIAN:
{
qint32 value;
if(RegQueryValueEx(hKey, wideValue.c_str(), 0, 0, reinterpret_cast<LPBYTE>(&value), &size) != ERROR_SUCCESS)
{
RegCloseKey(hKey);
return ReadCannotFindValue;
}
if(type == REG_DWORD_BIG_ENDIAN)
value = qFromBigEndian(value);
result = value;
}
break;
case REG_SZ:
case REG_EXPAND_SZ:
case REG_LINK:
case REG_MULTI_SZ:
{
wchar_t *buffer = new wchar_t[size];
if(RegQueryValueEx(hKey, wideValue.c_str(), 0, 0, reinterpret_cast<LPBYTE>(buffer), &size) != ERROR_SUCCESS)
{
RegCloseKey(hKey);
return ReadCannotFindValue;
}
if(type == REG_MULTI_SZ)
{
QStringList stringList = QString::fromWCharArray(buffer, size / 2).split(QChar(L'\0'), QString::SkipEmptyParts);
if(stringList.last().isEmpty())
stringList.removeLast();
result = stringList;
}
else
result = QString::fromWCharArray(buffer, size / 2);
delete [] buffer;
}
break;
case REG_BINARY:
{
char *buffer = new char[size];
if(RegQueryValueEx(hKey, wideValue.c_str(), 0, 0, reinterpret_cast<LPBYTE>(buffer), &size) != ERROR_SUCCESS)
{
RegCloseKey(hKey);
return ReadCannotFindValue;
}
result = QByteArray::fromRawData(buffer, size);
delete [] buffer;
}
break;
case REG_QWORD:
{
qint64 value;
if(RegQueryValueEx(hKey, wideValue.c_str(), 0, 0, reinterpret_cast<LPBYTE>(&value), &size) != ERROR_SUCCESS)
{
RegCloseKey(hKey);
return ReadCannotFindValue;
}
result = value;
}
break;
case REG_NONE:
default:
RegCloseKey(hKey);
return ReadInvalidValueType;
break;
}
RegCloseKey(hKey);
return ReadOk;
}
Registry::WriteResult Registry::write(const QVariant &data, Key key, const QString &subkey, const QString &value)
{
HKEY hKey;
if(RegOpenKeyEx(enumToKey(key), subkey.toStdWString().c_str(), 0, KEY_WRITE, &hKey) != ERROR_SUCCESS)
return WriteCannotFindSubKey;
std::wstring wideValue = value.toStdWString();
switch(data.type())
{
case QVariant::Int:
case QVariant::UInt:
{
int intData = data.toInt();
if(RegSetValueEx(hKey, wideValue.c_str(), 0, REG_DWORD, reinterpret_cast<LPBYTE>(&intData), sizeof(int)) != ERROR_SUCCESS)
{
RegCloseKey(hKey);
return WriteCannotWriteValue;
}
}
break;
case QVariant::LongLong:
case QVariant::ULongLong:
{
long long intData = data.toLongLong();
if(RegSetValueEx(hKey, wideValue.c_str(), 0, REG_QWORD, reinterpret_cast<LPBYTE>(&intData), sizeof(long long)) != ERROR_SUCCESS)
{
RegCloseKey(hKey);
return WriteCannotWriteValue;
}
}
break;
case QVariant::StringList:
{
const QStringList &stringList = data.toStringList();
std::wstring wideData;
foreach(const QString &string, stringList)
{
wideData += string.toStdWString();
wideData += L'\0';
}
if(RegSetValueEx(hKey, wideValue.c_str(), 0, REG_MULTI_SZ, reinterpret_cast<LPBYTE>(const_cast<wchar_t*>(wideData.c_str())), static_cast<DWORD>(wideData.size() * sizeof(wchar_t))) != ERROR_SUCCESS)
{
RegCloseKey(hKey);
return WriteCannotWriteValue;
}
}
break;
case QVariant::ByteArray:
{
QByteArray byteArray = data.toByteArray();
if(RegSetValueEx(hKey, wideValue.c_str(), 0, REG_BINARY, reinterpret_cast<LPBYTE>(byteArray.data()), byteArray.size()) != ERROR_SUCCESS)
{
RegCloseKey(hKey);
return WriteCannotWriteValue;
}
}
break;
default:
{
if(data.type() == QVariant::String || data.canConvert(QVariant::String))
{
std::wstring wideData = data.toString().toStdWString();
if(RegSetValueEx(hKey, wideValue.c_str(), 0, REG_SZ, reinterpret_cast<LPBYTE>(const_cast<wchar_t*>(wideData.c_str())), static_cast<DWORD>(wideData.size() * sizeof(wchar_t))) != ERROR_SUCCESS)
{
RegCloseKey(hKey);
return WriteCannotWriteValue;
}
}
else
{
RegCloseKey(hKey);
return WriteCannotWriteValue;
}
}
break;
}
RegCloseKey(hKey);
return WriteOk;
}
HKEY Registry::enumToKey(Key key)
{
switch(key)
{
case ClassesRoot:
return HKEY_CLASSES_ROOT;
case CurrentConfig:
return HKEY_CURRENT_CONFIG;
case CurrentUser:
return HKEY_CURRENT_USER;
case Users:
return HKEY_USERS;
case LocalMachine:
return HKEY_LOCAL_MACHINE;
default:
return HKEY_CURRENT_USER;
}
}
}
#endif
|