1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 #define UNICODE
29 
30 #ifdef _MSC_VER
31 #pragma warning(push,1) // disable warnings within system headers
32 #endif
33 #include <windows.h>
34 #include <msiquery.h>
35 #ifdef _MSC_VER
36 #pragma warning(pop)
37 #endif
38 
39 #include <string.h>
40 #include <malloc.h>
41 #include <stdio.h>
42 
43 //----------------------------------------------------------
44 BOOL UnicodeEquals( wchar_t* pStr1, wchar_t* pStr2 )
45 {
46 	if ( pStr1 == NULL && pStr2 == NULL )
47 		return TRUE;
48 	else if ( pStr1 == NULL || pStr2 == NULL )
49 		return FALSE;
50 
51 	while( *pStr1 == *pStr2 && *pStr1 && *pStr2 )
52 		pStr1++, pStr2++;
53 
54 	return ( *pStr1 == 0 && *pStr2 == 0 );
55 }
56 
57 //----------------------------------------------------------
58 BOOL GetMsiProp( MSIHANDLE hMSI, const wchar_t* pPropName, wchar_t** ppValue )
59 {
60     DWORD sz = 0;
61    	if ( MsiGetProperty( hMSI, pPropName, L"", &sz ) == ERROR_MORE_DATA )
62    	{
63        	sz++;
64        	DWORD nbytes = sz * sizeof( wchar_t );
65        	wchar_t* buff = reinterpret_cast<wchar_t*>( malloc( nbytes ) );
66        	ZeroMemory( buff, nbytes );
67        	MsiGetProperty( hMSI, pPropName, buff, &sz );
68    		*ppValue = buff;
69 
70 		return TRUE;
71 	}
72 
73 	return FALSE;
74 }
75 
76 //----------------------------------------------------------
77 BOOL MakeInstallForAllUsers( MSIHANDLE hMSI )
78 {
79 	BOOL bResult = FALSE;
80 	wchar_t* pVal = NULL;
81 	if ( GetMsiProp( hMSI, L"ALLUSERS", &pVal ) && pVal )
82 	{
83 		bResult = UnicodeEquals( pVal , L"1" );
84 		free( pVal );
85 	}
86 
87 	return bResult;
88 }
89 
90 //----------------------------------------------------------
91 extern "C" UINT __stdcall PatchActiveXControl( MSIHANDLE hMSI )
92 {
93 	// MessageBox(NULL, L"PatchActiveXControl", L"Information", MB_OK | MB_ICONINFORMATION);
94 
95 	INSTALLSTATE current_state;
96 	INSTALLSTATE future_state;
97 
98 	if ( ERROR_SUCCESS == MsiGetFeatureState( hMSI, L"gm_o_Activexcontrol", &current_state, &future_state ) )
99 	{
100 		BOOL bInstallForAllUsers = MakeInstallForAllUsers( hMSI );
101 
102 		if ( future_state == INSTALLSTATE_LOCAL
103 		  || ( current_state == INSTALLSTATE_LOCAL && future_state == INSTALLSTATE_UNKNOWN ) )
104 		{
105 			HKEY hkey = NULL;
106 			char* aSubKey = "Software\\Classes\\MIME\\DataBase\\Content Type\\application/vnd.sun.xml.base";
107 	   		if ( ERROR_SUCCESS == RegCreateKeyA(bInstallForAllUsers ? HKEY_LOCAL_MACHINE : HKEY_CURRENT_USER, aSubKey, &hkey) )
108 			{
109 	   			RegDeleteValueA( hkey, "CLSID" );
110 				RegCloseKey( hkey ), hkey = NULL;
111 			}
112 		}
113 	}
114 	else
115 	{
116 		// assert( FALSE );
117 	}
118 
119 	return ERROR_SUCCESS;
120 }
121 
122 
123