Update ooo320-m1
[ooovba.git] / setup_native / source / win32 / customactions / regactivex / regactivex.cxx
blob77971738fe71d5e6bc40edb9a07c757fef52c9d3
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: regactivex.cxx,v $
10 * $Revision: 1.18 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 #define UNICODE
33 #ifdef _MSC_VER
34 #pragma warning(push, 1) /* disable warnings within system headers */
35 #endif
36 #include <windows.h>
37 #include <msiquery.h>
38 #ifdef _MSC_VER
39 #pragma warning(pop)
40 #endif
42 #include <string.h>
43 #include <malloc.h>
45 #define CHART_COMPONENT 1
46 #define DRAW_COMPONENT 2
47 #define IMPRESS_COMPONENT 4
48 #define CALC_COMPONENT 8
49 #define WRITER_COMPONENT 16
50 #define MATH_COMPONENT 32
52 // #define OWN_DEBUG_PRINT
54 typedef int ( __stdcall * DllNativeRegProc ) ( int, BOOL, const char* );
55 typedef int ( __stdcall * DllNativeUnregProc ) ( int, BOOL );
57 BOOL UnicodeEquals( wchar_t* pStr1, wchar_t* pStr2 )
59 if ( pStr1 == NULL && pStr2 == NULL )
60 return TRUE;
61 else if ( pStr1 == NULL || pStr2 == NULL )
62 return FALSE;
64 while( *pStr1 == *pStr2 && *pStr1 && *pStr2 )
65 pStr1++, pStr2++;
67 return ( *pStr1 == 0 && *pStr2 == 0 );
70 //----------------------------------------------------------
71 char* UnicodeToAnsiString( wchar_t* pUniString )
73 int len = WideCharToMultiByte(
74 CP_ACP, 0, pUniString, -1, 0, 0, 0, 0 );
76 char* buff = reinterpret_cast<char*>( malloc( len ) );
78 WideCharToMultiByte(
79 CP_ACP, 0, pUniString, -1, buff, len, 0, 0 );
81 return buff;
84 #ifdef OWN_DEBUG_PRINT
85 void WarningMessageInt( wchar_t* pWarning, unsigned int nValue )
87 wchar_t pStr[5] = { nValue%10000/1000 + 48, nValue%1000/100 + 48, nValue%100/10 + 48, nValue%10 + 48, 0 };
88 MessageBox(NULL, pStr, pWarning, MB_OK | MB_ICONINFORMATION);
90 #endif
92 //----------------------------------------------------------
93 void RegisterActiveXNative( const char* pActiveXPath, int nMode, BOOL InstallForAllUser )
95 #ifdef OWN_DEBUG_PRINT
96 MessageBoxW(NULL, L"RegisterActiveXNative", L"Information", MB_OK | MB_ICONINFORMATION);
97 MessageBoxA(NULL, pActiveXPath, "Library Path", MB_OK | MB_ICONINFORMATION);
98 #endif
100 // For Win98/WinME the values should be written to the local machine
101 OSVERSIONINFO aVerInfo;
102 aVerInfo.dwOSVersionInfoSize = sizeof( aVerInfo );
103 if ( GetVersionEx( &aVerInfo ) && aVerInfo.dwPlatformId != VER_PLATFORM_WIN32_NT )
104 InstallForAllUser = TRUE;
106 HINSTANCE hModule = LoadLibraryExA( pActiveXPath, NULL, LOAD_WITH_ALTERED_SEARCH_PATH );
107 if( !( hModule <= ( HINSTANCE )HINSTANCE_ERROR ) )
109 DllNativeRegProc pNativeProc = ( DllNativeRegProc )GetProcAddress( hModule, "DllRegisterServerNative" );
110 if( pNativeProc!=NULL )
112 #ifdef OWN_DEBUG_PRINT
113 MessageBoxA(NULL, pActiveXPath, "Library Path", MB_OK | MB_ICONINFORMATION);
114 #endif
115 ( *pNativeProc )( nMode, InstallForAllUser, pActiveXPath );
118 FreeLibrary( hModule );
122 //----------------------------------------------------------
123 void UnregisterActiveXNative( const char* pActiveXPath, int nMode, BOOL InstallForAllUser )
125 // For Win98/WinME the values should be written to the local machine
126 OSVERSIONINFO aVerInfo;
127 aVerInfo.dwOSVersionInfoSize = sizeof( aVerInfo );
128 if ( GetVersionEx( &aVerInfo ) && aVerInfo.dwPlatformId != VER_PLATFORM_WIN32_NT )
129 InstallForAllUser = TRUE;
131 HINSTANCE hModule = LoadLibraryExA( pActiveXPath, NULL, LOAD_WITH_ALTERED_SEARCH_PATH );
132 if( !( hModule <= ( HINSTANCE )HINSTANCE_ERROR ) )
134 DllNativeUnregProc pNativeProc = ( DllNativeUnregProc )GetProcAddress( hModule, "DllUnregisterServerNative" );
135 if( pNativeProc!=NULL )
136 ( *pNativeProc )( nMode, InstallForAllUser );
138 FreeLibrary( hModule );
142 //----------------------------------------------------------
143 BOOL GetMsiProp( MSIHANDLE hMSI, const wchar_t* pPropName, wchar_t** ppValue )
145 DWORD sz = 0;
146 if ( MsiGetProperty( hMSI, pPropName, L"", &sz ) == ERROR_MORE_DATA )
148 sz++;
149 DWORD nbytes = sz * sizeof( wchar_t );
150 wchar_t* buff = reinterpret_cast<wchar_t*>( malloc( nbytes ) );
151 ZeroMemory( buff, nbytes );
152 MsiGetProperty( hMSI, pPropName, buff, &sz );
153 *ppValue = buff;
155 return TRUE;
158 return FALSE;
161 //----------------------------------------------------------
162 BOOL GetActiveXControlPath( MSIHANDLE hMSI, char** ppActiveXPath )
164 wchar_t* pProgPath = NULL;
165 if ( GetMsiProp( hMSI, L"BASISINSTALLLOCATION", &pProgPath ) && pProgPath )
167 char* pCharProgPath = UnicodeToAnsiString( pProgPath );
168 #ifdef OWN_DEBUG_PRINT
169 MessageBox(NULL, pProgPath, L"Basis Installation Path", MB_OK | MB_ICONINFORMATION);
170 MessageBoxA(NULL, pCharProgPath, "Basis Installation Path( char )", MB_OK | MB_ICONINFORMATION);
171 #endif
173 if ( pCharProgPath )
175 int nLen = strlen( pCharProgPath );
176 *ppActiveXPath = reinterpret_cast<char*>( malloc( nLen + 23 ) );
177 strncpy( *ppActiveXPath, pCharProgPath, nLen );
178 strncpy( (*ppActiveXPath) + nLen, "program\\so_activex.dll", 22 );
179 (*ppActiveXPath)[nLen+22] = 0;
181 free( pCharProgPath );
183 return TRUE;
186 free( pProgPath );
189 return FALSE;
192 //----------------------------------------------------------
193 BOOL GetDelta( MSIHANDLE hMSI, int& nOldInstallMode, int& nInstallMode, int& nDeinstallMode )
195 // for now the chart is always installed
196 nOldInstallMode = CHART_COMPONENT;
197 nInstallMode = CHART_COMPONENT;
198 nDeinstallMode = 0;
200 INSTALLSTATE current_state;
201 INSTALLSTATE future_state;
203 if ( ERROR_SUCCESS == MsiGetFeatureState( hMSI, L"gm_p_Wrt_Bin", &current_state, &future_state ) )
205 #ifdef OWN_DEBUG_PRINT
206 WarningMessageInt( L"writer current_state = ", current_state );
207 WarningMessageInt( L"writer future_state = ", future_state );
208 #endif
210 // analyze writer installation mode
211 if ( current_state == INSTALLSTATE_LOCAL )
212 nOldInstallMode |= WRITER_COMPONENT;
214 if ( future_state == INSTALLSTATE_LOCAL
215 || ( current_state == INSTALLSTATE_LOCAL && future_state == INSTALLSTATE_UNKNOWN ) )
216 nInstallMode |= WRITER_COMPONENT;
217 else if ( current_state == INSTALLSTATE_LOCAL && future_state == INSTALLSTATE_ABSENT )
218 nDeinstallMode |= WRITER_COMPONENT;
220 else
222 // assert( FALSE );
225 if ( ERROR_SUCCESS == MsiGetFeatureState( hMSI, L"gm_p_Calc_Bin", &current_state, &future_state ) )
227 #ifdef OWN_DEBUG_PRINT
228 WarningMessageInt( L"calc current_state = ", current_state );
229 WarningMessageInt( L"calc future_state = ", future_state );
230 #endif
232 // analyze calc installation mode
233 if ( current_state == INSTALLSTATE_LOCAL )
234 nOldInstallMode |= CALC_COMPONENT;
236 if ( future_state == INSTALLSTATE_LOCAL
237 || ( current_state == INSTALLSTATE_LOCAL && future_state == INSTALLSTATE_UNKNOWN ) )
238 nInstallMode |= CALC_COMPONENT;
239 else if ( current_state == INSTALLSTATE_LOCAL && future_state == INSTALLSTATE_ABSENT )
240 nDeinstallMode |= CALC_COMPONENT;
242 else
244 // assert( FALSE );
247 if ( ERROR_SUCCESS == MsiGetFeatureState( hMSI, L"gm_p_Draw_Bin", &current_state, &future_state ) )
249 // analyze draw installation mode
250 if ( current_state == INSTALLSTATE_LOCAL )
251 nOldInstallMode |= DRAW_COMPONENT;
253 if ( future_state == INSTALLSTATE_LOCAL
254 || ( current_state == INSTALLSTATE_LOCAL && future_state == INSTALLSTATE_UNKNOWN ) )
255 nInstallMode |= DRAW_COMPONENT;
256 else if ( current_state == INSTALLSTATE_LOCAL && future_state == INSTALLSTATE_ABSENT )
257 nDeinstallMode |= DRAW_COMPONENT;
259 else
261 // assert( FALSE );
264 if ( ERROR_SUCCESS == MsiGetFeatureState( hMSI, L"gm_p_Impress_Bin", &current_state, &future_state ) )
266 // analyze impress installation mode
267 if ( current_state == INSTALLSTATE_LOCAL )
268 nOldInstallMode |= IMPRESS_COMPONENT;
270 if ( future_state == INSTALLSTATE_LOCAL
271 || ( current_state == INSTALLSTATE_LOCAL && future_state == INSTALLSTATE_UNKNOWN ) )
272 nInstallMode |= IMPRESS_COMPONENT;
273 else if ( current_state == INSTALLSTATE_LOCAL && future_state == INSTALLSTATE_ABSENT )
274 nDeinstallMode |= IMPRESS_COMPONENT;
276 else
278 // assert( FALSE );
281 if ( ERROR_SUCCESS == MsiGetFeatureState( hMSI, L"gm_p_Math_Bin", &current_state, &future_state ) )
283 // analyze math installation mode
284 if ( current_state == INSTALLSTATE_LOCAL )
285 nOldInstallMode |= MATH_COMPONENT;
287 if ( future_state == INSTALLSTATE_LOCAL
288 || ( current_state == INSTALLSTATE_LOCAL && future_state == INSTALLSTATE_UNKNOWN ) )
289 nInstallMode |= MATH_COMPONENT;
290 else if ( current_state == INSTALLSTATE_LOCAL && future_state == INSTALLSTATE_ABSENT )
291 nDeinstallMode |= MATH_COMPONENT;
293 else
295 // assert( FALSE );
298 return TRUE;
301 //----------------------------------------------------------
302 BOOL MakeInstallForAllUsers( MSIHANDLE hMSI )
304 BOOL bResult = FALSE;
305 wchar_t* pVal = NULL;
306 if ( GetMsiProp( hMSI, L"ALLUSERS", &pVal ) && pVal )
308 bResult = UnicodeEquals( pVal , L"1" );
309 free( pVal );
312 return bResult;
315 //----------------------------------------------------------
316 extern "C" UINT __stdcall InstallActiveXControl( MSIHANDLE hMSI )
318 int nOldInstallMode = 0;
319 int nInstallMode = 0;
320 int nDeinstallMode = 0;
322 #ifdef OWN_DEBUG_PRINT
323 MessageBox(NULL, L"InstallActiveXControl", L"Information", MB_OK | MB_ICONINFORMATION);
324 #endif
326 INSTALLSTATE current_state;
327 INSTALLSTATE future_state;
329 if ( ERROR_SUCCESS == MsiGetFeatureState( hMSI, L"gm_o_Activexcontrol", &current_state, &future_state ) )
331 #ifdef OWN_DEBUG_PRINT
332 MessageBox(NULL, L"InstallActiveXControl Step2", L"Information", MB_OK | MB_ICONINFORMATION);
333 #endif
335 BOOL bInstallForAllUser = MakeInstallForAllUsers( hMSI );
336 char* pActiveXPath = NULL;
337 if ( GetActiveXControlPath( hMSI, &pActiveXPath ) && pActiveXPath
338 && GetDelta( hMSI, nOldInstallMode, nInstallMode, nDeinstallMode ) )
340 #ifdef OWN_DEBUG_PRINT
341 MessageBox(NULL, L"InstallActiveXControl Step3", L"Information", MB_OK | MB_ICONINFORMATION);
342 #endif
344 if ( future_state == INSTALLSTATE_LOCAL
345 || ( current_state == INSTALLSTATE_LOCAL && future_state == INSTALLSTATE_UNKNOWN ) )
347 #ifdef OWN_DEBUG_PRINT
348 MessageBox(NULL, L"InstallActiveXControl, adjusting", L"Information", MB_OK | MB_ICONINFORMATION);
349 WarningMessageInt( L"nInstallMode = ", nInstallMode );
350 #endif
351 // the control is installed in the new selected configuration
353 if ( current_state == INSTALLSTATE_LOCAL && nDeinstallMode )
354 UnregisterActiveXNative( pActiveXPath, nDeinstallMode, bInstallForAllUser );
356 if ( nInstallMode )
357 RegisterActiveXNative( pActiveXPath, nInstallMode, bInstallForAllUser );
359 else if ( current_state == INSTALLSTATE_LOCAL && future_state == INSTALLSTATE_ABSENT )
361 #ifdef OWN_DEBUG_PRINT
362 MessageBox(NULL, L"InstallActiveXControl, removing", L"Information", MB_OK | MB_ICONINFORMATION);
363 #endif
364 if ( nOldInstallMode )
365 UnregisterActiveXNative( pActiveXPath, nOldInstallMode, bInstallForAllUser );
369 if ( pActiveXPath )
370 free( pActiveXPath );
372 else
374 // assert( FALSE );
377 return ERROR_SUCCESS;
380 //----------------------------------------------------------
381 extern "C" UINT __stdcall DeinstallActiveXControl( MSIHANDLE hMSI )
383 INSTALLSTATE current_state;
384 INSTALLSTATE future_state;
386 #ifdef OWN_DEBUG_PRINT
387 MessageBox(NULL, L"DeinstallActiveXControl", L"Information", MB_OK | MB_ICONINFORMATION);
388 #endif
390 if ( ERROR_SUCCESS == MsiGetFeatureState( hMSI, L"gm_o_Activexcontrol", &current_state, &future_state ) )
392 char* pActiveXPath = NULL;
393 if ( current_state == INSTALLSTATE_LOCAL && GetActiveXControlPath( hMSI, &pActiveXPath ) && pActiveXPath )
395 BOOL bInstallForAllUser = MakeInstallForAllUsers( hMSI );
398 UnregisterActiveXNative( pActiveXPath,
399 CHART_COMPONENT
400 | DRAW_COMPONENT
401 | IMPRESS_COMPONENT
402 | CALC_COMPONENT
403 | WRITER_COMPONENT
404 | MATH_COMPONENT,
405 bInstallForAllUser );
408 free( pActiveXPath );
412 return ERROR_SUCCESS;