132b1fd08SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
332b1fd08SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
432b1fd08SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
532b1fd08SAndrew Rist  * distributed with this work for additional information
632b1fd08SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
732b1fd08SAndrew Rist  * to you under the Apache License, Version 2.0 (the
832b1fd08SAndrew Rist  * "License"); you may not use this file except in compliance
932b1fd08SAndrew Rist  * with the License.  You may obtain a copy of the License at
1032b1fd08SAndrew Rist  *
1132b1fd08SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
1232b1fd08SAndrew Rist  *
1332b1fd08SAndrew Rist  * Unless required by applicable law or agreed to in writing,
1432b1fd08SAndrew Rist  * software distributed under the License is distributed on an
1532b1fd08SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
1632b1fd08SAndrew Rist  * KIND, either express or implied.  See the License for the
1732b1fd08SAndrew Rist  * specific language governing permissions and limitations
1832b1fd08SAndrew Rist  * under the License.
1932b1fd08SAndrew Rist  *
2032b1fd08SAndrew Rist  *************************************************************/
2132b1fd08SAndrew Rist 
2232b1fd08SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir #define _WIN32_WINDOWS 0x0410
25cdf0e10cSrcweir 
26cdf0e10cSrcweir #ifdef _MSC_VER
27cdf0e10cSrcweir #pragma warning(push, 1) /* disable warnings within system headers */
28cdf0e10cSrcweir #endif
29cdf0e10cSrcweir #define WIN32_LEAN_AND_MEAN
30cdf0e10cSrcweir #include <windows.h>
31cdf0e10cSrcweir #include <msiquery.h>
32cdf0e10cSrcweir #ifdef _MSC_VER
33cdf0e10cSrcweir #pragma warning(pop)
34cdf0e10cSrcweir #endif
35cdf0e10cSrcweir 
36cdf0e10cSrcweir #include <malloc.h>
37cdf0e10cSrcweir #include <assert.h>
38cdf0e10cSrcweir 
39cdf0e10cSrcweir #ifdef UNICODE
40cdf0e10cSrcweir #define _UNICODE
41cdf0e10cSrcweir #define _tstring	wstring
42cdf0e10cSrcweir #else
43cdf0e10cSrcweir #define _tstring	string
44cdf0e10cSrcweir #endif
45cdf0e10cSrcweir #include <tchar.h>
46cdf0e10cSrcweir #include <string>
47cdf0e10cSrcweir #include <queue>
48cdf0e10cSrcweir #include <stdio.h>
49cdf0e10cSrcweir 
50cdf0e10cSrcweir #include <systools/win32/uwinapi.h>
51cdf0e10cSrcweir #include <../tools/seterror.hxx>
52cdf0e10cSrcweir 
53cdf0e10cSrcweir #define	WININIT_FILENAME	"wininit.ini"
54cdf0e10cSrcweir #define RENAME_SECTION		"rename"
55cdf0e10cSrcweir 
56cdf0e10cSrcweir #ifdef DEBUG
57cdf0e10cSrcweir inline void OutputDebugStringFormat( LPCTSTR pFormat, ... )
58cdf0e10cSrcweir {
59cdf0e10cSrcweir 	_TCHAR	buffer[1024];
60cdf0e10cSrcweir 	va_list	args;
61cdf0e10cSrcweir 
62cdf0e10cSrcweir 	va_start( args, pFormat );
63cdf0e10cSrcweir 	_vsntprintf( buffer, elementsof(buffer), pFormat, args );
64cdf0e10cSrcweir 	OutputDebugString( buffer );
65cdf0e10cSrcweir }
66cdf0e10cSrcweir #else
67cdf0e10cSrcweir static inline void OutputDebugStringFormat( LPCTSTR, ... )
68cdf0e10cSrcweir {
69cdf0e10cSrcweir }
70cdf0e10cSrcweir #endif
71cdf0e10cSrcweir 
72cdf0e10cSrcweir static std::_tstring GetMsiProperty( MSIHANDLE handle, const std::_tstring& sProperty )
73cdf0e10cSrcweir {
74cdf0e10cSrcweir 	std::_tstring	result;
75cdf0e10cSrcweir 	TCHAR	szDummy[1] = TEXT("");
76cdf0e10cSrcweir 	DWORD	nChars = 0;
77cdf0e10cSrcweir 
78cdf0e10cSrcweir 	if ( MsiGetProperty( handle, sProperty.c_str(), szDummy, &nChars ) == ERROR_MORE_DATA )
79cdf0e10cSrcweir 	{
80cdf0e10cSrcweir 		DWORD nBytes = ++nChars * sizeof(TCHAR);
81cdf0e10cSrcweir 		LPTSTR buffer = reinterpret_cast<LPTSTR>(_alloca(nBytes));
82cdf0e10cSrcweir 		ZeroMemory( buffer, nBytes );
83cdf0e10cSrcweir 		MsiGetProperty(handle, sProperty.c_str(), buffer, &nChars);
84cdf0e10cSrcweir 		result = buffer;
85cdf0e10cSrcweir 	}
86cdf0e10cSrcweir 
87cdf0e10cSrcweir 	return	result;
88cdf0e10cSrcweir }
89cdf0e10cSrcweir 
90cdf0e10cSrcweir static inline bool IsSetMsiProperty(MSIHANDLE handle, const std::_tstring& sProperty)
91cdf0e10cSrcweir {
92cdf0e10cSrcweir 	std::_tstring value = GetMsiProperty(handle, sProperty);
93cdf0e10cSrcweir 	return (value.length() > 0);
94cdf0e10cSrcweir }
95cdf0e10cSrcweir 
96cdf0e10cSrcweir static inline void UnsetMsiProperty(MSIHANDLE handle, const std::_tstring& sProperty)
97cdf0e10cSrcweir {
98cdf0e10cSrcweir 	MsiSetProperty(handle, sProperty.c_str(), NULL);
99cdf0e10cSrcweir }
100cdf0e10cSrcweir 
101cdf0e10cSrcweir static inline void SetMsiProperty(MSIHANDLE handle, const std::_tstring& sProperty)
102cdf0e10cSrcweir {
103cdf0e10cSrcweir 	MsiSetProperty(handle, sProperty.c_str(), TEXT("1"));
104cdf0e10cSrcweir }
105cdf0e10cSrcweir 
106cdf0e10cSrcweir static BOOL MoveFileEx9x( LPCSTR lpExistingFileNameA, LPCSTR lpNewFileNameA, DWORD dwFlags )
107cdf0e10cSrcweir {
108cdf0e10cSrcweir 	BOOL	fSuccess = FALSE;	// assume failure
109cdf0e10cSrcweir 
110cdf0e10cSrcweir 	// Windows 9x has a special mechanism to move files after reboot
111cdf0e10cSrcweir 
112cdf0e10cSrcweir 	if ( dwFlags & MOVEFILE_DELAY_UNTIL_REBOOT )
113cdf0e10cSrcweir 	{
114cdf0e10cSrcweir 		CHAR	szExistingFileNameA[MAX_PATH];
115cdf0e10cSrcweir 		CHAR	szNewFileNameA[MAX_PATH] = "NUL";
116cdf0e10cSrcweir 
117cdf0e10cSrcweir 		// Path names in WININIT.INI must be in short path name form
118cdf0e10cSrcweir 
119cdf0e10cSrcweir 		if (
120cdf0e10cSrcweir 			GetShortPathNameA( lpExistingFileNameA, szExistingFileNameA, MAX_PATH ) &&
121cdf0e10cSrcweir 			(!lpNewFileNameA || GetShortPathNameA( lpNewFileNameA, szNewFileNameA, MAX_PATH ))
122cdf0e10cSrcweir 			)
123cdf0e10cSrcweir 		{
124cdf0e10cSrcweir 			CHAR	szBuffer[32767];	// The buffer size must not exceed 32K
125cdf0e10cSrcweir 			DWORD	dwBufLen = GetPrivateProfileSectionA( RENAME_SECTION, szBuffer, elementsof(szBuffer), WININIT_FILENAME );
126cdf0e10cSrcweir 
127cdf0e10cSrcweir 			CHAR	szRename[MAX_PATH];	// This is enough for at most to times 67 chracters
128cdf0e10cSrcweir 			strcpy( szRename, szNewFileNameA );
129cdf0e10cSrcweir 			strcat( szRename, "=" );
130cdf0e10cSrcweir 			strcat( szRename, szExistingFileNameA );
131cdf0e10cSrcweir 			size_t	lnRename = strlen(szRename);
132cdf0e10cSrcweir 
133cdf0e10cSrcweir 			if ( dwBufLen + lnRename + 2 <= elementsof(szBuffer) )
134cdf0e10cSrcweir 			{
135cdf0e10cSrcweir 				CopyMemory( &szBuffer[dwBufLen], szRename, lnRename );
136cdf0e10cSrcweir 				szBuffer[dwBufLen + lnRename ] = 0;
137cdf0e10cSrcweir 				szBuffer[dwBufLen + lnRename + 1 ] = 0;
138cdf0e10cSrcweir 
139cdf0e10cSrcweir 				fSuccess = WritePrivateProfileSectionA( RENAME_SECTION, szBuffer, WININIT_FILENAME );
140cdf0e10cSrcweir 			}
141cdf0e10cSrcweir 			else
142cdf0e10cSrcweir 				SetLastError( ERROR_BUFFER_OVERFLOW );
143cdf0e10cSrcweir 		}
144cdf0e10cSrcweir 	}
145cdf0e10cSrcweir 	else
146cdf0e10cSrcweir 	{
147cdf0e10cSrcweir 
148cdf0e10cSrcweir 		fSuccess = MoveFileA( lpExistingFileNameA, lpNewFileNameA );
149cdf0e10cSrcweir 
150cdf0e10cSrcweir 		if ( !fSuccess && GetLastError() != ERROR_ACCESS_DENIED &&
151cdf0e10cSrcweir 			0 != (dwFlags & (MOVEFILE_COPY_ALLOWED | MOVEFILE_REPLACE_EXISTING)) )
152cdf0e10cSrcweir 		{
153cdf0e10cSrcweir 			BOOL	bFailIfExist = 0 == (dwFlags & MOVEFILE_REPLACE_EXISTING);
154cdf0e10cSrcweir 
155cdf0e10cSrcweir 			fSuccess = CopyFileA( lpExistingFileNameA, lpNewFileNameA, bFailIfExist );
156cdf0e10cSrcweir 
157cdf0e10cSrcweir 			if ( fSuccess )
158cdf0e10cSrcweir 				fSuccess = DeleteFileA( lpExistingFileNameA );
159cdf0e10cSrcweir 		}
160cdf0e10cSrcweir 
161cdf0e10cSrcweir 	}
162cdf0e10cSrcweir 
163cdf0e10cSrcweir 	return fSuccess;
164cdf0e10cSrcweir }
165cdf0e10cSrcweir 
166cdf0e10cSrcweir static BOOL MoveFileExImpl( LPCSTR lpExistingFileNameA, LPCSTR lpNewFileNameA, DWORD dwFlags )
167cdf0e10cSrcweir {
168cdf0e10cSrcweir 	if ( 0 > ((LONG)GetVersion())) // High order bit indicates Win 9x
169cdf0e10cSrcweir 		return MoveFileEx9x( lpExistingFileNameA, lpNewFileNameA, dwFlags );
170cdf0e10cSrcweir 	else
171cdf0e10cSrcweir 		return MoveFileExA( lpExistingFileNameA, lpNewFileNameA, dwFlags );
172cdf0e10cSrcweir }
173cdf0e10cSrcweir 
174cdf0e10cSrcweir extern "C" UINT __stdcall IsOfficeRunning( MSIHANDLE handle )
175cdf0e10cSrcweir {
176cdf0e10cSrcweir 	std::_tstring	sInstDir = GetMsiProperty( handle, TEXT("INSTALLLOCATION") );
177*910823aeSJürgen Schmidt //	std::_tstring	sResourceDir = sInstDir + TEXT("Basis\\program\\resource\\");
178*910823aeSJürgen Schmidt 	std::_tstring	sResourceDir = sInstDir + TEXT("program\\resource\\");
179cdf0e10cSrcweir 	std::_tstring	sPattern = sResourceDir + TEXT("vcl*.res");
180cdf0e10cSrcweir 
181cdf0e10cSrcweir 	WIN32_FIND_DATA	aFindFileData;
182cdf0e10cSrcweir 	HANDLE	hFind = FindFirstFile( sPattern.c_str(), &aFindFileData );
183cdf0e10cSrcweir 
184cdf0e10cSrcweir 	if ( IsValidHandle(hFind) )
185cdf0e10cSrcweir 	{
186cdf0e10cSrcweir 		BOOL	fSuccess = false;
187cdf0e10cSrcweir 		bool	fRenameSucceeded;
188cdf0e10cSrcweir 
189cdf0e10cSrcweir 		do
190cdf0e10cSrcweir 		{
191cdf0e10cSrcweir 			std::_tstring	sResourceFile = sResourceDir + aFindFileData.cFileName;
192cdf0e10cSrcweir 			std::_tstring	sIntermediate = sResourceFile + TEXT(".tmp");
193cdf0e10cSrcweir 
194cdf0e10cSrcweir 			fRenameSucceeded = MoveFileExImpl( sResourceFile.c_str(), sIntermediate.c_str(), MOVEFILE_REPLACE_EXISTING );
195cdf0e10cSrcweir 			if ( fRenameSucceeded )
196cdf0e10cSrcweir 			{
197cdf0e10cSrcweir 				MoveFileExImpl( sIntermediate.c_str(), sResourceFile.c_str(), 0 );
198cdf0e10cSrcweir 				fSuccess = FindNextFile( hFind, &aFindFileData );
199cdf0e10cSrcweir 			}
200cdf0e10cSrcweir 		} while ( fSuccess && fRenameSucceeded );
201cdf0e10cSrcweir 
202cdf0e10cSrcweir 		if ( !fRenameSucceeded )
203cdf0e10cSrcweir         {
204cdf0e10cSrcweir 			MsiSetProperty(handle, TEXT("OFFICERUNS"), TEXT("1"));
205cdf0e10cSrcweir             SetMsiErrorCode( MSI_ERROR_OFFICE_IS_RUNNING );
206cdf0e10cSrcweir         }
207cdf0e10cSrcweir 
208cdf0e10cSrcweir 		FindClose( hFind );
209cdf0e10cSrcweir 	}
210cdf0e10cSrcweir 
211cdf0e10cSrcweir 	return ERROR_SUCCESS;
212cdf0e10cSrcweir }
213cdf0e10cSrcweir 
214cdf0e10cSrcweir 
215cdf0e10cSrcweir 
216