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 OSVERSIONINFO osverinfo; 181*cdf0e10cSrcweir osverinfo.dwOSVersionInfoSize = sizeof(OSVERSIONINFO); 182*cdf0e10cSrcweir GetVersionEx( &osverinfo ); 183*cdf0e10cSrcweir 184*cdf0e10cSrcweir // renaming the vcl resource doesn't work reliable with OS >= Windows Vista 185*cdf0e10cSrcweir if (osverinfo.dwMajorVersion < 6 ) 186*cdf0e10cSrcweir { 187*cdf0e10cSrcweir std::_tstring sInstDir = GetMsiProperty( handle, TEXT("INSTALLLOCATION") ); 188*cdf0e10cSrcweir // Property empty -> no office installed 189*cdf0e10cSrcweir if ( sInstDir.length() == 0 ) 190*cdf0e10cSrcweir return ERROR_SUCCESS; 191*cdf0e10cSrcweir 192*cdf0e10cSrcweir std::_tstring sResourceDir = sInstDir + TEXT("Basis\\program\\resource\\"); 193*cdf0e10cSrcweir std::_tstring sPattern = sResourceDir + TEXT("vcl*.res"); 194*cdf0e10cSrcweir 195*cdf0e10cSrcweir // std::_tstring mystr; 196*cdf0e10cSrcweir // mystr = "IsOfficeRunning start. Checking file in dir: " + sResourceDir; 197*cdf0e10cSrcweir // MessageBox( NULL, mystr.c_str(), "IsOfficeRunning", MB_OK ); 198*cdf0e10cSrcweir 199*cdf0e10cSrcweir WIN32_FIND_DATA aFindFileData; 200*cdf0e10cSrcweir HANDLE hFind = FindFirstFile( sPattern.c_str(), &aFindFileData ); 201*cdf0e10cSrcweir 202*cdf0e10cSrcweir if ( IsValidHandle(hFind) ) 203*cdf0e10cSrcweir { 204*cdf0e10cSrcweir BOOL fSuccess = false; 205*cdf0e10cSrcweir bool fRenameSucceeded; 206*cdf0e10cSrcweir 207*cdf0e10cSrcweir do 208*cdf0e10cSrcweir { 209*cdf0e10cSrcweir std::_tstring sResourceFile = sResourceDir + aFindFileData.cFileName; 210*cdf0e10cSrcweir std::_tstring sIntermediate = sResourceFile + TEXT(".tmp"); 211*cdf0e10cSrcweir 212*cdf0e10cSrcweir fRenameSucceeded = MoveFileExImpl( sResourceFile.c_str(), sIntermediate.c_str(), MOVEFILE_REPLACE_EXISTING ); 213*cdf0e10cSrcweir if ( fRenameSucceeded ) 214*cdf0e10cSrcweir { 215*cdf0e10cSrcweir MoveFileExImpl( sIntermediate.c_str(), sResourceFile.c_str(), 0 ); 216*cdf0e10cSrcweir fSuccess = FindNextFile( hFind, &aFindFileData ); 217*cdf0e10cSrcweir } 218*cdf0e10cSrcweir } while ( fSuccess && fRenameSucceeded ); 219*cdf0e10cSrcweir 220*cdf0e10cSrcweir if ( !fRenameSucceeded ) 221*cdf0e10cSrcweir { 222*cdf0e10cSrcweir MsiSetProperty(handle, TEXT("OFFICERUNS"), TEXT("1")); 223*cdf0e10cSrcweir SetMsiErrorCode( MSI_ERROR_OFFICE_IS_RUNNING ); 224*cdf0e10cSrcweir 225*cdf0e10cSrcweir // mystr = "Office is running"; 226*cdf0e10cSrcweir // MessageBox( NULL, mystr.c_str(), "IsOfficeRunning", MB_OK ); 227*cdf0e10cSrcweir } 228*cdf0e10cSrcweir 229*cdf0e10cSrcweir FindClose( hFind ); 230*cdf0e10cSrcweir } 231*cdf0e10cSrcweir // mystr = "IsOfficeRunning end"; 232*cdf0e10cSrcweir // MessageBox( NULL, mystr.c_str(), "IsOfficeRunning", MB_OK ); 233*cdf0e10cSrcweir } 234*cdf0e10cSrcweir else 235*cdf0e10cSrcweir { 236*cdf0e10cSrcweir std::_tstring sOfficeInstallPath = GetMsiProperty(handle, TEXT("INSTALLLOCATION")); 237*cdf0e10cSrcweir // Property empty -> no office installed 238*cdf0e10cSrcweir if ( sOfficeInstallPath.length() == 0 ) 239*cdf0e10cSrcweir return ERROR_SUCCESS; 240*cdf0e10cSrcweir 241*cdf0e10cSrcweir std::_tstring sRenameSrc = sOfficeInstallPath + TEXT("program"); 242*cdf0e10cSrcweir std::_tstring sRenameDst = sOfficeInstallPath + TEXT("program_test"); 243*cdf0e10cSrcweir 244*cdf0e10cSrcweir bool bSuccess = MoveFile( sRenameSrc.c_str(), sRenameDst.c_str() ); 245*cdf0e10cSrcweir 246*cdf0e10cSrcweir if ( bSuccess ) 247*cdf0e10cSrcweir { 248*cdf0e10cSrcweir MoveFile( sRenameDst.c_str(), sRenameSrc.c_str() ); 249*cdf0e10cSrcweir } 250*cdf0e10cSrcweir else 251*cdf0e10cSrcweir { 252*cdf0e10cSrcweir DWORD dwError = GetLastError(); 253*cdf0e10cSrcweir LPVOID lpMsgBuf; 254*cdf0e10cSrcweir // When there is no program folder, there could be no running office 255*cdf0e10cSrcweir if ( dwError == ERROR_FILE_NOT_FOUND ) 256*cdf0e10cSrcweir return ERROR_SUCCESS; 257*cdf0e10cSrcweir if ( dwError == ERROR_PATH_NOT_FOUND ) 258*cdf0e10cSrcweir return ERROR_SUCCESS; 259*cdf0e10cSrcweir 260*cdf0e10cSrcweir // The destination folder should never exist, don't know what to do here 261*cdf0e10cSrcweir if ( dwError == ERROR_ALREADY_EXISTS ) 262*cdf0e10cSrcweir return ERROR_SUCCESS; 263*cdf0e10cSrcweir 264*cdf0e10cSrcweir if ( FormatMessage( 265*cdf0e10cSrcweir FORMAT_MESSAGE_ALLOCATE_BUFFER | 266*cdf0e10cSrcweir FORMAT_MESSAGE_FROM_SYSTEM | 267*cdf0e10cSrcweir FORMAT_MESSAGE_IGNORE_INSERTS, 268*cdf0e10cSrcweir NULL, 269*cdf0e10cSrcweir GetLastError(), 270*cdf0e10cSrcweir MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language 271*cdf0e10cSrcweir (LPTSTR) &lpMsgBuf, 272*cdf0e10cSrcweir 0, 273*cdf0e10cSrcweir NULL )) 274*cdf0e10cSrcweir { 275*cdf0e10cSrcweir OutputDebugStringFormat( TEXT("Error Code %d: %s"), dwError, lpMsgBuf ); 276*cdf0e10cSrcweir LocalFree( lpMsgBuf ); 277*cdf0e10cSrcweir } 278*cdf0e10cSrcweir else 279*cdf0e10cSrcweir OutputDebugStringFormat( TEXT("Error Code %d: Unknown"), dwError ); 280*cdf0e10cSrcweir 281*cdf0e10cSrcweir MsiSetProperty( handle, TEXT("OFFICERUNS"), TEXT("1") ); 282*cdf0e10cSrcweir SetMsiErrorCode( MSI_ERROR_OFFICE_IS_RUNNING ); 283*cdf0e10cSrcweir } 284*cdf0e10cSrcweir } 285*cdf0e10cSrcweir 286*cdf0e10cSrcweir return ERROR_SUCCESS; 287*cdf0e10cSrcweir } 288*cdf0e10cSrcweir 289*cdf0e10cSrcweir 290*cdf0e10cSrcweir 291