root/swishctl/src/regkey.cpp

Revision 1236, 3.6 kB (checked in by augur, 5 years ago)

Added license and copyright information.

  • Property svn:eol-style set to native
  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
Line 
1 /*
2  * $Id$
3  *
4  * SwishCtl - SWISH-E API ActiveX Control
5  * Copyright (c) 2003 Peoples Resource Centre Wellington NZ
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20  */
21
22 /* - - - - - - - - - - - - - - - - - - - - - - - - - - -
23  * RegKey Starts
24  * - - - - - - - - - - - - - - - - - - - - - - - - - - - */
25 #include "stdafx.h"
26 #include <string>
27 using namespace std;
28
29 #include "regkey.h"
30
31 RegKey::RegKey(HKEY startkey)
32 {
33         hkey=NULL;
34         this->startkey = startkey;
35 }
36
37 bool RegKey::CreateKey(const char *keyname)
38 {
39         if ( ! keyname ) return false;
40         if (hkey) RegCloseKey(hkey);
41
42         LONG res = RegCreateKeyEx(startkey,  keyname,
43                         0, NULL, REG_OPTION_NON_VOLATILE, KEY_WRITE, 0, &hkey, 0 ) ;
44
45         return res == ERROR_SUCCESS;
46 }
47
48 bool RegKey::OpenKey(const char *keyname, REGSAM samdesired )
49 {
50         if ( ! keyname ) return false;
51         if (hkey) RegCloseKey(hkey);
52 #if 0
53         LONG res = RegOpenKeyEx (startkey,
54                                                         (const char *)keyname,
55                                                         0,
56                                                         samdesired,
57                                                         &hkey);
58 #endif
59         DWORD disposition;
60         LONG res = RegCreateKeyEx(
61           startkey,                // handle to an open key
62           (const char *)keyname,         // address of subkey name
63           0,           // reserved
64           NULL,           // address of class string
65           REG_OPTION_NON_VOLATILE,          // special options flag
66           samdesired,        // desired security access
67           NULL,
68                                                                 // address of key security structure
69           &hkey,          // address of buffer for opened handle
70           &disposition   // address of disposition value buffer
71         );
72  
73         return res == ERROR_SUCCESS;
74 }
75
76 bool RegKey::OpenKey(const char *keyname)
77 {
78         return OpenKey(keyname, KEY_ALL_ACCESS );
79 }
80
81 bool RegKey::SetValue(const char *valuename, const char *value)
82 {
83         if ( ! valuename || ! value ) return false;
84         LONG res = RegSetValueEx( hkey, valuename, 0, REG_SZ,
85                                                           (const unsigned char *)value, lstrlen(value)+1);
86         return res == ERROR_SUCCESS;
87 }
88
89
90 string RegKey::QueryValue(const char *valuename)
91 {
92         DWORD dwType = REG_SZ;
93
94         if (! hkey ) return NULL; // can I do this ?
95
96         LPBYTE lpData = NULL;
97         DWORD cbData = 0;
98
99         LONG res = RegQueryValueEx(     hkey, valuename, 0, &dwType, NULL, &cbData );
100        
101 //      if ( res != ERROR_MORE_DATA ) { // this should be true but isn't
102 //              ::MessageBox( NULL, "ERROR_MORE_DATA should have been seen...",
103 //                      "Whatever", MB_OK );
104 //              return NULL;   
105 //      }
106
107         lpData = (LPBYTE) malloc( cbData );
108
109         if ( !lpData ) {
110                 ::MessageBox( NULL, "Out of memory error occured in QueryValue function.",
111                         "SwishCtl Error", MB_OK | MB_ICONINFORMATION);
112
113                 return *(new string());
114         }
115
116         res = RegQueryValueEx(  hkey, valuename, 0, &dwType, lpData, &cbData );
117        
118         if (res != ERROR_SUCCESS) {
119                 free( lpData );
120                 return *(new string());
121         }
122
123         string *temp = new string( (char *)lpData ) ;
124         free( lpData );
125
126         return *temp;
127 }
128
129 RegKey::~RegKey()
130 {
131         if ( hkey )     ::RegCloseKey(hkey);
132 }
133
134 bool RegKey::DeleteKey(const char *subkey)
135 {
136         return ::RegDeleteKey(hkey, subkey ) == ERROR_SUCCESS;
137 }
138
139 /* - - - - - - - - - - - - - - - - - - - - - - - - - - -
140  * RegKey ends
141  * - - - - - - - - - - - - - - - - - - - - - - - - - - - */
Note: See TracBrowser for help on using the browser.