source: nscp/include/REGSettings.h @ 70f2d7b

0.4.00.4.10.4.2stable
Last change on this file since 70f2d7b was 70f2d7b, checked in by Michael Medin <michael@…>, 7 years ago

Updated with reg settings (take 1)

  • Property mode set to 100644
File size: 4.5 KB
Line 
1#pragma once
2
3#include <string>
4#include <windows.h>
5#include <TSettings.h>
6#define BUFF_LEN 4096
7
8#include <iostream>
9class REGSettings : public TSettings
10{
11public:
12        typedef std::list<std::string> sectionList;
13        REGSettings(void)
14        {
15        }
16
17        virtual ~REGSettings(void)
18        {
19        }
20
21        static bool hasSettings() {
22                return getInt_(NS_HKEY_ROOT, NS_REG_ROOT, "use_reg", 0) == 1;
23        }
24
25        std::string getActiveType() {
26                return "registry";
27        }
28
29        sectionList getSections(unsigned int bufferLength = BUFF_LEN) {
30                return getSubKeys_(NS_HKEY_ROOT, NS_REG_ROOT);
31        }
32
33        /**
34        * Get all keys from a section as a list<string>
35        * @param section The section to return all keys from
36        * @return A list with all keys from the section
37        */
38        sectionList getSection(std::string section, unsigned int bufferLength = BUFF_LEN) {
39                return getValues_(NS_HKEY_ROOT, std::string((std::string)NS_REG_ROOT + "\\" + section).c_str());
40        }
41        /**
42        * Get a string from the settings file
43        * @param section Section to read from
44        * @param key Key to retrieve
45        * @param defaultValue Default value to return if key is not found
46        * @return The value or defaultValue if the key is not found
47        */
48        std::string getString(std::string section, std::string key, std::string defaultValue = "") const {
49                return getString_(NS_HKEY_ROOT, std::string((std::string)NS_REG_ROOT + "\\" + section).c_str(), key.c_str(), defaultValue);
50        }
51
52        void setString(std::string section, std::string key, std::string value) {
53        }
54
55        /**
56        * Get an integer from the settings file
57        * @param section Section to read from
58        * @param key Key to retrieve
59        * @param defaultValue Default value to return if key is not found
60        * @return The value or defaultValue if the key is not found
61        */
62        int getInt(std::string section, std::string key, int defaultValue = 0) {
63                return getInt_(NS_HKEY_ROOT, std::string((std::string)NS_REG_ROOT + "\\" + section).c_str(), key.c_str(), defaultValue);
64        }
65        void setInt(std::string section, std::string key, int value) {
66        }
67
68
69        static std::string getString_(HKEY hKey, LPCTSTR lpszPath, LPCTSTR lpszKey, std::string def) {
70                std::string ret = def;
71                HKEY hTemp;
72                if (RegOpenKeyEx(hKey, lpszPath, 0, KEY_QUERY_VALUE, &hTemp) != ERROR_SUCCESS) {
73                        return def;
74                }
75                DWORD type;
76                DWORD cbData = 1024;
77                BYTE *bData = new BYTE[cbData];
78                BOOL bRet = RegQueryValueEx(hTemp, lpszKey, NULL, &type, bData, &cbData);
79                if (type != REG_SZ) {
80                        bRet = false;
81                }
82                RegCloseKey(hTemp);
83                if (bRet) {
84                        ret = (LPCTSTR)bData;
85                }
86                delete [] bData;
87                return ret;
88        }
89        static DWORD getInt_(HKEY hKey, LPCTSTR lpszPath, LPCTSTR lpszKey, DWORD def) {
90                DWORD ret = def;
91                LONG bRet;
92                HKEY hTemp;
93                if ((bRet = RegOpenKeyEx(hKey, lpszPath, 0, KEY_READ, &hTemp)) != ERROR_SUCCESS) {
94                        return def;
95                }
96                DWORD type;
97                DWORD cbData = 1024;
98                BYTE *bData = new BYTE[sizeof(DWORD)];
99                bRet = RegQueryValueEx(hTemp, lpszKey, NULL, &type, bData, &cbData);
100                if (type != REG_DWORD) {
101                        bRet = -1;
102                }
103                RegCloseKey(hTemp);
104                if (bRet == ERROR_SUCCESS) {
105                        ret = (DWORD)*bData;
106                }
107                delete [] bData;
108                return ret;
109        }
110        static sectionList getValues_(HKEY hKey, LPCTSTR lpszPath) {
111                sectionList ret;
112                LONG bRet;
113                HKEY hTemp;
114                if ((bRet = RegOpenKeyEx(hKey, lpszPath, 0, KEY_READ, &hTemp)) != ERROR_SUCCESS) {
115                        return ret;
116                }
117                DWORD    cValues=0;
118                DWORD    cMaxValLen;
119                // Get the class name and the value count.
120                bRet = RegQueryInfoKey(hTemp,NULL,NULL,NULL,NULL,NULL,NULL,&cValues,&cMaxValLen,NULL,NULL,NULL);
121                if ((bRet == ERROR_SUCCESS)&&(cValues>0)) {
122                        TCHAR *lpValueName = new TCHAR[cMaxValLen+1];
123                        for (unsigned int i=0; i<cValues; i++) {
124                                DWORD len = cMaxValLen;
125                                bRet = RegEnumValue(hKey, i, lpValueName, &len, NULL, NULL, NULL, NULL);
126                                if (bRet == ERROR_SUCCESS) {
127                                        ret.push_back(std::string(lpValueName));
128                                }
129                        }
130                        delete [] lpValueName;
131                }
132                return ret;
133        }
134        static sectionList getSubKeys_(HKEY hKey, LPCTSTR lpszPath) {
135                sectionList ret;
136                LONG bRet;
137                HKEY hTemp;
138                if ((bRet = RegOpenKeyEx(hKey, lpszPath, 0, KEY_READ, &hTemp)) != ERROR_SUCCESS) {
139                        return ret;
140                }
141                DWORD    cSubKeys=0;
142                DWORD    cMaxKeyLen;
143                // Get the class name and the value count.
144                bRet = RegQueryInfoKey(hTemp,NULL,NULL,NULL,&cSubKeys,&cMaxKeyLen,NULL,NULL,NULL,NULL,NULL,NULL);
145                if ((bRet == ERROR_SUCCESS)&&(cSubKeys>0)) {
146                        TCHAR *lpValueName = new TCHAR[cMaxKeyLen+1];
147                        for (unsigned int i=0; i<cSubKeys; i++) {
148                                DWORD len = cMaxKeyLen;
149                                bRet = RegEnumKey(hKey, i, lpValueName, len);
150                                if (bRet == ERROR_SUCCESS) {
151                                        ret.push_back(std::string(lpValueName));
152                                }
153                        }
154                        delete [] lpValueName;
155                }
156                return ret;
157        }
158};
Note: See TracBrowser for help on using the repository browser.