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