Version 3.6.0.4, tag libreoffice-3.6.0.4
[LibreOffice.git] / setup_native / source / win32 / customactions / regactivex / regactivex.cxx
blob7552e866ebac331b9091fe6fcb50f6d676b6052a
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*************************************************************************
4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * Copyright 2000, 2010 Oracle and/or its affiliates.
8 * OpenOffice.org - a multi-platform office productivity suite
10 * This file is part of OpenOffice.org.
12 * OpenOffice.org is free software: you can redistribute it and/or modify
13 * it under the terms of the GNU Lesser General Public License version 3
14 * only, as published by the Free Software Foundation.
16 * OpenOffice.org is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU Lesser General Public License version 3 for more details
20 * (a copy is included in the LICENSE file that accompanied this code).
22 * You should have received a copy of the GNU Lesser General Public License
23 * version 3 along with OpenOffice.org. If not, see
24 * <http://www.openoffice.org/license.html>
25 * for a copy of the LGPLv3 License.
27 ************************************************************************/
29 #define UNICODE
31 #ifdef _MSC_VER
32 #pragma warning(push, 1) /* disable warnings within system headers */
33 #endif
34 #include <windows.h>
35 #include <msiquery.h>
36 #ifdef _MSC_VER
37 #pragma warning(pop)
38 #endif
40 #include <string.h>
41 #include <malloc.h>
43 #define CHART_COMPONENT 1
44 #define DRAW_COMPONENT 2
45 #define IMPRESS_COMPONENT 4
46 #define CALC_COMPONENT 8
47 #define WRITER_COMPONENT 16
48 #define MATH_COMPONENT 32
50 typedef int ( __stdcall * DllNativeRegProc ) ( int, BOOL, BOOL, const char* );
51 typedef int ( __stdcall * DllNativeUnregProc ) ( int, BOOL, BOOL );
53 BOOL UnicodeEquals( wchar_t* pStr1, wchar_t* pStr2 )
55 if ( pStr1 == NULL && pStr2 == NULL )
56 return TRUE;
57 else if ( pStr1 == NULL || pStr2 == NULL )
58 return FALSE;
60 while( *pStr1 == *pStr2 && *pStr1 && *pStr2 )
61 pStr1++, pStr2++;
63 return ( *pStr1 == 0 && *pStr2 == 0 );
66 //----------------------------------------------------------
67 char* UnicodeToAnsiString( wchar_t* pUniString )
69 int len = WideCharToMultiByte(
70 CP_ACP, 0, pUniString, -1, 0, 0, 0, 0 );
72 char* buff = reinterpret_cast<char*>( malloc( len ) );
74 WideCharToMultiByte(
75 CP_ACP, 0, pUniString, -1, buff, len, 0, 0 );
77 return buff;
80 //----------------------------------------------------------
81 void RegisterActiveXNative( const char* pActiveXPath, int nMode, BOOL InstallForAllUser, BOOL InstallFor64Bit )
83 // For Win98/WinME the values should be written to the local machine
84 OSVERSIONINFO aVerInfo;
85 aVerInfo.dwOSVersionInfoSize = sizeof( aVerInfo );
86 if ( GetVersionEx( &aVerInfo ) && aVerInfo.dwPlatformId != VER_PLATFORM_WIN32_NT )
87 InstallForAllUser = TRUE;
89 HINSTANCE hModule = LoadLibraryExA( pActiveXPath, NULL, LOAD_WITH_ALTERED_SEARCH_PATH );
90 if( !( hModule <= ( HINSTANCE )HINSTANCE_ERROR ) )
92 DllNativeRegProc pNativeProc = ( DllNativeRegProc )GetProcAddress( hModule, "DllRegisterServerNative" );
93 if( pNativeProc!=NULL )
95 int nLen = strlen( pActiveXPath );
96 int nRemoveLen = strlen( "\\so_activex.dll" );
97 if ( nLen > nRemoveLen )
99 char* pProgramPath = reinterpret_cast<char*>( malloc( nLen - nRemoveLen + 1 ) );
100 strncpy( pProgramPath, pActiveXPath, nLen - nRemoveLen );
101 pProgramPath[ nLen - nRemoveLen ] = 0;
103 ( *pNativeProc )( nMode, InstallForAllUser, InstallFor64Bit, pProgramPath );
105 free( pProgramPath );
109 FreeLibrary( hModule );
113 //----------------------------------------------------------
114 void UnregisterActiveXNative( const char* pActiveXPath, int nMode, BOOL InstallForAllUser, BOOL InstallFor64Bit )
116 // For Win98/WinME the values should be written to the local machine
117 OSVERSIONINFO aVerInfo;
118 aVerInfo.dwOSVersionInfoSize = sizeof( aVerInfo );
119 if ( GetVersionEx( &aVerInfo ) && aVerInfo.dwPlatformId != VER_PLATFORM_WIN32_NT )
120 InstallForAllUser = TRUE;
122 HINSTANCE hModule = LoadLibraryExA( pActiveXPath, NULL, LOAD_WITH_ALTERED_SEARCH_PATH );
123 if( !( hModule <= ( HINSTANCE )HINSTANCE_ERROR ) )
125 DllNativeUnregProc pNativeProc = ( DllNativeUnregProc )GetProcAddress( hModule, "DllUnregisterServerNative" );
126 if( pNativeProc!=NULL )
127 ( *pNativeProc )( nMode, InstallForAllUser, InstallFor64Bit );
129 FreeLibrary( hModule );
133 //----------------------------------------------------------
134 BOOL GetMsiProp( MSIHANDLE hMSI, const wchar_t* pPropName, wchar_t** ppValue )
136 DWORD sz = 0;
137 if ( MsiGetProperty( hMSI, pPropName, L"", &sz ) == ERROR_MORE_DATA )
139 sz++;
140 DWORD nbytes = sz * sizeof( wchar_t );
141 wchar_t* buff = reinterpret_cast<wchar_t*>( malloc( nbytes ) );
142 ZeroMemory( buff, nbytes );
143 MsiGetProperty( hMSI, pPropName, buff, &sz );
144 *ppValue = buff;
146 return TRUE;
149 return FALSE;
152 //----------------------------------------------------------
153 BOOL GetActiveXControlPath( MSIHANDLE hMSI, char** ppActiveXPath )
155 wchar_t* pProgPath = NULL;
156 if ( GetMsiProp( hMSI, L"INSTALLLOCATION", &pProgPath ) && pProgPath )
158 char* pCharProgPath = UnicodeToAnsiString( pProgPath );
160 if ( pCharProgPath )
162 int nLen = strlen( pCharProgPath );
163 *ppActiveXPath = reinterpret_cast<char*>( malloc( nLen + 23 ) );
164 strncpy( *ppActiveXPath, pCharProgPath, nLen );
165 strncpy( (*ppActiveXPath) + nLen, "program\\so_activex.dll", 22 );
166 (*ppActiveXPath)[nLen+22] = 0;
168 free( pCharProgPath );
170 return TRUE;
173 free( pProgPath );
176 return FALSE;
179 //----------------------------------------------------------
180 BOOL GetDelta( MSIHANDLE hMSI, int& nOldInstallMode, int& nInstallMode, int& nDeinstallMode )
182 // for now the chart is always installed
183 nOldInstallMode = CHART_COMPONENT;
184 nInstallMode = CHART_COMPONENT;
185 nDeinstallMode = 0;
187 INSTALLSTATE current_state;
188 INSTALLSTATE future_state;
190 if ( ERROR_SUCCESS == MsiGetFeatureState( hMSI, L"gm_p_Wrt_Bin", &current_state, &future_state ) )
192 // analyze writer installation mode
193 if ( current_state == INSTALLSTATE_LOCAL )
194 nOldInstallMode |= WRITER_COMPONENT;
196 if ( future_state == INSTALLSTATE_LOCAL
197 || ( current_state == INSTALLSTATE_LOCAL && future_state == INSTALLSTATE_UNKNOWN ) )
198 nInstallMode |= WRITER_COMPONENT;
199 else if ( current_state == INSTALLSTATE_LOCAL && future_state == INSTALLSTATE_ABSENT )
200 nDeinstallMode |= WRITER_COMPONENT;
202 else
204 // assert( FALSE );
207 if ( ERROR_SUCCESS == MsiGetFeatureState( hMSI, L"gm_p_Calc_Bin", &current_state, &future_state ) )
209 // analyze calc installation mode
210 if ( current_state == INSTALLSTATE_LOCAL )
211 nOldInstallMode |= CALC_COMPONENT;
213 if ( future_state == INSTALLSTATE_LOCAL
214 || ( current_state == INSTALLSTATE_LOCAL && future_state == INSTALLSTATE_UNKNOWN ) )
215 nInstallMode |= CALC_COMPONENT;
216 else if ( current_state == INSTALLSTATE_LOCAL && future_state == INSTALLSTATE_ABSENT )
217 nDeinstallMode |= CALC_COMPONENT;
219 else
221 // assert( FALSE );
224 if ( ERROR_SUCCESS == MsiGetFeatureState( hMSI, L"gm_p_Draw_Bin", &current_state, &future_state ) )
226 // analyze draw installation mode
227 if ( current_state == INSTALLSTATE_LOCAL )
228 nOldInstallMode |= DRAW_COMPONENT;
230 if ( future_state == INSTALLSTATE_LOCAL
231 || ( current_state == INSTALLSTATE_LOCAL && future_state == INSTALLSTATE_UNKNOWN ) )
232 nInstallMode |= DRAW_COMPONENT;
233 else if ( current_state == INSTALLSTATE_LOCAL && future_state == INSTALLSTATE_ABSENT )
234 nDeinstallMode |= DRAW_COMPONENT;
236 else
238 // assert( FALSE );
241 if ( ERROR_SUCCESS == MsiGetFeatureState( hMSI, L"gm_p_Impress_Bin", &current_state, &future_state ) )
243 // analyze impress installation mode
244 if ( current_state == INSTALLSTATE_LOCAL )
245 nOldInstallMode |= IMPRESS_COMPONENT;
247 if ( future_state == INSTALLSTATE_LOCAL
248 || ( current_state == INSTALLSTATE_LOCAL && future_state == INSTALLSTATE_UNKNOWN ) )
249 nInstallMode |= IMPRESS_COMPONENT;
250 else if ( current_state == INSTALLSTATE_LOCAL && future_state == INSTALLSTATE_ABSENT )
251 nDeinstallMode |= IMPRESS_COMPONENT;
253 else
255 // assert( FALSE );
258 if ( ERROR_SUCCESS == MsiGetFeatureState( hMSI, L"gm_p_Math_Bin", &current_state, &future_state ) )
260 // analyze math installation mode
261 if ( current_state == INSTALLSTATE_LOCAL )
262 nOldInstallMode |= MATH_COMPONENT;
264 if ( future_state == INSTALLSTATE_LOCAL
265 || ( current_state == INSTALLSTATE_LOCAL && future_state == INSTALLSTATE_UNKNOWN ) )
266 nInstallMode |= MATH_COMPONENT;
267 else if ( current_state == INSTALLSTATE_LOCAL && future_state == INSTALLSTATE_ABSENT )
268 nDeinstallMode |= MATH_COMPONENT;
270 else
272 // assert( FALSE );
275 return TRUE;
278 //----------------------------------------------------------
279 BOOL MakeInstallForAllUsers( MSIHANDLE hMSI )
281 BOOL bResult = FALSE;
282 wchar_t* pVal = NULL;
283 if ( GetMsiProp( hMSI, L"ALLUSERS", &pVal ) && pVal )
285 bResult = UnicodeEquals( pVal , L"1" );
286 free( pVal );
289 return bResult;
292 //----------------------------------------------------------
293 BOOL MakeInstallFor64Bit( MSIHANDLE hMSI )
295 BOOL bResult = FALSE;
296 wchar_t* pVal = NULL;
297 if ( GetMsiProp( hMSI, L"VersionNT64", &pVal ) && pVal )
299 bResult = TRUE;
300 free( pVal );
303 return bResult;
305 //----------------------------------------------------------
306 extern "C" UINT __stdcall InstallActiveXControl( MSIHANDLE hMSI )
308 INSTALLSTATE current_state;
309 INSTALLSTATE future_state;
311 if ( ERROR_SUCCESS == MsiGetFeatureState( hMSI, L"gm_o_Activexcontrol", &current_state, &future_state ) )
313 int nOldInstallMode = 0;
314 int nInstallMode = 0;
315 int nDeinstallMode = 0;
316 BOOL bInstallForAllUser = MakeInstallForAllUsers( hMSI );
317 BOOL bInstallFor64Bit = MakeInstallFor64Bit( hMSI );
319 char* pActiveXPath = NULL;
320 if ( GetActiveXControlPath( hMSI, &pActiveXPath ) && pActiveXPath
321 && GetDelta( hMSI, nOldInstallMode, nInstallMode, nDeinstallMode ) )
323 if ( future_state == INSTALLSTATE_LOCAL
324 || ( current_state == INSTALLSTATE_LOCAL && future_state == INSTALLSTATE_UNKNOWN ) )
326 // the control is installed in the new selected configuration
328 if ( current_state == INSTALLSTATE_LOCAL && nDeinstallMode )
329 UnregisterActiveXNative( pActiveXPath, nDeinstallMode, bInstallForAllUser, bInstallFor64Bit );
331 if ( nInstallMode )
332 RegisterActiveXNative( pActiveXPath, nInstallMode, bInstallForAllUser, bInstallFor64Bit );
334 else if ( current_state == INSTALLSTATE_LOCAL && future_state == INSTALLSTATE_ABSENT )
336 if ( nOldInstallMode )
337 UnregisterActiveXNative( pActiveXPath, nOldInstallMode, bInstallForAllUser, bInstallFor64Bit );
341 if ( pActiveXPath )
342 free( pActiveXPath );
344 else
346 // assert( FALSE );
349 return ERROR_SUCCESS;
352 //----------------------------------------------------------
353 extern "C" UINT __stdcall DeinstallActiveXControl( MSIHANDLE hMSI )
355 INSTALLSTATE current_state;
356 INSTALLSTATE future_state;
358 if ( ERROR_SUCCESS == MsiGetFeatureState( hMSI, L"gm_o_Activexcontrol", &current_state, &future_state ) )
360 char* pActiveXPath = NULL;
361 if ( current_state == INSTALLSTATE_LOCAL && GetActiveXControlPath( hMSI, &pActiveXPath ) && pActiveXPath )
363 BOOL bInstallForAllUser = MakeInstallForAllUsers( hMSI );
364 BOOL bInstallFor64Bit = MakeInstallFor64Bit( hMSI );
367 UnregisterActiveXNative( pActiveXPath,
368 CHART_COMPONENT
369 | DRAW_COMPONENT
370 | IMPRESS_COMPONENT
371 | CALC_COMPONENT
372 | WRITER_COMPONENT
373 | MATH_COMPONENT,
374 bInstallForAllUser,
375 bInstallFor64Bit );
378 free( pActiveXPath );
382 return ERROR_SUCCESS;
385 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */