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 /*
25     Windows shell extensions need to be approved in order to be used by the
26     Windows shell for clarification read the following section from the
27     Microsoft Developers Network Library (MSDN) see
28     http://msdn.microsoft.com/library/default.asp?url=/library/en-us/shellcc/platform/shell/programmersguide/shell_int/shell_int_extending/extensionhandlers/shell_ext.asp
29 
30 
31     <MSDN>
32     Shell extension handlers run in the Shell process. Because it is a system process,
33     the administrator of a Windows NT system can limit Shell extension handlers to
34     those on an approved list by setting the EnforceShellExtensionSecurity value of the
35     HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Policies\Explorer key to 1
36     (one).
37     To place a Shell extension handler on the approved list, create a REG_SZ value whose
38     name is the string form of the handler's GUID under
39     HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Shell Extensions\Approved.
40 
41     The Shell does not use the value that is assigned to the GUID, but it should be set to
42     make inspecting the registry easier.
43 
44     Your setup application can add values to the Approved key only if the person installing
45     the application has sufficient privileges. If the attempt to add an extension handler
46     fails, you should inform the user that administrative privileges are required to fully
47     install the application. If the handler is essential to the application, you should fail
48     the setup and notify the user to contact an administrator.
49 
50     While there is no need to add values to the Approved key on Windows 95 or Windows 98
51     systems, there is no harm in doing so. The system will simply ignore them. However, there
52     is no guarantee that the key will exist on these systems. Your setup program must be able
53     to handle this case.
54     </MSDN>
55 
56     We add the following entries to the respective registry key
57     "{C52AF81D-F7A0-4AAB-8E87-F80A60CCD396}"="OpenOffice Column Handler"
58     "{087B3AE3-E237-4467-B8DB-5A38AB959AC9}"="OpenOffice Infotip Handler"
59     "{63542C48-9552-494A-84F7-73AA6A7C99C1}"="OpenOffice Property Sheet Handler"
60     "{3B092F0C-7696-40E3-A80F-68D74DA84210}"="OpenOffice Thumbnail Viewer"
61 
62     These shell extensions are implemented in the 'shell' project. We ignore registration
63     failures because of insufficient privileges. The reason is: On systems which restrict the
64     use of shell extensions by applying the aforementioned policy probably only people with
65     sufficient privileges are allowed to install applications anyway. On systems where the
66     use of shell extensions is not restricted registration failures because of insufficient
67     prviliges have no negative effect because the shell extensions will work anyhow.
68 */
69 
70 #ifdef _MSC_VER
71 #pragma warning(push, 1) /* disable warnings within system headers */
72 #endif
73 #define WIN32_LEAN_AND_MEAN
74 #include <windows.h>
75 #include <msiquery.h>
76 #ifdef _MSC_VER
77 #pragma warning(pop)
78 #endif
79 
80 #include <malloc.h>
81 
82 #ifdef UNICODE
83 #define _UNICODE
84 #endif
85 #include <tchar.h>
86 
87 struct RegistryEntry
88 {
89     TCHAR* Key;
90     TCHAR* Value;
91 };
92 
93 RegistryEntry ColumnHandler = { TEXT("{C52AF81D-F7A0-4AAB-8E87-F80A60CCD396}"), TEXT("OpenOffice Column Handler") };
94 RegistryEntry InfotipHandler = { TEXT("{087B3AE3-E237-4467-B8DB-5A38AB959AC9}"), TEXT("OpenOffice Infotip Handler") };
95 RegistryEntry PropHandler = { TEXT("{63542C48-9552-494A-84F7-73AA6A7C99C1}"), TEXT("OpenOffice Property Sheet Handler") };
96 RegistryEntry ThumbViewer = { TEXT("{3B092F0C-7696-40E3-A80F-68D74DA84210}"), TEXT("OpenOffice Thumbnail Viewer") };
97 
GetMsiProp(MSIHANDLE hMSI,const char * pPropName,char ** ppValue)98 BOOL GetMsiProp( MSIHANDLE hMSI, const char* pPropName, char** ppValue )
99 {
100     DWORD sz = 0;
101    	if ( MsiGetProperty( hMSI, pPropName, 0, &sz ) == ERROR_MORE_DATA )
102    	{
103        	sz++;
104        	DWORD nbytes = sz * sizeof( char );
105        	char* buff = reinterpret_cast<char*>( malloc( nbytes ) );
106        	ZeroMemory( buff, nbytes );
107        	MsiGetProperty( hMSI, pPropName, buff, &sz );
108    		*ppValue = buff;
109 
110 		return TRUE;
111 	}
112 
113 	return FALSE;
114 }
115 
IsVersionNT64(MSIHANDLE hMSI)116 bool IsVersionNT64( MSIHANDLE hMSI )
117 {
118     char* pVal = NULL;
119 
120 	if ( GetMsiProp( hMSI, "VersionNT64", &pVal ) && pVal )
121 	{
122 		free( pVal );
123 		return true;
124 	}
125 
126 	return false;
127 }
128 
129 
130 
131 
132 /*
133     Called during installation when the module "Windows Explorer Extensions" is
134     selected.
135 */
InstallExecSequenceEntry(MSIHANDLE hMSI)136 extern "C" UINT __stdcall InstallExecSequenceEntry(MSIHANDLE hMSI)
137 {
138     //MessageBox(NULL, TEXT("InstallExecSequenceEntry"), TEXT("Pythonmsi"), MB_OK | MB_ICONINFORMATION);
139     HKEY hKey;
140 
141 
142 // 06.11.2009 tkr: to provide windows xp as build systems for mingw we need to define KEY_WOW64_64KEY
143 // in mingw 3.13 KEY_WOW64_64KEY isn't available < Win2003 systems.
144 // Also defined in setup_native\source\win32\customactions\reg64\reg64.cxx,source\win32\customactions\shellextensions\shellextensions.cxx and
145 // extensions\source\activex\main\so_activex.cpp
146 #ifndef KEY_WOW64_64KEY
147 	#define KEY_WOW64_64KEY	(0x0100)
148 #endif
149 
150 	if (IsVersionNT64(hMSI))
151 	{
152 		// Open Windows 64 Bit Registry
153 		if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, TEXT("Software\\Microsoft\\Windows\\CurrentVersion\\Shell Extensions\\Approved"),0, KEY_WRITE | KEY_WOW64_64KEY, &hKey) == ERROR_SUCCESS)
154 		{
155 			RegSetValueEx(hKey, ColumnHandler.Key, 0, REG_SZ, reinterpret_cast<const BYTE*>(ColumnHandler.Value), _tcslen(ColumnHandler.Value) + 1);
156 			RegSetValueEx(hKey, InfotipHandler.Key, 0, REG_SZ, reinterpret_cast<const BYTE*>(InfotipHandler.Value), _tcslen(InfotipHandler.Value) + 1);
157 			RegSetValueEx(hKey, PropHandler.Key, 0, REG_SZ, reinterpret_cast<const BYTE*>(PropHandler.Value), _tcslen(PropHandler.Value) + 1);
158 			RegSetValueEx(hKey, ThumbViewer.Key, 0, REG_SZ, reinterpret_cast<const BYTE*>(ThumbViewer.Value), _tcslen(ThumbViewer.Value) + 1);
159 
160 			RegCloseKey(hKey);
161 		}
162 
163 		// Open Windows 32 Bit Registry on Win64 maschine
164 
165 		if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, TEXT("Software\\Microsoft\\Windows\\CurrentVersion\\Shell Extensions\\Approved"),0, KEY_WRITE, &hKey ) == ERROR_SUCCESS)
166 		{
167 			RegSetValueEx(hKey, ColumnHandler.Key, 0, REG_SZ, reinterpret_cast<const BYTE*>(ColumnHandler.Value), _tcslen(ColumnHandler.Value) + 1);
168 			RegSetValueEx(hKey, InfotipHandler.Key, 0, REG_SZ, reinterpret_cast<const BYTE*>(InfotipHandler.Value), _tcslen(InfotipHandler.Value) + 1);
169 			RegSetValueEx(hKey, PropHandler.Key, 0, REG_SZ, reinterpret_cast<const BYTE*>(PropHandler.Value), _tcslen(PropHandler.Value) + 1);
170 			RegSetValueEx(hKey, ThumbViewer.Key, 0, REG_SZ, reinterpret_cast<const BYTE*>(ThumbViewer.Value), _tcslen(ThumbViewer.Value) + 1);
171 
172 			RegCloseKey(hKey);
173 		}
174 
175 
176 	} else
177 	{
178 		if (RegOpenKey(HKEY_LOCAL_MACHINE, TEXT("Software\\Microsoft\\Windows\\CurrentVersion\\Shell Extensions\\Approved"), &hKey) == ERROR_SUCCESS)
179 		{
180 			RegSetValueEx(hKey, ColumnHandler.Key, 0, REG_SZ, reinterpret_cast<const BYTE*>(ColumnHandler.Value), _tcslen(ColumnHandler.Value) + 1);
181 			RegSetValueEx(hKey, InfotipHandler.Key, 0, REG_SZ, reinterpret_cast<const BYTE*>(InfotipHandler.Value), _tcslen(InfotipHandler.Value) + 1);
182 			RegSetValueEx(hKey, PropHandler.Key, 0, REG_SZ, reinterpret_cast<const BYTE*>(PropHandler.Value), _tcslen(PropHandler.Value) + 1);
183 			RegSetValueEx(hKey, ThumbViewer.Key, 0, REG_SZ, reinterpret_cast<const BYTE*>(ThumbViewer.Value), _tcslen(ThumbViewer.Value) + 1);
184 
185 			RegCloseKey(hKey);
186 		}
187 	}
188     return ERROR_SUCCESS;
189 }
190 
191 /*
192     Called during deinstallation when the module "Windows Explorer Extensions" has
193     been installed.
194 */
DeinstallExecSequenceEntry(MSIHANDLE)195 extern "C" UINT __stdcall DeinstallExecSequenceEntry(MSIHANDLE)
196 {
197     //MessageBox(NULL, TEXT("DeinstallExecSequenceEntry"), TEXT("Pythonmsi"), MB_OK | MB_ICONINFORMATION);
198     HKEY hKey;
199     if (RegOpenKey(HKEY_LOCAL_MACHINE, TEXT("Software\\Microsoft\\Windows\\CurrentVersion\\Shell Extensions\\Approved"), &hKey) == ERROR_SUCCESS)
200     {
201         RegDeleteValue(hKey, ColumnHandler.Key);
202         RegDeleteValue(hKey, InfotipHandler.Key);
203         RegDeleteValue(hKey, PropHandler.Key);
204         RegDeleteValue(hKey, ThumbViewer.Key);
205 
206         RegCloseKey(hKey);
207     }
208     return ERROR_SUCCESS;
209 }
210