1*b1cdbd2cSJim Jagielski /************************************************************** 2*b1cdbd2cSJim Jagielski * 3*b1cdbd2cSJim Jagielski * Licensed to the Apache Software Foundation (ASF) under one 4*b1cdbd2cSJim Jagielski * or more contributor license agreements. See the NOTICE file 5*b1cdbd2cSJim Jagielski * distributed with this work for additional information 6*b1cdbd2cSJim Jagielski * regarding copyright ownership. The ASF licenses this file 7*b1cdbd2cSJim Jagielski * to you under the Apache License, Version 2.0 (the 8*b1cdbd2cSJim Jagielski * "License"); you may not use this file except in compliance 9*b1cdbd2cSJim Jagielski * with the License. You may obtain a copy of the License at 10*b1cdbd2cSJim Jagielski * 11*b1cdbd2cSJim Jagielski * http://www.apache.org/licenses/LICENSE-2.0 12*b1cdbd2cSJim Jagielski * 13*b1cdbd2cSJim Jagielski * Unless required by applicable law or agreed to in writing, 14*b1cdbd2cSJim Jagielski * software distributed under the License is distributed on an 15*b1cdbd2cSJim Jagielski * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16*b1cdbd2cSJim Jagielski * KIND, either express or implied. See the License for the 17*b1cdbd2cSJim Jagielski * specific language governing permissions and limitations 18*b1cdbd2cSJim Jagielski * under the License. 19*b1cdbd2cSJim Jagielski * 20*b1cdbd2cSJim Jagielski *************************************************************/ 21*b1cdbd2cSJim Jagielski 22*b1cdbd2cSJim Jagielski 23*b1cdbd2cSJim Jagielski 24*b1cdbd2cSJim Jagielski #ifndef _REGISTRY_HXX_ 25*b1cdbd2cSJim Jagielski #define _REGISTRY_HXX_ 26*b1cdbd2cSJim Jagielski 27*b1cdbd2cSJim Jagielski #ifdef _MSC_VER 28*b1cdbd2cSJim Jagielski #pragma warning(push, 1) /* disable warnings within system headers */ 29*b1cdbd2cSJim Jagielski #endif 30*b1cdbd2cSJim Jagielski #include <windows.h> 31*b1cdbd2cSJim Jagielski #ifdef _MSC_VER 32*b1cdbd2cSJim Jagielski #pragma warning(pop) 33*b1cdbd2cSJim Jagielski #endif 34*b1cdbd2cSJim Jagielski 35*b1cdbd2cSJim Jagielski #include <memory> 36*b1cdbd2cSJim Jagielski #include <vector> 37*b1cdbd2cSJim Jagielski #include <string> 38*b1cdbd2cSJim Jagielski 39*b1cdbd2cSJim Jagielski #include "registryvalueimpl.hxx" 40*b1cdbd2cSJim Jagielski 41*b1cdbd2cSJim Jagielski //--------------------------------------- 42*b1cdbd2cSJim Jagielski // forward declaration 43*b1cdbd2cSJim Jagielski //--------------------------------------- 44*b1cdbd2cSJim Jagielski 45*b1cdbd2cSJim Jagielski class RegistryKeyImpl; 46*b1cdbd2cSJim Jagielski 47*b1cdbd2cSJim Jagielski //--------------------------------------- 48*b1cdbd2cSJim Jagielski // typedefs 49*b1cdbd2cSJim Jagielski //--------------------------------------- 50*b1cdbd2cSJim Jagielski 51*b1cdbd2cSJim Jagielski typedef std::auto_ptr<RegistryKeyImpl> RegistryKey; 52*b1cdbd2cSJim Jagielski typedef std::vector<std::wstring> StringList; 53*b1cdbd2cSJim Jagielski typedef std::auto_ptr<StringList> StringListPtr; 54*b1cdbd2cSJim Jagielski 55*b1cdbd2cSJim Jagielski //--------------------------------------- 56*b1cdbd2cSJim Jagielski // 57*b1cdbd2cSJim Jagielski //--------------------------------------- 58*b1cdbd2cSJim Jagielski 59*b1cdbd2cSJim Jagielski class RegistryKeyImpl 60*b1cdbd2cSJim Jagielski { 61*b1cdbd2cSJim Jagielski public: 62*b1cdbd2cSJim Jagielski 63*b1cdbd2cSJim Jagielski //############################################ 64*b1cdbd2cSJim Jagielski // Destruction 65*b1cdbd2cSJim Jagielski //############################################ 66*b1cdbd2cSJim Jagielski 67*b1cdbd2cSJim Jagielski virtual ~RegistryKeyImpl(); 68*b1cdbd2cSJim Jagielski 69*b1cdbd2cSJim Jagielski 70*b1cdbd2cSJim Jagielski //############################################ 71*b1cdbd2cSJim Jagielski // Queries 72*b1cdbd2cSJim Jagielski //############################################ 73*b1cdbd2cSJim Jagielski 74*b1cdbd2cSJim Jagielski 75*b1cdbd2cSJim Jagielski /** The name of the key at hand, maybe empty 76*b1cdbd2cSJim Jagielski if this is any of the root keys 77*b1cdbd2cSJim Jagielski */ 78*b1cdbd2cSJim Jagielski std::wstring GetName() const; 79*b1cdbd2cSJim Jagielski 80*b1cdbd2cSJim Jagielski /** The number of sub values of the key at hand 81*b1cdbd2cSJim Jagielski 82*b1cdbd2cSJim Jagielski @precond IsOpen = true 83*b1cdbd2cSJim Jagielski 84*b1cdbd2cSJim Jagielski @throws 85*b1cdbd2cSJim Jagielski */ 86*b1cdbd2cSJim Jagielski virtual size_t GetSubValueCount() const = 0; 87*b1cdbd2cSJim Jagielski 88*b1cdbd2cSJim Jagielski /** The number of sub-keys of the key at hand 89*b1cdbd2cSJim Jagielski 90*b1cdbd2cSJim Jagielski @precond IsOpen = true 91*b1cdbd2cSJim Jagielski 92*b1cdbd2cSJim Jagielski @throws 93*b1cdbd2cSJim Jagielski */ 94*b1cdbd2cSJim Jagielski virtual size_t GetSubKeyCount() const = 0; 95*b1cdbd2cSJim Jagielski 96*b1cdbd2cSJim Jagielski bool IsOpen() const; 97*b1cdbd2cSJim Jagielski 98*b1cdbd2cSJim Jagielski /** Do we have write access on the key at hand 99*b1cdbd2cSJim Jagielski */ 100*b1cdbd2cSJim Jagielski bool IsWriteable() const; 101*b1cdbd2cSJim Jagielski 102*b1cdbd2cSJim Jagielski /** The StringList will be allocated on the heap, 103*b1cdbd2cSJim Jagielski so this is in fact a transfer of ownership 104*b1cdbd2cSJim Jagielski to the caller 105*b1cdbd2cSJim Jagielski 106*b1cdbd2cSJim Jagielski @precond IsOpen = true 107*b1cdbd2cSJim Jagielski 108*b1cdbd2cSJim Jagielski @throws RegistryIOException 109*b1cdbd2cSJim Jagielski */ 110*b1cdbd2cSJim Jagielski virtual StringListPtr GetSubKeyNames() const = 0; 111*b1cdbd2cSJim Jagielski 112*b1cdbd2cSJim Jagielski /** The StringList will be allocated on the heap, 113*b1cdbd2cSJim Jagielski so this is in fact a transfer of ownership 114*b1cdbd2cSJim Jagielski to the caller 115*b1cdbd2cSJim Jagielski 116*b1cdbd2cSJim Jagielski @precond IsOpen = true 117*b1cdbd2cSJim Jagielski 118*b1cdbd2cSJim Jagielski @throws RegistryIOException 119*b1cdbd2cSJim Jagielski */ 120*b1cdbd2cSJim Jagielski virtual StringListPtr GetSubValueNames() const = 0; 121*b1cdbd2cSJim Jagielski 122*b1cdbd2cSJim Jagielski /** Get the specified registry value 123*b1cdbd2cSJim Jagielski 124*b1cdbd2cSJim Jagielski @precond IsOpen = true 125*b1cdbd2cSJim Jagielski */ 126*b1cdbd2cSJim Jagielski virtual RegistryValue GetValue(const std::wstring& Name) const = 0; 127*b1cdbd2cSJim Jagielski 128*b1cdbd2cSJim Jagielski /** Get the specified registry value, return the given 129*b1cdbd2cSJim Jagielski default value if value not found 130*b1cdbd2cSJim Jagielski 131*b1cdbd2cSJim Jagielski @precond IsOpen = true 132*b1cdbd2cSJim Jagielski */ 133*b1cdbd2cSJim Jagielski virtual RegistryValue GetValue(const std::wstring& Name, const RegistryValue& Default) const = 0; 134*b1cdbd2cSJim Jagielski 135*b1cdbd2cSJim Jagielski /** Convenience function to determine if the 136*b1cdbd2cSJim Jagielski Registry key at hand has the specified 137*b1cdbd2cSJim Jagielski value 138*b1cdbd2cSJim Jagielski 139*b1cdbd2cSJim Jagielski @precond IsOpen = true 140*b1cdbd2cSJim Jagielski 141*b1cdbd2cSJim Jagielski throws RegistryAccessDenyException 142*b1cdbd2cSJim Jagielski */ 143*b1cdbd2cSJim Jagielski bool HasValue(const std::wstring& Name) const; 144*b1cdbd2cSJim Jagielski 145*b1cdbd2cSJim Jagielski /** Convenience function to determine if the 146*b1cdbd2cSJim Jagielski Registry key at hand has the specified 147*b1cdbd2cSJim Jagielski sub-key 148*b1cdbd2cSJim Jagielski 149*b1cdbd2cSJim Jagielski @precond IsOpen = true 150*b1cdbd2cSJim Jagielski 151*b1cdbd2cSJim Jagielski throws RegistryAccessDenyException 152*b1cdbd2cSJim Jagielski */ 153*b1cdbd2cSJim Jagielski bool HasSubKey(const std::wstring& Name) const; 154*b1cdbd2cSJim Jagielski 155*b1cdbd2cSJim Jagielski 156*b1cdbd2cSJim Jagielski //############################################ 157*b1cdbd2cSJim Jagielski // Commands 158*b1cdbd2cSJim Jagielski //############################################ 159*b1cdbd2cSJim Jagielski 160*b1cdbd2cSJim Jagielski 161*b1cdbd2cSJim Jagielski /** Open the registry key, has no effect if 162*b1cdbd2cSJim Jagielski the key is already open 163*b1cdbd2cSJim Jagielski 164*b1cdbd2cSJim Jagielski @precond IsOpen = false 165*b1cdbd2cSJim Jagielski 166*b1cdbd2cSJim Jagielski @throws RegistryWriteAccessDenyException 167*b1cdbd2cSJim Jagielski RegistryAccessDenyException 168*b1cdbd2cSJim Jagielski */ 169*b1cdbd2cSJim Jagielski virtual void Open(bool Writeable = true) = 0; 170*b1cdbd2cSJim Jagielski 171*b1cdbd2cSJim Jagielski /** Close the registry key at hand, further 172*b1cdbd2cSJim Jagielski using it without re-opening may cause 173*b1cdbd2cSJim Jagielski RegistryIOExceptions to be thrown 174*b1cdbd2cSJim Jagielski 175*b1cdbd2cSJim Jagielski This is a template method that calls 176*b1cdbd2cSJim Jagielski ImplClose which has to be overwritten 177*b1cdbd2cSJim Jagielski by sub-classes 178*b1cdbd2cSJim Jagielski */ 179*b1cdbd2cSJim Jagielski void Close(); 180*b1cdbd2cSJim Jagielski 181*b1cdbd2cSJim Jagielski /** Open the specified sub-key of the registry key 182*b1cdbd2cSJim Jagielski at hand 183*b1cdbd2cSJim Jagielski 184*b1cdbd2cSJim Jagielski @precond IsOpen = true 185*b1cdbd2cSJim Jagielski HasSubKey(Name) = true 186*b1cdbd2cSJim Jagielski 187*b1cdbd2cSJim Jagielski @throws RegistryIOException 188*b1cdbd2cSJim Jagielski RegistryKeyNotFoundException 189*b1cdbd2cSJim Jagielski RegistryAccessDeniedException 190*b1cdbd2cSJim Jagielski */ 191*b1cdbd2cSJim Jagielski virtual RegistryKey OpenSubKey(const std::wstring& Name, bool Writeable = true) = 0; 192*b1cdbd2cSJim Jagielski 193*b1cdbd2cSJim Jagielski /** Creates a new sub-key below the key at hand 194*b1cdbd2cSJim Jagielski 195*b1cdbd2cSJim Jagielski @precond IsOpen = true 196*b1cdbd2cSJim Jagielski IsWriteable = true 197*b1cdbd2cSJim Jagielski 198*b1cdbd2cSJim Jagielski @throws RegistryIOException 199*b1cdbd2cSJim Jagielski RegistryWriteAccessDenyException 200*b1cdbd2cSJim Jagielski */ 201*b1cdbd2cSJim Jagielski virtual RegistryKey CreateSubKey(const std::wstring& Name) = 0; 202*b1cdbd2cSJim Jagielski 203*b1cdbd2cSJim Jagielski /** Deletes a sub-key below the key at hand, the 204*b1cdbd2cSJim Jagielski key must not have sub-keys 205*b1cdbd2cSJim Jagielski 206*b1cdbd2cSJim Jagielski @precond IsOpen = true 207*b1cdbd2cSJim Jagielski IsWriteable = true 208*b1cdbd2cSJim Jagielski 209*b1cdbd2cSJim Jagielski @throws RegistryIOException 210*b1cdbd2cSJim Jagielski RegistryWriteAccessDenyException 211*b1cdbd2cSJim Jagielski */ 212*b1cdbd2cSJim Jagielski virtual void DeleteSubKey(const std::wstring& Name) = 0; 213*b1cdbd2cSJim Jagielski 214*b1cdbd2cSJim Jagielski /** Deletes a sub-key below the key at hand with all 215*b1cdbd2cSJim Jagielski its sub-keys 216*b1cdbd2cSJim Jagielski 217*b1cdbd2cSJim Jagielski @precond IsOpen = true 218*b1cdbd2cSJim Jagielski IsWriteable = true; 219*b1cdbd2cSJim Jagielski 220*b1cdbd2cSJim Jagielski @throws RegistryIOException 221*b1cdbd2cSJim Jagielski RegistryWriteAccessDenyException 222*b1cdbd2cSJim Jagielski */ 223*b1cdbd2cSJim Jagielski virtual void DeleteSubKeyTree(const std::wstring& Name) = 0; 224*b1cdbd2cSJim Jagielski 225*b1cdbd2cSJim Jagielski /** Delete the specified value 226*b1cdbd2cSJim Jagielski 227*b1cdbd2cSJim Jagielski @precond IsOpen = true 228*b1cdbd2cSJim Jagielski IsWriteable = true 229*b1cdbd2cSJim Jagielski HasValue(Name) = true 230*b1cdbd2cSJim Jagielski 231*b1cdbd2cSJim Jagielski @throws RegistryIOException 232*b1cdbd2cSJim Jagielski RegistryWriteAccessDeniedException 233*b1cdbd2cSJim Jagielski RegistryValueNotFoundException 234*b1cdbd2cSJim Jagielski */ 235*b1cdbd2cSJim Jagielski virtual void DeleteValue(const std::wstring& Name) = 0; 236*b1cdbd2cSJim Jagielski 237*b1cdbd2cSJim Jagielski /** Set the specified registry value 238*b1cdbd2cSJim Jagielski 239*b1cdbd2cSJim Jagielski @precond IsOpen = true 240*b1cdbd2cSJim Jagielski IsWriteable = true 241*b1cdbd2cSJim Jagielski 242*b1cdbd2cSJim Jagielski @throws RegistryIOException 243*b1cdbd2cSJim Jagielski RegistryWriteAccessDenyException 244*b1cdbd2cSJim Jagielski */ 245*b1cdbd2cSJim Jagielski virtual void SetValue(const RegistryValue& Value) = 0; 246*b1cdbd2cSJim Jagielski 247*b1cdbd2cSJim Jagielski 248*b1cdbd2cSJim Jagielski /** Copies the specified value from RegistryKey to 249*b1cdbd2cSJim Jagielski the registry key at hand, if a value with this 250*b1cdbd2cSJim Jagielski name already exist under the registry key at hand 251*b1cdbd2cSJim Jagielski it will be overwritten 252*b1cdbd2cSJim Jagielski 253*b1cdbd2cSJim Jagielski @precond IsOpen = true 254*b1cdbd2cSJim Jagielski IsWriteable = true 255*b1cdbd2cSJim Jagielski RegistryKey.HasSubValue(Name) = true 256*b1cdbd2cSJim Jagielski 257*b1cdbd2cSJim Jagielski @throws RegistryIOException 258*b1cdbd2cSJim Jagielski RegistryWriteAccessDeniedException 259*b1cdbd2cSJim Jagielski RegistryValueNotFoundException 260*b1cdbd2cSJim Jagielski */ 261*b1cdbd2cSJim Jagielski virtual void CopyValue(const RegistryKey& RegistryKey, const std::wstring& Name); 262*b1cdbd2cSJim Jagielski 263*b1cdbd2cSJim Jagielski /** Copies the specified value from RegistryKey to 264*b1cdbd2cSJim Jagielski the registry key at hand under a new name, 265*b1cdbd2cSJim Jagielski if a value with this name already exist there 266*b1cdbd2cSJim Jagielski it will be overwritten 267*b1cdbd2cSJim Jagielski 268*b1cdbd2cSJim Jagielski @precond IsOpen = true 269*b1cdbd2cSJim Jagielski IsWriteable = true 270*b1cdbd2cSJim Jagielski RegistryKey.HasSubValue(Name) = true 271*b1cdbd2cSJim Jagielski 272*b1cdbd2cSJim Jagielski @throws RegistryIOException 273*b1cdbd2cSJim Jagielski RegistryWriteAccessDeniedException 274*b1cdbd2cSJim Jagielski RegistryValueNotFoundException 275*b1cdbd2cSJim Jagielski */ 276*b1cdbd2cSJim Jagielski virtual void CopyValue(const RegistryKey& RegistryKey, const std::wstring& Name, const std::wstring& NewName); 277*b1cdbd2cSJim Jagielski 278*b1cdbd2cSJim Jagielski //############################################ 279*b1cdbd2cSJim Jagielski // Creation 280*b1cdbd2cSJim Jagielski // only possible through WindowsRegistry class 281*b1cdbd2cSJim Jagielski //############################################ 282*b1cdbd2cSJim Jagielski 283*b1cdbd2cSJim Jagielski 284*b1cdbd2cSJim Jagielski protected: 285*b1cdbd2cSJim Jagielski /** Create instance of the specified Registry key 286*b1cdbd2cSJim Jagielski 287*b1cdbd2cSJim Jagielski @throws RegistryWriteAccessDenyException 288*b1cdbd2cSJim Jagielski RegistryAccessDenyException 289*b1cdbd2cSJim Jagielski RegistryKeyNotFoundException 290*b1cdbd2cSJim Jagielski */ 291*b1cdbd2cSJim Jagielski RegistryKeyImpl(HKEY RootKey, const std::wstring& KeyName); 292*b1cdbd2cSJim Jagielski 293*b1cdbd2cSJim Jagielski /** Create instance of the specified Registry key. 294*b1cdbd2cSJim Jagielski RootKey should only one of the predefined 295*b1cdbd2cSJim Jagielski keys HKEY_CLASSES_ROOT, HKEY_CURRENT_USER, 296*b1cdbd2cSJim Jagielski HKEY_LOCAL_MACHINE, HKEY_USERS 297*b1cdbd2cSJim Jagielski 298*b1cdbd2cSJim Jagielski @throws RegistryWriteAccessDenyException 299*b1cdbd2cSJim Jagielski RegistryAccessDenyException 300*b1cdbd2cSJim Jagielski RegistryKeyNotFoundException 301*b1cdbd2cSJim Jagielski */ 302*b1cdbd2cSJim Jagielski RegistryKeyImpl(HKEY RootKey); 303*b1cdbd2cSJim Jagielski 304*b1cdbd2cSJim Jagielski /** Create an instances of the specified Registry key, 305*b1cdbd2cSJim Jagielski the key is assumed to be already opened. 306*b1cdbd2cSJim Jagielski */ 307*b1cdbd2cSJim Jagielski RegistryKeyImpl(HKEY RootKey, HKEY SubKey, const std::wstring& KeyName, bool Writeable = true); 308*b1cdbd2cSJim Jagielski 309*b1cdbd2cSJim Jagielski /** Is this one of the root keys 310*b1cdbd2cSJim Jagielski HKEY_CLASSES_ROOT 311*b1cdbd2cSJim Jagielski HKEY_CURRENT_USER 312*b1cdbd2cSJim Jagielski etc. 313*b1cdbd2cSJim Jagielski */ 314*b1cdbd2cSJim Jagielski bool IsRootKey() const; 315*b1cdbd2cSJim Jagielski 316*b1cdbd2cSJim Jagielski protected: 317*b1cdbd2cSJim Jagielski HKEY m_hRootKey; 318*b1cdbd2cSJim Jagielski HKEY m_hSubKey; 319*b1cdbd2cSJim Jagielski std::wstring m_KeyName; 320*b1cdbd2cSJim Jagielski bool m_IsWriteable; 321*b1cdbd2cSJim Jagielski 322*b1cdbd2cSJim Jagielski // prevent copy and assignment 323*b1cdbd2cSJim Jagielski private: 324*b1cdbd2cSJim Jagielski RegistryKeyImpl(const RegistryKeyImpl&); 325*b1cdbd2cSJim Jagielski RegistryKeyImpl& operator=(const RegistryKeyImpl&); 326*b1cdbd2cSJim Jagielski 327*b1cdbd2cSJim Jagielski //###################################### 328*b1cdbd2cSJim Jagielski // Friend declarations 329*b1cdbd2cSJim Jagielski //###################################### 330*b1cdbd2cSJim Jagielski 331*b1cdbd2cSJim Jagielski friend class WindowsRegistry; 332*b1cdbd2cSJim Jagielski }; 333*b1cdbd2cSJim Jagielski 334*b1cdbd2cSJim Jagielski #endif 335