1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_shell.hxx"
26 
27 #if defined _MSC_VER
28 #pragma warning(push, 1)
29 #endif
30 #include <windows.h>
31 #if defined _MSC_VER
32 #pragma warning(pop)
33 #endif
34 #include <malloc.h>
35 #include "internal/dbgmacros.hxx"
36 #include "internal/registry.hxx"
37 
38 #if defined _MSC_VER
39 #pragma warning(push, 1)
40 #endif
41 #include <objbase.h>
42 #if defined _MSC_VER
43 #pragma warning(pop)
44 #endif
45 
46 //---------------------------------------
47 //
48 //---------------------------------------
49 
50 // Size of a CLSID as a string
51 const int CLSID_STRING_SIZE = 39;
52 
53 //---------------------------------------
54 //
55 //---------------------------------------
56 
SetRegistryKey(HKEY RootKey,const char * KeyName,const char * ValueName,const char * Value)57 bool SetRegistryKey(HKEY RootKey, const char* KeyName, const char* ValueName, const char* Value)
58 {
59 	HKEY hSubKey;
60 
61 	// open or create the desired key
62 	int rc = RegCreateKeyExA(
63 		RootKey, KeyName, 0, "", REG_OPTION_NON_VOLATILE, KEY_WRITE, 0, &hSubKey, 0);
64 
65 	if (ERROR_SUCCESS == rc)
66 	{
67 		rc = RegSetValueExA(
68 			hSubKey, ValueName, 0, REG_SZ, reinterpret_cast<const BYTE*>(Value), strlen(Value) + 1);
69 
70 		RegCloseKey(hSubKey);
71 	}
72 
73 	return (ERROR_SUCCESS == rc);
74 }
75 
76 //---------------------------------------
77 //
78 //---------------------------------------
79 
DeleteRegistryKey(HKEY RootKey,const char * KeyName)80 bool DeleteRegistryKey(HKEY RootKey, const char* KeyName)
81 {
82 	HKEY hKey;
83 
84 	int rc = RegOpenKeyExA(
85 		RootKey,
86 		KeyName,
87 		0,
88 		KEY_READ | DELETE,
89 		&hKey);
90 
91 	if ( rc == ERROR_FILE_NOT_FOUND )
92 		return true;
93 
94 	if (ERROR_SUCCESS == rc)
95 	{
96 		char* SubKey;
97 		DWORD nMaxSubKeyLen;
98 
99 		rc = RegQueryInfoKeyA(
100 			hKey, 0, 0, 0, 0,
101 			&nMaxSubKeyLen,
102 			0, 0, 0, 0, 0, 0);
103 
104 		nMaxSubKeyLen++; // space for trailing '\0'
105 
106 		SubKey = reinterpret_cast<char*>(
107 			_alloca(nMaxSubKeyLen*sizeof(char)));
108 
109 		while (ERROR_SUCCESS == rc)
110 		{
111 			DWORD nLen = nMaxSubKeyLen;
112 
113 			rc = RegEnumKeyExA(
114 				hKey,
115 				0, // always index zero
116 				SubKey,
117 				&nLen,
118 				0, 0, 0, 0);
119 
120 			if (ERROR_NO_MORE_ITEMS == rc)
121 			{
122 				rc = RegDeleteKeyA(RootKey, KeyName);
123 				break;
124 			}
125 			else if (rc == ERROR_SUCCESS)
126 			{
127 				DeleteRegistryKey(hKey, SubKey);
128 			}
129 
130 		} // while
131 
132 		RegCloseKey(hKey);
133 
134 	} // if
135 
136 	return (ERROR_SUCCESS == rc);
137 }
138 
139 /** May be used to determine if the specified registry key has subkeys
140 	The function returns true on success else if an error occurs false
141 */
HasSubkeysRegistryKey(HKEY RootKey,const char * KeyName,bool & bResult)142 bool HasSubkeysRegistryKey(HKEY RootKey, const char* KeyName, /* out */ bool& bResult)
143 {
144 	HKEY hKey;
145 
146 	LONG rc = RegOpenKeyExA(RootKey, KeyName, 0, KEY_READ, &hKey);
147 
148 	if (ERROR_SUCCESS == rc)
149 	{
150 		DWORD nSubKeys = 0;
151 
152 		rc = RegQueryInfoKeyA(hKey, 0, 0, 0, &nSubKeys, 0, 0, 0, 0, 0, 0, 0);
153 
154 		bResult = (nSubKeys > 0);
155 	}
156 
157 	return (ERROR_SUCCESS == rc);
158 }
159 
160 // Convert a CLSID to a char string.
ClsidToString(const CLSID & clsid)161 std::string ClsidToString(const CLSID& clsid)
162 {
163 	// Get CLSID
164 	LPOLESTR wszCLSID = NULL;
165 	StringFromCLSID(clsid, &wszCLSID);
166 
167 	char buff[39];
168 	// Convert from wide characters to non-wide.
169 	wcstombs(buff, wszCLSID, sizeof(buff));
170 
171 	// Free memory.
172 	CoTaskMemFree(wszCLSID) ;
173 
174 	return std::string(buff);
175 }
176 
177 //---------------------------------------
178 //
179 //---------------------------------------
180 
QueryRegistryKey(HKEY RootKey,const char * KeyName,const char * ValueName,char * pszData,DWORD dwBufLen)181 bool QueryRegistryKey(HKEY RootKey, const char* KeyName, const char* ValueName, char *pszData, DWORD dwBufLen)
182 {
183 	HKEY hKey;
184 
185 	int rc = RegOpenKeyExA(
186 		RootKey,
187 		KeyName,
188 		0,
189 		KEY_READ,
190 		&hKey);
191 
192 	if (ERROR_SUCCESS == rc)
193 	{
194 		rc = RegQueryValueExA(
195 			hKey, ValueName, NULL, NULL, (LPBYTE)pszData,&dwBufLen);
196 
197 		RegCloseKey(hKey);
198 	}
199 
200 	return (ERROR_SUCCESS == rc);
201 }
202