xref: /trunk/main/setup_native/source/win32/customactions/shellextensions/checkrunningoffice.cxx (revision 1ecadb572e7010ff3b3382ad9bf179dbc6efadbb)
1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 #define _WIN32_WINDOWS 0x0410
29 
30 #ifdef _MSC_VER
31 #pragma warning(push, 1) /* disable warnings within system headers */
32 #endif
33 #define WIN32_LEAN_AND_MEAN
34 #include <windows.h>
35 #include <msiquery.h>
36 #ifdef _MSC_VER
37 #pragma warning(pop)
38 #endif
39 
40 #include <malloc.h>
41 #include <assert.h>
42 
43 #ifdef UNICODE
44 #define _UNICODE
45 #define _tstring    wstring
46 #else
47 #define _tstring    string
48 #endif
49 #include <tchar.h>
50 #include <string>
51 #include <queue>
52 #include <stdio.h>
53 
54 #include <systools/win32/uwinapi.h>
55 #include <../tools/seterror.hxx>
56 
57 #define WININIT_FILENAME    "wininit.ini"
58 #define RENAME_SECTION      "rename"
59 
60 #ifdef DEBUG
61 inline void OutputDebugStringFormat( LPCTSTR pFormat, ... )
62 {
63     _TCHAR  buffer[1024];
64     va_list args;
65 
66     va_start( args, pFormat );
67     _vsntprintf( buffer, elementsof(buffer), pFormat, args );
68     OutputDebugString( buffer );
69 }
70 #else
71 static inline void OutputDebugStringFormat( LPCTSTR, ... )
72 {
73 }
74 #endif
75 
76 static std::_tstring GetMsiProperty( MSIHANDLE handle, const std::_tstring& sProperty )
77 {
78     std::_tstring   result;
79     TCHAR   szDummy[1] = TEXT("");
80     DWORD   nChars = 0;
81 
82     if ( MsiGetProperty( handle, sProperty.c_str(), szDummy, &nChars ) == ERROR_MORE_DATA )
83     {
84         DWORD nBytes = ++nChars * sizeof(TCHAR);
85         LPTSTR buffer = reinterpret_cast<LPTSTR>(_alloca(nBytes));
86         ZeroMemory( buffer, nBytes );
87         MsiGetProperty(handle, sProperty.c_str(), buffer, &nChars);
88         result = buffer;
89     }
90 
91     return  result;
92 }
93 
94 static inline bool IsSetMsiProperty(MSIHANDLE handle, const std::_tstring& sProperty)
95 {
96     std::_tstring value = GetMsiProperty(handle, sProperty);
97     return (value.length() > 0);
98 }
99 
100 static inline void UnsetMsiProperty(MSIHANDLE handle, const std::_tstring& sProperty)
101 {
102     MsiSetProperty(handle, sProperty.c_str(), NULL);
103 }
104 
105 static inline void SetMsiProperty(MSIHANDLE handle, const std::_tstring& sProperty)
106 {
107     MsiSetProperty(handle, sProperty.c_str(), TEXT("1"));
108 }
109 
110 static BOOL MoveFileEx9x( LPCSTR lpExistingFileNameA, LPCSTR lpNewFileNameA, DWORD dwFlags )
111 {
112     BOOL    fSuccess = FALSE;   // assume failure
113 
114     // Windows 9x has a special mechanism to move files after reboot
115 
116     if ( dwFlags & MOVEFILE_DELAY_UNTIL_REBOOT )
117     {
118         CHAR    szExistingFileNameA[MAX_PATH];
119         CHAR    szNewFileNameA[MAX_PATH] = "NUL";
120 
121         // Path names in WININIT.INI must be in short path name form
122 
123         if (
124             GetShortPathNameA( lpExistingFileNameA, szExistingFileNameA, MAX_PATH ) &&
125             (!lpNewFileNameA || GetShortPathNameA( lpNewFileNameA, szNewFileNameA, MAX_PATH ))
126             )
127         {
128             CHAR    szBuffer[32767];    // The buffer size must not exceed 32K
129             DWORD   dwBufLen = GetPrivateProfileSectionA( RENAME_SECTION, szBuffer, elementsof(szBuffer), WININIT_FILENAME );
130 
131             CHAR    szRename[MAX_PATH]; // This is enough for at most to times 67 chracters
132             strcpy( szRename, szNewFileNameA );
133             strcat( szRename, "=" );
134             strcat( szRename, szExistingFileNameA );
135             size_t  lnRename = strlen(szRename);
136 
137             if ( dwBufLen + lnRename + 2 <= elementsof(szBuffer) )
138             {
139                 CopyMemory( &szBuffer[dwBufLen], szRename, lnRename );
140                 szBuffer[dwBufLen + lnRename ] = 0;
141                 szBuffer[dwBufLen + lnRename + 1 ] = 0;
142 
143                 fSuccess = WritePrivateProfileSectionA( RENAME_SECTION, szBuffer, WININIT_FILENAME );
144             }
145             else
146                 SetLastError( ERROR_BUFFER_OVERFLOW );
147         }
148     }
149     else
150     {
151 
152         fSuccess = MoveFileA( lpExistingFileNameA, lpNewFileNameA );
153 
154         if ( !fSuccess && GetLastError() != ERROR_ACCESS_DENIED &&
155             0 != (dwFlags & (MOVEFILE_COPY_ALLOWED | MOVEFILE_REPLACE_EXISTING)) )
156         {
157             BOOL    bFailIfExist = 0 == (dwFlags & MOVEFILE_REPLACE_EXISTING);
158 
159             fSuccess = CopyFileA( lpExistingFileNameA, lpNewFileNameA, bFailIfExist );
160 
161             if ( fSuccess )
162                 fSuccess = DeleteFileA( lpExistingFileNameA );
163         }
164 
165     }
166 
167     return fSuccess;
168 }
169 
170 static BOOL MoveFileExImpl( LPCSTR lpExistingFileNameA, LPCSTR lpNewFileNameA, DWORD dwFlags )
171 {
172     if ( 0 > ((LONG)GetVersion())) // High order bit indicates Win 9x
173         return MoveFileEx9x( lpExistingFileNameA, lpNewFileNameA, dwFlags );
174     else
175         return MoveFileExA( lpExistingFileNameA, lpNewFileNameA, dwFlags );
176 }
177 
178 extern "C" UINT __stdcall IsOfficeRunning( MSIHANDLE handle )
179 {
180     OSVERSIONINFO   osverinfo;
181     osverinfo.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
182     GetVersionEx( &osverinfo );
183 
184     // renaming the vcl resource doesn't work reliable with OS >= Windows Vista
185     if (osverinfo.dwMajorVersion < 6 )
186     {
187         std::_tstring sInstDir = GetMsiProperty( handle, TEXT("INSTALLLOCATION") );
188         // Property empty -> no office installed
189         if ( sInstDir.length() == 0 )
190             return ERROR_SUCCESS;
191 
192         std::_tstring sResourceDir = sInstDir + TEXT("Basis\\program\\resource\\");
193         std::_tstring sPattern = sResourceDir + TEXT("vcl*.res");
194 
195 //        std::_tstring mystr;
196 //        mystr = "IsOfficeRunning start. Checking file in dir: " + sResourceDir;
197 //        MessageBox( NULL, mystr.c_str(), "IsOfficeRunning", MB_OK );
198 
199         WIN32_FIND_DATA aFindFileData;
200         HANDLE  hFind = FindFirstFile( sPattern.c_str(), &aFindFileData );
201 
202         if ( IsValidHandle(hFind) )
203         {
204             BOOL    fSuccess = false;
205             bool    fRenameSucceeded;
206 
207             do
208             {
209                 std::_tstring   sResourceFile = sResourceDir + aFindFileData.cFileName;
210                 std::_tstring   sIntermediate = sResourceFile + TEXT(".tmp");
211 
212                 fRenameSucceeded = MoveFileExImpl( sResourceFile.c_str(), sIntermediate.c_str(), MOVEFILE_REPLACE_EXISTING );
213                 if ( fRenameSucceeded )
214                 {
215                     MoveFileExImpl( sIntermediate.c_str(), sResourceFile.c_str(), 0 );
216                     fSuccess = FindNextFile( hFind, &aFindFileData );
217                 }
218             } while ( fSuccess && fRenameSucceeded );
219 
220             if ( !fRenameSucceeded )
221             {
222                 MsiSetProperty(handle, TEXT("OFFICERUNS"), TEXT("1"));
223                 SetMsiErrorCode( MSI_ERROR_OFFICE_IS_RUNNING );
224 
225 //                mystr = "Office is running";
226 //                MessageBox( NULL, mystr.c_str(), "IsOfficeRunning", MB_OK );
227             }
228 
229             FindClose( hFind );
230         }
231 //        mystr = "IsOfficeRunning end";
232 //        MessageBox( NULL, mystr.c_str(), "IsOfficeRunning", MB_OK );
233     }
234     else
235     {
236         std::_tstring sOfficeInstallPath = GetMsiProperty(handle, TEXT("INSTALLLOCATION"));
237         // Property empty -> no office installed
238         if ( sOfficeInstallPath.length() == 0 )
239             return ERROR_SUCCESS;
240 
241         std::_tstring sRenameSrc = sOfficeInstallPath + TEXT("program");
242         std::_tstring sRenameDst = sOfficeInstallPath + TEXT("program_test");
243 
244         bool bSuccess = MoveFile( sRenameSrc.c_str(), sRenameDst.c_str() );
245 
246         if ( bSuccess )
247         {
248             MoveFile( sRenameDst.c_str(), sRenameSrc.c_str() );
249         }
250         else
251         {
252             DWORD  dwError = GetLastError();
253             LPVOID lpMsgBuf;
254             // When there is no program folder, there could be no running office
255             if ( dwError == ERROR_FILE_NOT_FOUND )
256                 return ERROR_SUCCESS;
257             if ( dwError == ERROR_PATH_NOT_FOUND )
258                 return ERROR_SUCCESS;
259 
260             // The destination folder should never exist, don't know what to do here
261             if ( dwError == ERROR_ALREADY_EXISTS )
262                 return ERROR_SUCCESS;
263 
264             if ( FormatMessage(
265                 FORMAT_MESSAGE_ALLOCATE_BUFFER |
266                 FORMAT_MESSAGE_FROM_SYSTEM |
267                 FORMAT_MESSAGE_IGNORE_INSERTS,
268                 NULL,
269                 GetLastError(),
270                 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
271                 (LPTSTR) &lpMsgBuf,
272                 0,
273                 NULL ))
274             {
275                 OutputDebugStringFormat( TEXT("Error Code %d: %s"), dwError, lpMsgBuf );
276                 LocalFree( lpMsgBuf );
277             }
278             else
279                 OutputDebugStringFormat( TEXT("Error Code %d: Unknown"), dwError );
280 
281             MsiSetProperty( handle, TEXT("OFFICERUNS"), TEXT("1") );
282             SetMsiErrorCode( MSI_ERROR_OFFICE_IS_RUNNING );
283         }
284     }
285 
286     return ERROR_SUCCESS;
287 }
288 
289 
290 
291