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