winnt-win2000-win98-afs-client-updates-20010623
[openafs.git] / src / WINNT / win9xpanel / CRegkey.cpp
1 /* Copyright 2000, International Business Machines Corporation and others.
2         All Rights Reserved.
3  
4         This software has been released under the terms of the IBM Public
5         License.  For details, see the LICENSE file in the top-level source
6         directory or online at http://www.openafs.org/dl/license10.html
7 */
8 #include "stdafx.h"
9 #include "cregkey.h"
10
11 #ifdef _DEBUG
12 #define new DEBUG_NEW
13 #undef THIS_FILE
14 static char THIS_FILE[] = __FILE__;
15 #endif
16
17 CRegkey::CRegkey(HKEY hKey,const char *subkey,const char *skey)
18 {
19         DWORD disposition,result;
20         m_kPkey=m_kSkey=0;
21         result=(RegCreateKeyEx(hKey     //HKEY_LOCAL_MACHINE
22                 ,subkey                 //"Software\\IBM"
23                 ,0,NULL
24                 ,REG_OPTION_NON_VOLATILE
25                 ,KEY_ALL_ACCESS,NULL
26                 ,&m_kPkey
27                 ,&disposition)==ERROR_SUCCESS);
28         if(!result)
29         {
30                 AfxMessageBox("AFS Error - Could Not create a registration key!");
31                 m_kPkey=0;
32         }
33         result=(RegCreateKeyEx(m_kPkey
34                 ,skey
35                 ,0,NULL
36                 ,REG_OPTION_NON_VOLATILE
37                 ,KEY_ALL_ACCESS,NULL
38                 ,&m_kSkey,&disposition)==ERROR_SUCCESS);
39         if(!result)
40         {
41                 RegCloseKey(m_kPkey);
42                 AfxMessageBox("AFS Error - Could Not create a registration key!");
43                 m_kPkey=m_kSkey=0;
44         }
45         m_dwIndex=0;
46 }
47
48 BOOL CRegkey::Enumerate()
49 {
50         DWORD result;
51         CString lpName;
52         DWORD dwLen=128;
53         char *p=lpName.GetBuffer(dwLen);
54         FILETIME ftLastWriteTime;
55         result=RegEnumKeyEx(m_kSkey,// handle to key to enumerate
56         m_dwIndex++,              // subkey index
57         p,              // subkey name
58         &dwLen,            // size of subkey buffer
59         NULL,         // reserved
60         NULL,             // class string buffer
61         NULL,           // size of class string buffer
62         &ftLastWriteTime // last write time
63         );
64         switch (result)
65         {
66         case ERROR_NO_MORE_ITEMS:
67                 return TRUE;
68         case ERROR_SUCCESS:
69                 return TRUE;
70         default:
71                 AfxMessageBox("AFS Error - Could Not enumerate a registration key!");
72                 return FALSE;
73         }
74         return TRUE;
75 }
76
77
78 CRegkey::CRegkey(const char *skey)
79 {
80         DWORD disposition,result;
81         m_kPkey=m_kSkey=0;
82         result=(RegCreateKeyEx(HKEY_CURRENT_USER
83                 ,"Software\\IBM"
84                 ,0,NULL
85                 ,REG_OPTION_NON_VOLATILE
86                 ,KEY_ALL_ACCESS,NULL
87                 ,&m_kPkey
88                 ,&disposition)==ERROR_SUCCESS);
89         if(!result)
90         {
91                 AfxMessageBox("AFS Error - Could Not create a registration key!");
92                 m_kPkey=0;
93         }
94         result=(RegCreateKeyEx(m_kPkey
95                 ,skey
96                 ,0,NULL
97                 ,REG_OPTION_NON_VOLATILE
98                 ,KEY_ALL_ACCESS,NULL
99                 ,&m_kSkey,&disposition)==ERROR_SUCCESS);
100         if(!result)
101         {
102                 RegCloseKey(m_kPkey);
103                 AfxMessageBox("AFS Error - Could Not create a registration key!");
104                 m_kPkey=m_kSkey=0;
105         }
106 }
107
108 BOOL CRegkey::GetString(const char *field,CString &buffer,DWORD len)
109 {
110         if (m_kSkey==0) return FALSE;
111         UCHAR *pBuffer=(UCHAR *)buffer.GetBuffer(len);
112         DWORD type;
113         if (RegQueryValueEx(m_kSkey
114                 ,field,0,&type
115                 ,pBuffer
116                 ,&len)!=ERROR_SUCCESS)
117         {
118                 buffer.ReleaseBuffer();
119                 return FALSE; //assume never been set
120         }
121         ASSERT(type==REG_SZ);
122         buffer.ReleaseBuffer(len);
123         return TRUE;
124 }
125
126 BOOL CRegkey::PutString(const char *field,const char *pBuffer)
127 {
128         if (m_kSkey==0)
129                 return FALSE;
130         if ((pBuffer)&&(strlen(pBuffer)))
131                 return (RegSetValueEx(m_kSkey,field,0,REG_SZ,(CONST BYTE *)pBuffer,strlen(pBuffer))==ERROR_SUCCESS);
132         return (RegSetValueEx(m_kSkey,field,0,REG_SZ,(CONST BYTE *)"",0)==ERROR_SUCCESS);
133 }
134
135
136 BOOL CRegkey::GetBinary(const char *field,LPBYTE msg,DWORD &len)
137 {
138         if (m_kSkey==0)
139                 return FALSE;
140         DWORD type;
141         if (RegQueryValueEx(m_kSkey
142                         ,field,0,&type
143                         ,msg
144                         ,&len)!=ERROR_SUCCESS)
145         {
146                 return FALSE; //assume never been set
147         }
148         ASSERT(type==REG_BINARY);
149         return TRUE;
150 }
151
152 BOOL CRegkey::PutBinary(const char *field,LPBYTE msg,DWORD len)
153 {
154         CHAR empty[1];
155         empty[0]=0;
156         if (m_kSkey==0)
157                 return FALSE;
158         if (msg)
159                 return (RegSetValueEx(m_kSkey,field,0,REG_BINARY,msg,len)==ERROR_SUCCESS);
160         return (RegSetValueEx(m_kSkey,field,0,REG_BINARY,(LPBYTE)&empty,0)==ERROR_SUCCESS);
161 }
162
163 CRegkey::~CRegkey()
164 {
165         if (m_kSkey)
166                 RegCloseKey(m_kSkey);
167         if (m_kPkey)
168                 RegCloseKey(m_kPkey);
169 }
170