1 //
2 // RegistryTree.h: interface for the CRegistryTree class.
3 //
4 //////////////////////////////////////////////////////////////////////
5 
6 #if !defined(REGISTRYTREE_H__239A6461_70F2_11D3_9085_204C4F4F5020__INCLUDED_)
7 #define REGISTRYTREE_H__239A6461_70F2_11D3_9085_204C4F4F5020__INCLUDED_
8 
9 #include "RegistryKey.h"
10 
11 // Max size of error description.
12 #define ERROR_MSG_BUFFER_SIZE	1024
13 
14 class CRegistryTree
15 {
16 public:
17 	// Constructor
18   //
19 	// Parameters:
20 	//		nMaxPathSize - size in characters of longest path including terminating NULL char
21 	CRegistryTree();
22 
23   // Destructor
24 	virtual ~CRegistryTree();
25 
26   // Call this function after fail of this class method.
27   //
28   // Return value:
29   //   Pointer to buffer containing description of last error.
30   //   return value is valid until next method of this class is called.
31 	const TCHAR * GetLastErrorDescription();
32 
33   // Call this function to get string representation (path) of current key.
34   //
35   // Return value:
36   //   Pointer to buffer containing current key path. The pointer is valid until next call to this objet method.
37 	const TCHAR * GetCurrentPath() const;
38 
39   // Call this function to check if current key is the root key.
40   //
41   // Return value:
42   //   FALSE - current key is not the root key.
43   //   TRUE - current key is the root key.
44 	BOOL IsCurrentRoot();
45 
46   // Call this function to change the current key.
47   //
48   // Parameters:
49   //   pchRelativePath - relative path to target key.
50   //
51   // Return value:
52   //   TRUE - current key changed successfully.
53   //   FALSE - failed to change current key. Call GetLastErrorDescription() to get error description.
54 	BOOL ChangeCurrentKey(const TCHAR *pchRelativePath);
55 
56   // Call this function to obtain key at relative path and opened with desired access.
57   //
58   // Parametes:
59   //   pchRelativePath - path to key to be opened.
60   //   DesiredAccess - desired access to key.
61   //   rKey - reference to variable that receives pointer to key. Caller must free object with delete operator, when object is not longer needed.
62   //
63   // Return value:
64   //   TRUE - key opened successfully.
65   //   FALSE - failed to open desired key path size. Call GetLastErrorDescription() to get error description.
66   BOOL GetKey(const TCHAR *pchRelativePath, REGSAM DesiredAccess, CRegistryKey& rKey);
67 
68   // Call this function to delete key subkeys.
69   //
70   // Parameters:
71   //   pszKeyPattern - pattern to specifying which subkeys to delete.
72   //   pszPath - path to key which subkeys will be deleted.
73   //   blnRecursive - if FALSE and particular subkey has subkeys, it will not be deleted.
74   //
75   // Return value:
76   //   TRUE - key opened successfully.
77   //   FALSE - error. Call GetLastErrorDescription() to get error description.
78 	BOOL DeleteSubkeys(const TCHAR *pszKeyPattern, const TCHAR *pszPath, BOOL blnRecursive = FALSE);
79 
80 	BOOL NewKey(const TCHAR *pszKeyName, const TCHAR *pszPath, BOOL blnVolatile = FALSE);
81 
82 	BOOL SetMachineName(LPCTSTR pszMachineName);
83 
84 // Internal methods
85 private:
86 	CRegistryTree(const CRegistryTree& Tree);
87 
88   // returns description of error value returned by RegXXXX functions in advapi32.
89   const TCHAR *GetErrorDescription(LONG nError);
90 
91   void SetError(LONG nError);
92   void SetError(const TCHAR *pszFormat, ...);
93   void SetErrorCommandNAOnRoot(const TCHAR *pszCommand);
94   void SetInternalError();
95   void AddErrorDescription(const TCHAR *pszFormat, ...);
96 
97   BOOL InternalChangeCurrentKey(const TCHAR *pszSubkeyName, REGSAM DesiredAccess);
98   BOOL InternalGetSubkey(const TCHAR *pszSubkeyName, REGSAM DesiredAccess, CRegistryKey& rKey);
99   void GotoRoot();
100   BOOL DeleteSubkeys(CRegistryKey& rKey, const TCHAR *pszKeyPattern, BOOL blnRecursive);
101 
102 private:
103   class CNode
104   {
105   public:
106     CNode *m_pUp;
107     CRegistryKey m_Key;
108   } m_Root;
109 
110 	CNode *m_pCurrentKey;              // The current key.
111 	TCHAR m_ErrorMsg[ERROR_MSG_BUFFER_SIZE+1];   // Last error description buffer.
112 	LPTSTR m_pszMachineName;                     // Pointer to buffer containing machine name with leading backslashes. NULL if local.
113 };
114 
115 #endif // !defined(REGISTRYTREE_H__239A6461_70F2_11D3_9085_204C4F4F5020__INCLUDED_)
116