Welcome to mirror list, hosted at ThFree Co, Russian Federation.

registry.cpp « DSUtilLite « common - github.com/mpc-hc/LAVFilters.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 7fb16c9886f4f857ec6aca9561ea6b5dfc619602 (plain)
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
/*
 *      Copyright (C) 2010-2013 Hendrik Leppkes
 *      http://www.1f0.de
 *
 *  This program 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 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program 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, write to the Free Software Foundation, Inc.,
 *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

#include "stdafx.h"
#include "registry.h"

bool CreateRegistryKey(HKEY hKeyRoot, LPCTSTR pszSubKey)
{
  HKEY hKey;
  LONG  lRet;

  lRet = RegCreateKeyEx(
    hKeyRoot,
    pszSubKey,
    0,
    nullptr,
    REG_OPTION_NON_VOLATILE,
    KEY_WRITE,
    nullptr,
    &hKey,
    nullptr
    );

  if(lRet == ERROR_SUCCESS)
  {
    RegCloseKey(hKey);
    hKey = (HKEY)nullptr;
    return true;
  }

  SetLastError((DWORD)lRet);
  return false;
}

CRegistry::CRegistry()
{
}

CRegistry::CRegistry(HKEY hkeyRoot, LPCTSTR pszSubKey, HRESULT &hr, BOOL bReadOnly, BOOL b64Bit)
{
  hr = Open(hkeyRoot, pszSubKey, bReadOnly, b64Bit);
}

CRegistry::~CRegistry()
{
  if (m_key)
    RegCloseKey(*m_key);
  delete m_key;
}

HRESULT CRegistry::Open(HKEY hkeyRoot, LPCTSTR pszSubKey, BOOL bReadOnly, BOOL b64Bit)
{
  LONG lRet;
  
  if (m_key != nullptr) { return E_UNEXPECTED; }
  
  m_key = new HKEY();
  REGSAM sam = bReadOnly ? KEY_READ : KEY_READ|KEY_WRITE;
  if (b64Bit) sam |= KEY_WOW64_64KEY;
  lRet = RegOpenKeyEx(hkeyRoot, pszSubKey, 0, sam, m_key);
  if (lRet != ERROR_SUCCESS) {
    delete m_key;
    m_key = nullptr;
    return E_FAIL;
  }
  return S_OK;
}

std::wstring CRegistry::ReadString(LPCTSTR pszKey, HRESULT &hr)
{
  LONG lRet;
  DWORD dwSize;
  std::wstring result;

  hr = S_OK;

  if (m_key == nullptr) { hr = E_UNEXPECTED;  return result; }

  lRet = RegQueryValueEx(*m_key, pszKey, nullptr, nullptr, nullptr, &dwSize);

  if (lRet == ERROR_SUCCESS) {
    // Alloc Buffer to fit the data
    WCHAR *buffer = (WCHAR *)CoTaskMemAlloc(dwSize);
    if (!buffer) { hr = E_OUTOFMEMORY; return result; }
    memset(buffer, 0, dwSize);
    lRet = RegQueryValueEx(*m_key, pszKey, nullptr, nullptr, (LPBYTE)buffer, &dwSize);
    result = std::wstring(buffer);
    CoTaskMemFree(buffer);
  }
  
  if (lRet != ERROR_SUCCESS) {
    hr = E_FAIL;
  }

  return result;
}

HRESULT CRegistry::WriteString(LPCTSTR pszKey, const LPCTSTR pszValue)
{
  LONG lRet;
  HRESULT hr;

  hr = S_OK;

  if (m_key == nullptr) { return E_UNEXPECTED; }

  lRet = RegSetValueEx(*m_key, pszKey, 0, REG_SZ, (const BYTE *)pszValue, (DWORD)((wcslen(pszValue) + 1) * sizeof(WCHAR)));
  if (lRet != ERROR_SUCCESS) {
    return E_FAIL;
  }
  return S_OK;
}

DWORD CRegistry::ReadDWORD(LPCTSTR pszKey, HRESULT &hr)
{
  LONG lRet;
  DWORD dwSize = sizeof(DWORD);
  DWORD dwVal = 0;

  hr = S_OK;

  if (m_key == nullptr) { hr = E_UNEXPECTED; return 0; }

  lRet = RegQueryValueEx(*m_key, pszKey, 0, nullptr, (LPBYTE)&dwVal, &dwSize);

  if (lRet != ERROR_SUCCESS) {
    hr = E_FAIL;
  }

  return dwVal;
}

HRESULT CRegistry::WriteDWORD(LPCTSTR pszKey, DWORD dwValue)
{
  LONG lRet;
  HRESULT hr;

  hr = S_OK;

  if (m_key == nullptr) { return E_UNEXPECTED; }

  lRet = RegSetValueEx(*m_key, pszKey, 0, REG_DWORD, (const BYTE *)&dwValue, sizeof(dwValue));
  if (lRet != ERROR_SUCCESS) {
    return E_FAIL;
  }
  return S_OK;
}

BOOL CRegistry::ReadBOOL(LPCTSTR pszKey, HRESULT &hr)
{
  DWORD dwVal = ReadDWORD(pszKey, hr);
  return dwVal ? TRUE : FALSE;
}

HRESULT CRegistry::WriteBOOL(LPCTSTR pszKey, BOOL bValue)
{
  return WriteDWORD(pszKey, bValue);
}

BYTE *CRegistry::ReadBinary(LPCTSTR pszKey, DWORD &dwSize, HRESULT &hr)
{
  LONG lRet;
  BYTE *result = nullptr;

  hr = S_OK;

  if (m_key == nullptr) { hr = E_UNEXPECTED;  return result; }

  lRet = RegQueryValueEx(*m_key, pszKey, nullptr, nullptr, nullptr, &dwSize);

  if (lRet == ERROR_SUCCESS) {
    // Alloc Buffer to fit the data
    result = (BYTE *)CoTaskMemAlloc(dwSize);
    if (!result) { hr = E_OUTOFMEMORY; return result; }
    memset(result, 0, dwSize);
    lRet = RegQueryValueEx(*m_key, pszKey, nullptr, nullptr, (LPBYTE)result, &dwSize);
  }

  if (lRet != ERROR_SUCCESS) {
    hr = E_FAIL;
    CoTaskMemFree(result);
    result = nullptr;
  }

  return result;
}

HRESULT CRegistry::WriteBinary(LPCTSTR pszKey, const BYTE *pbValue, int iLen)
{
  LONG lRet;
  HRESULT hr;

  hr = S_OK;

  if (m_key == nullptr) { return E_UNEXPECTED; }

  lRet = RegSetValueEx(*m_key, pszKey, 0, REG_BINARY, (const BYTE *)pbValue, iLen);
  if (lRet != ERROR_SUCCESS) {
    return E_FAIL;
  }
  return S_OK;
}

HRESULT CRegistry::DeleteKey(LPCTSTR pszKey)
{
  LONG lRet;

  if (m_key == nullptr) { return E_UNEXPECTED; }
  lRet = RegDeleteValue(*m_key, pszKey);
  if (lRet != ERROR_SUCCESS) {
    return E_FAIL;
  }
  return S_OK;
}