Clone
ixen <ixen@copyhandler.com>
committed
on 18 Mar 19
Removed dependency of chext on ATL (CH-339)
ch-1.40 + 2 more
src/ch/TRegistry.cpp (deleted)
1   // ============================================================================
2   //  Copyright (C) 2001-2015 by Jozef Starosczyk
3   //  ixen@copyhandler.com
4   //
5   //  This program is free software; you can redistribute it and/or modify
6   //  it under the terms of the GNU Library General Public License
7   //  (version 2) as published by the Free Software Foundation;
8   //
9   //  This program is distributed in the hope that it will be useful,
10   //  but WITHOUT ANY WARRANTY; without even the implied warranty of
11   //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12   //  GNU General Public License for more details.
13   //
14   //  You should have received a copy of the GNU Library General Public
15   //  License along with this program; if not, write to the
16   //  Free Software Foundation, Inc.,
17   //  59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
18   // ============================================================================
19   #include "stdafx.h"
20   #include "TRegistry.h"
21  
22   TRegistry::TRegistry(HKEY key, const wchar_t* pszKey)
23   {
24           LSTATUS lStatus = RegOpenKeyEx(key, pszKey, 0, KEY_QUERY_VALUE, &m_hKey);
25           if (lStatus != ERROR_SUCCESS || m_hKey == nullptr)
26                   throw std::runtime_error("Cannot open registry key");
27   }
28  
29   TRegistry::~TRegistry()
30   {
31           if(m_hKey)
32                   RegCloseKey(m_hKey);
33   }
34  
35   bool TRegistry::QueryString(const wchar_t* pszValueKey, std::wstring& wstrValue)
36   {
37           DWORD dwType = REG_SZ;
38           const DWORD stMaxBuffer = 1024;
39           std::unique_ptr<wchar_t[]> buf(new wchar_t[stMaxBuffer]);
40  
41           DWORD dwCount = stMaxBuffer;
42           LSTATUS lStatus = RegQueryValueEx(m_hKey, pszValueKey, nullptr, &dwType, (BYTE*)buf.get(), &dwCount);
43           if (lStatus != ERROR_SUCCESS)
44                   return false;
45  
46           buf[dwCount / 2] = L'\0';
47           wstrValue = buf.get();
48  
49           return true;
50   }
51  
52   bool TRegistry::QueryDword(const wchar_t* pszValueKey, DWORD& dwOutValue)
53   {
54           DWORD dwType = REG_DWORD;
55           DWORD dwCount = sizeof(DWORD);
56           DWORD dwValue = 0;
57           LSTATUS lStatus = RegQueryValueEx(m_hKey, pszValueKey, nullptr, &dwType, (BYTE*)&dwValue, &dwCount);
58           if (lStatus != ERROR_SUCCESS)
59                   return false;
60  
61           dwOutValue = dwValue;
62  
63           return true;
64   }