1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir 
28*cdf0e10cSrcweir #define _WIN32_WINDOWS 0x0410
29*cdf0e10cSrcweir 
30*cdf0e10cSrcweir #ifdef _MSC_VER
31*cdf0e10cSrcweir #pragma warning(push, 1) /* disable warnings within system headers */
32*cdf0e10cSrcweir #endif
33*cdf0e10cSrcweir #define WIN32_LEAN_AND_MEAN
34*cdf0e10cSrcweir #include <windows.h>
35*cdf0e10cSrcweir #include <msiquery.h>
36*cdf0e10cSrcweir #ifdef _MSC_VER
37*cdf0e10cSrcweir #pragma warning(pop)
38*cdf0e10cSrcweir #endif
39*cdf0e10cSrcweir 
40*cdf0e10cSrcweir #include <malloc.h>
41*cdf0e10cSrcweir #include <assert.h>
42*cdf0e10cSrcweir 
43*cdf0e10cSrcweir #ifdef UNICODE
44*cdf0e10cSrcweir #define _UNICODE
45*cdf0e10cSrcweir #define _tstring	wstring
46*cdf0e10cSrcweir #else
47*cdf0e10cSrcweir #define _tstring	string
48*cdf0e10cSrcweir #endif
49*cdf0e10cSrcweir #include <tchar.h>
50*cdf0e10cSrcweir #include <string>
51*cdf0e10cSrcweir #include <queue>
52*cdf0e10cSrcweir #include <stdio.h>
53*cdf0e10cSrcweir 
54*cdf0e10cSrcweir #include <systools/win32/uwinapi.h>
55*cdf0e10cSrcweir #include <../tools/seterror.hxx>
56*cdf0e10cSrcweir 
57*cdf0e10cSrcweir #define	WININIT_FILENAME	"wininit.ini"
58*cdf0e10cSrcweir #define RENAME_SECTION		"rename"
59*cdf0e10cSrcweir 
60*cdf0e10cSrcweir #ifdef DEBUG
61*cdf0e10cSrcweir inline void OutputDebugStringFormat( LPCTSTR pFormat, ... )
62*cdf0e10cSrcweir {
63*cdf0e10cSrcweir 	_TCHAR	buffer[1024];
64*cdf0e10cSrcweir 	va_list	args;
65*cdf0e10cSrcweir 
66*cdf0e10cSrcweir 	va_start( args, pFormat );
67*cdf0e10cSrcweir 	_vsntprintf( buffer, elementsof(buffer), pFormat, args );
68*cdf0e10cSrcweir 	OutputDebugString( buffer );
69*cdf0e10cSrcweir }
70*cdf0e10cSrcweir #else
71*cdf0e10cSrcweir static inline void OutputDebugStringFormat( LPCTSTR, ... )
72*cdf0e10cSrcweir {
73*cdf0e10cSrcweir }
74*cdf0e10cSrcweir #endif
75*cdf0e10cSrcweir 
76*cdf0e10cSrcweir static std::_tstring GetMsiProperty( MSIHANDLE handle, const std::_tstring& sProperty )
77*cdf0e10cSrcweir {
78*cdf0e10cSrcweir 	std::_tstring	result;
79*cdf0e10cSrcweir 	TCHAR	szDummy[1] = TEXT("");
80*cdf0e10cSrcweir 	DWORD	nChars = 0;
81*cdf0e10cSrcweir 
82*cdf0e10cSrcweir 	if ( MsiGetProperty( handle, sProperty.c_str(), szDummy, &nChars ) == ERROR_MORE_DATA )
83*cdf0e10cSrcweir 	{
84*cdf0e10cSrcweir 		DWORD nBytes = ++nChars * sizeof(TCHAR);
85*cdf0e10cSrcweir 		LPTSTR buffer = reinterpret_cast<LPTSTR>(_alloca(nBytes));
86*cdf0e10cSrcweir 		ZeroMemory( buffer, nBytes );
87*cdf0e10cSrcweir 		MsiGetProperty(handle, sProperty.c_str(), buffer, &nChars);
88*cdf0e10cSrcweir 		result = buffer;
89*cdf0e10cSrcweir 	}
90*cdf0e10cSrcweir 
91*cdf0e10cSrcweir 	return	result;
92*cdf0e10cSrcweir }
93*cdf0e10cSrcweir 
94*cdf0e10cSrcweir static inline bool IsSetMsiProperty(MSIHANDLE handle, const std::_tstring& sProperty)
95*cdf0e10cSrcweir {
96*cdf0e10cSrcweir 	std::_tstring value = GetMsiProperty(handle, sProperty);
97*cdf0e10cSrcweir 	return (value.length() > 0);
98*cdf0e10cSrcweir }
99*cdf0e10cSrcweir 
100*cdf0e10cSrcweir static inline void UnsetMsiProperty(MSIHANDLE handle, const std::_tstring& sProperty)
101*cdf0e10cSrcweir {
102*cdf0e10cSrcweir 	MsiSetProperty(handle, sProperty.c_str(), NULL);
103*cdf0e10cSrcweir }
104*cdf0e10cSrcweir 
105*cdf0e10cSrcweir static inline void SetMsiProperty(MSIHANDLE handle, const std::_tstring& sProperty)
106*cdf0e10cSrcweir {
107*cdf0e10cSrcweir 	MsiSetProperty(handle, sProperty.c_str(), TEXT("1"));
108*cdf0e10cSrcweir }
109*cdf0e10cSrcweir 
110*cdf0e10cSrcweir static BOOL MoveFileEx9x( LPCSTR lpExistingFileNameA, LPCSTR lpNewFileNameA, DWORD dwFlags )
111*cdf0e10cSrcweir {
112*cdf0e10cSrcweir 	BOOL	fSuccess = FALSE;	// assume failure
113*cdf0e10cSrcweir 
114*cdf0e10cSrcweir 	// Windows 9x has a special mechanism to move files after reboot
115*cdf0e10cSrcweir 
116*cdf0e10cSrcweir 	if ( dwFlags & MOVEFILE_DELAY_UNTIL_REBOOT )
117*cdf0e10cSrcweir 	{
118*cdf0e10cSrcweir 		CHAR	szExistingFileNameA[MAX_PATH];
119*cdf0e10cSrcweir 		CHAR	szNewFileNameA[MAX_PATH] = "NUL";
120*cdf0e10cSrcweir 
121*cdf0e10cSrcweir 		// Path names in WININIT.INI must be in short path name form
122*cdf0e10cSrcweir 
123*cdf0e10cSrcweir 		if (
124*cdf0e10cSrcweir 			GetShortPathNameA( lpExistingFileNameA, szExistingFileNameA, MAX_PATH ) &&
125*cdf0e10cSrcweir 			(!lpNewFileNameA || GetShortPathNameA( lpNewFileNameA, szNewFileNameA, MAX_PATH ))
126*cdf0e10cSrcweir 			)
127*cdf0e10cSrcweir 		{
128*cdf0e10cSrcweir 			CHAR	szBuffer[32767];	// The buffer size must not exceed 32K
129*cdf0e10cSrcweir 			DWORD	dwBufLen = GetPrivateProfileSectionA( RENAME_SECTION, szBuffer, elementsof(szBuffer), WININIT_FILENAME );
130*cdf0e10cSrcweir 
131*cdf0e10cSrcweir 			CHAR	szRename[MAX_PATH];	// This is enough for at most to times 67 chracters
132*cdf0e10cSrcweir 			strcpy( szRename, szNewFileNameA );
133*cdf0e10cSrcweir 			strcat( szRename, "=" );
134*cdf0e10cSrcweir 			strcat( szRename, szExistingFileNameA );
135*cdf0e10cSrcweir 			size_t	lnRename = strlen(szRename);
136*cdf0e10cSrcweir 
137*cdf0e10cSrcweir 			if ( dwBufLen + lnRename + 2 <= elementsof(szBuffer) )
138*cdf0e10cSrcweir 			{
139*cdf0e10cSrcweir 				CopyMemory( &szBuffer[dwBufLen], szRename, lnRename );
140*cdf0e10cSrcweir 				szBuffer[dwBufLen + lnRename ] = 0;
141*cdf0e10cSrcweir 				szBuffer[dwBufLen + lnRename + 1 ] = 0;
142*cdf0e10cSrcweir 
143*cdf0e10cSrcweir 				fSuccess = WritePrivateProfileSectionA( RENAME_SECTION, szBuffer, WININIT_FILENAME );
144*cdf0e10cSrcweir 			}
145*cdf0e10cSrcweir 			else
146*cdf0e10cSrcweir 				SetLastError( ERROR_BUFFER_OVERFLOW );
147*cdf0e10cSrcweir 		}
148*cdf0e10cSrcweir 	}
149*cdf0e10cSrcweir 	else
150*cdf0e10cSrcweir 	{
151*cdf0e10cSrcweir 
152*cdf0e10cSrcweir 		fSuccess = MoveFileA( lpExistingFileNameA, lpNewFileNameA );
153*cdf0e10cSrcweir 
154*cdf0e10cSrcweir 		if ( !fSuccess && GetLastError() != ERROR_ACCESS_DENIED &&
155*cdf0e10cSrcweir 			0 != (dwFlags & (MOVEFILE_COPY_ALLOWED | MOVEFILE_REPLACE_EXISTING)) )
156*cdf0e10cSrcweir 		{
157*cdf0e10cSrcweir 			BOOL	bFailIfExist = 0 == (dwFlags & MOVEFILE_REPLACE_EXISTING);
158*cdf0e10cSrcweir 
159*cdf0e10cSrcweir 			fSuccess = CopyFileA( lpExistingFileNameA, lpNewFileNameA, bFailIfExist );
160*cdf0e10cSrcweir 
161*cdf0e10cSrcweir 			if ( fSuccess )
162*cdf0e10cSrcweir 				fSuccess = DeleteFileA( lpExistingFileNameA );
163*cdf0e10cSrcweir 		}
164*cdf0e10cSrcweir 
165*cdf0e10cSrcweir 	}
166*cdf0e10cSrcweir 
167*cdf0e10cSrcweir 	return fSuccess;
168*cdf0e10cSrcweir }
169*cdf0e10cSrcweir 
170*cdf0e10cSrcweir static BOOL MoveFileExImpl( LPCSTR lpExistingFileNameA, LPCSTR lpNewFileNameA, DWORD dwFlags )
171*cdf0e10cSrcweir {
172*cdf0e10cSrcweir 	if ( 0 > ((LONG)GetVersion())) // High order bit indicates Win 9x
173*cdf0e10cSrcweir 		return MoveFileEx9x( lpExistingFileNameA, lpNewFileNameA, dwFlags );
174*cdf0e10cSrcweir 	else
175*cdf0e10cSrcweir 		return MoveFileExA( lpExistingFileNameA, lpNewFileNameA, dwFlags );
176*cdf0e10cSrcweir }
177*cdf0e10cSrcweir 
178*cdf0e10cSrcweir extern "C" UINT __stdcall IsOfficeRunning( MSIHANDLE handle )
179*cdf0e10cSrcweir {
180*cdf0e10cSrcweir 	std::_tstring	sInstDir = GetMsiProperty( handle, TEXT("INSTALLLOCATION") );
181*cdf0e10cSrcweir 	std::_tstring	sResourceDir = sInstDir + TEXT("Basis\\program\\resource\\");
182*cdf0e10cSrcweir 	std::_tstring	sPattern = sResourceDir + TEXT("vcl*.res");
183*cdf0e10cSrcweir 
184*cdf0e10cSrcweir 	WIN32_FIND_DATA	aFindFileData;
185*cdf0e10cSrcweir 	HANDLE	hFind = FindFirstFile( sPattern.c_str(), &aFindFileData );
186*cdf0e10cSrcweir 
187*cdf0e10cSrcweir 	if ( IsValidHandle(hFind) )
188*cdf0e10cSrcweir 	{
189*cdf0e10cSrcweir 		BOOL	fSuccess = false;
190*cdf0e10cSrcweir 		bool	fRenameSucceeded;
191*cdf0e10cSrcweir 
192*cdf0e10cSrcweir 		do
193*cdf0e10cSrcweir 		{
194*cdf0e10cSrcweir 			std::_tstring	sResourceFile = sResourceDir + aFindFileData.cFileName;
195*cdf0e10cSrcweir 			std::_tstring	sIntermediate = sResourceFile + TEXT(".tmp");
196*cdf0e10cSrcweir 
197*cdf0e10cSrcweir 			fRenameSucceeded = MoveFileExImpl( sResourceFile.c_str(), sIntermediate.c_str(), MOVEFILE_REPLACE_EXISTING );
198*cdf0e10cSrcweir 			if ( fRenameSucceeded )
199*cdf0e10cSrcweir 			{
200*cdf0e10cSrcweir 				MoveFileExImpl( sIntermediate.c_str(), sResourceFile.c_str(), 0 );
201*cdf0e10cSrcweir 				fSuccess = FindNextFile( hFind, &aFindFileData );
202*cdf0e10cSrcweir 			}
203*cdf0e10cSrcweir 		} while ( fSuccess && fRenameSucceeded );
204*cdf0e10cSrcweir 
205*cdf0e10cSrcweir 		if ( !fRenameSucceeded )
206*cdf0e10cSrcweir         {
207*cdf0e10cSrcweir 			MsiSetProperty(handle, TEXT("OFFICERUNS"), TEXT("1"));
208*cdf0e10cSrcweir             SetMsiErrorCode( MSI_ERROR_OFFICE_IS_RUNNING );
209*cdf0e10cSrcweir         }
210*cdf0e10cSrcweir 
211*cdf0e10cSrcweir 		FindClose( hFind );
212*cdf0e10cSrcweir 	}
213*cdf0e10cSrcweir 
214*cdf0e10cSrcweir 	return ERROR_SUCCESS;
215*cdf0e10cSrcweir }
216*cdf0e10cSrcweir 
217*cdf0e10cSrcweir 
218*cdf0e10cSrcweir 
219