1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: reg4allmsi.cxx,v $
13 * This file is part of OpenOffice.org.
15 * OpenOffice.org is free software: you can redistribute it and/or modify
16 * it under the terms of the GNU Lesser General Public License version 3
17 * only, as published by the Free Software Foundation.
19 * OpenOffice.org is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU Lesser General Public License version 3 for more details
23 * (a copy is included in the LICENSE file that accompanied this code).
25 * You should have received a copy of the GNU Lesser General Public License
26 * version 3 along with OpenOffice.org. If not, see
27 * <http://www.openoffice.org/license.html>
28 * for a copy of the LGPLv3 License.
30 ************************************************************************/
33 #pragma warning(push, 1) /* disable warnings within system headers */
35 #define WIN32_LEAN_AND_MEAN
46 //----------------------------------------------------------
47 static const CHAR
* g_Extensions
[] =
49 ".doc", // Microsoft Word Text [0]
50 ".dot", // Microsoft Word Template
52 ".docx", // Office Word 2007 XML document
53 ".docm", // Office Word 2007 XML macro-enabled document
54 ".dotx", // Office Word 2007 XML template
55 ".dotm", // Office Word 2007 XML macro-enabled template
56 ".xlw", // Microsoft Excel
57 ".xls", // Microsoft Excel
58 ".xlt", // Microsoft Excel Template
59 ".xlsx", // Office Excel 2007 XML workbook
60 ".xlsm", // Office Excel 2007 XML macro-enabled workbook
61 ".xltx", // Office Excel 2007 XML template
62 ".xltm", // Office Excel 2007 XML macro-enabled template
63 ".xlsb", // Office Excel 2007 binary workbook (BIFF12)
64 ".ppt", // Microsoft Powerpoint
65 ".pps", // Microsoft Powerpoint
66 ".pot", // Microsoft Powerpoint Template
67 ".pptx", // Office PowerPoint 2007 XML presentation
68 ".pptm", // Office PowerPoint 2007 macro-enabled XML presentation
69 ".potx", // Office PowerPoint 2007 XML template
70 ".potm", // Office PowerPoint 2007 macro-enabled XML template
71 ".ppsx", // Office PowerPoint 2007 XML show
75 // ".xlam", // Office Excel 2007 XML macro-enabled add-in
76 // ".ppam", // Office PowerPoint 2007 macro-enabled XML add-in
77 // ".ppsm", // Office PowerPoint 2007 macro-enabled XML show
79 //----------------------------------------------------------
81 inline void OutputDebugStringFormat( LPCSTR pFormat
, ... )
86 va_start( args
, pFormat
);
87 StringCchVPrintfA( buffer
, sizeof(buffer
), pFormat
, args
);
88 OutputDebugStringA( buffer
);
91 static inline void OutputDebugStringFormat( LPCSTR
, ... )
96 //----------------------------------------------------------
97 static bool IsModuleSelectedForInstallation( MSIHANDLE handle
, LPCTSTR name
)
99 INSTALLSTATE current_state
;
100 INSTALLSTATE future_state
;
101 MsiGetFeatureState(handle
, name
, ¤t_state
, &future_state
);
102 return (future_state
== INSTALLSTATE_LOCAL
);
105 //----------------------------------------------------------
106 static BOOL
CheckExtensionInRegistry( LPCSTR lpSubKey
)
110 LONG lResult
= RegOpenKeyExA( HKEY_CLASSES_ROOT
, lpSubKey
, 0, KEY_QUERY_VALUE
, &hKey
);
112 if ( ERROR_SUCCESS
== lResult
)
115 DWORD nSize
= sizeof( szBuffer
);
117 lResult
= RegQueryValueExA( hKey
, "", NULL
, NULL
, (LPBYTE
)szBuffer
, &nSize
);
118 if ( ERROR_SUCCESS
== lResult
)
120 szBuffer
[nSize
] = '\0';
121 OutputDebugStringFormat( "Found value [%s] for key [%s].\n", szBuffer
, lpSubKey
);
123 if ( strncmp( szBuffer
, "WordPad.Document.1", 18 ) == 0 )
124 { // We will replace registration for word pad
127 if ( strncmp( szBuffer
, "OpenOffice.org.", 15 ) == 0 )
128 { // We will replace registration for our own types, too
131 if ( strncmp( szBuffer
, "ooostub.", 8 ) == 0 )
132 { // We will replace registration for ooostub, too
136 else // no default value found -> return TRUE to register for that key
141 else // no key found -> return TRUE to register for that key
147 //----------------------------------------------------------
148 static LONG
DeleteSubKeyTree( HKEY RootKey
, LPCSTR lpKey
)
151 LONG rc
= RegOpenKeyExA( RootKey
, lpKey
, 0, KEY_READ
| DELETE
, &hKey
);
153 if (ERROR_SUCCESS
== rc
)
158 rc
= RegQueryInfoKeyA( hKey
, 0, 0, 0, 0, &nMaxSubKeyLen
, 0, 0, 0, 0, 0, 0 );
159 nMaxSubKeyLen
++; // space for trailing '\0'
160 lpSubKey
= reinterpret_cast<CHAR
*>( _alloca( nMaxSubKeyLen
*sizeof(CHAR
) ) );
162 while (ERROR_SUCCESS
== rc
)
164 DWORD nLen
= nMaxSubKeyLen
;
165 rc
= RegEnumKeyExA( hKey
, 0, (LPSTR
)lpSubKey
, &nLen
, 0, 0, 0, 0); // always index zero
167 if ( ERROR_NO_MORE_ITEMS
== rc
)
169 rc
= RegDeleteKeyA( RootKey
, lpKey
);
170 if ( rc
== ERROR_SUCCESS
)
171 OutputDebugStringFormat( "deleted key [%s] from registry.\n", lpKey
);
173 OutputDebugStringFormat( "RegDeleteKeyA %s returned %ld.\n", lpKey
, rc
);
176 else if ( rc
== ERROR_SUCCESS
)
178 rc
= DeleteSubKeyTree( hKey
, lpSubKey
);
179 if ( ERROR_SUCCESS
!= rc
)
180 OutputDebugStringFormat( "RegDeleteKeyA %s returned %ld.\n", lpSubKey
, rc
);
188 OutputDebugStringFormat( "RegOpenKeyExA %s returned %ld.\n", lpKey
, rc
);
194 //----------------------------------------------------------
195 static BOOL
RemoveExtensionInRegistry( LPCSTR lpSubKey
)
198 DWORD nSize
= sizeof( szBuffer
);
201 LONG lResult
= RegOpenKeyExA( HKEY_LOCAL_MACHINE
, "SOFTWARE\\Classes", 0, KEY_QUERY_VALUE
, &hKey
);
203 if ( ERROR_SUCCESS
== lResult
)
205 lResult
= RegOpenKeyExA( hKey
, lpSubKey
, 0, KEY_QUERY_VALUE
, &hSubKey
);
207 if ( ERROR_SUCCESS
== lResult
)
212 // we get the value of the default key fist and while we are on querying,
213 // we ask for the subkey count, too
214 lResult
= RegQueryValueExA( hSubKey
, "", NULL
, NULL
, (LPBYTE
)szBuffer
, &nSize
);
215 if ( ERROR_SUCCESS
== lResult
)
216 RegQueryInfoKeyA( hSubKey
, 0, 0, 0, &nSubKeys
, 0, 0, 0, 0, 0, 0, 0 );
217 RegCloseKey( hSubKey
);
219 // we will remove all key with an default value starting with ooostub but
220 // we have to be careful about MSO keys
221 if ( strncmp( szBuffer
, "opendocument.", 13 ) == 0 )
225 DeleteSubKeyTree( hKey
, lpSubKey
);
229 lResult
= RegOpenKeyExA( hKey
, lpSubKey
, 0, KEY_SET_VALUE
, &hSubKey
);
230 if ( ERROR_SUCCESS
== lResult
)
231 RegDeleteValueA( hSubKey
, "" );
233 OutputDebugStringFormat( "Could not open key %s for deleting: RegOpenKeyEx returned %ld.\n", lpSubKey
, lResult
);
241 return ( ERROR_SUCCESS
== lResult
);
244 //----------------------------------------------------------
245 bool GetMsiProp( MSIHANDLE handle
, LPCSTR name
, /*out*/std::string
& value
)
249 if (MsiGetPropertyA(handle
, name
, dummy
, &sz
) == ERROR_MORE_DATA
)
252 DWORD nbytes
= sz
* sizeof(TCHAR
);
253 LPSTR buff
= reinterpret_cast<LPSTR
>(_alloca(nbytes
));
254 ZeroMemory(buff
, nbytes
);
255 MsiGetPropertyA(handle
, name
, buff
, &sz
);
262 //----------------------------------------------------------
263 bool IsSetMsiProp( MSIHANDLE handle
, LPCSTR name
)
266 GetMsiProp( handle
, name
, val
);
270 //----------------------------------------------------------
271 static void registerForExtensions( MSIHANDLE handle
, BOOL bRegisterAll
)
272 { // Check all file extensions
274 while ( g_Extensions
[nIndex
] != 0 )
276 BOOL bRegister
= bRegisterAll
|| CheckExtensionInRegistry( g_Extensions
[nIndex
] );
280 StringCchCopyA( sPropName
, 256, "REGISTER_" );
281 StringCchCatA( sPropName
, 256, (g_Extensions
[nIndex
])+1 );
282 CharUpperBuffA( sPropName
+9, 4 );
283 MsiSetPropertyA( handle
, sPropName
, "1" );
284 OutputDebugStringFormat( "Set MSI property %s.\n", sPropName
);
290 //----------------------------------------------------------
291 //----------------------------------------------------------
292 //----------------------------------------------------------
293 extern "C" UINT __stdcall
FindRegisteredExtensions( MSIHANDLE handle
)
295 bool bRegisterAll
= IsSetMsiProp( handle
, "REGISTER_ALL_MSO_TYPES" );
297 if ( IsSetMsiProp( handle
, "REGISTER_NO_MSO_TYPES" ) )
299 OutputDebugStringFormat( "FindRegisteredExtensions: Register none!" );
300 return ERROR_SUCCESS
;
302 else if ( bRegisterAll
)
303 OutputDebugStringFormat( "FindRegisteredExtensions: Force all on" );
305 OutputDebugStringFormat( "FindRegisteredExtensions: " );
307 registerForExtensions( handle
, bRegisterAll
);
309 return ERROR_SUCCESS
;
312 //----------------------------------------------------------
313 extern "C" UINT __stdcall
DeleteRegisteredExtensions( MSIHANDLE
/*handle*/ )
315 OutputDebugStringFormat( "DeleteRegisteredExtensions\n" );
317 // remove all file extensions
319 while ( g_Extensions
[nIndex
] != 0 )
321 RemoveExtensionInRegistry( g_Extensions
[nIndex
] );
325 return ERROR_SUCCESS
;