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 #undef UNICODE 29*cdf0e10cSrcweir #undef _UNICODE 30*cdf0e10cSrcweir 31*cdf0e10cSrcweir #define _WIN32_WINDOWS 0x0410 32*cdf0e10cSrcweir 33*cdf0e10cSrcweir #ifdef _MSC_VER 34*cdf0e10cSrcweir #pragma warning(push, 1) /* disable warnings within system headers */ 35*cdf0e10cSrcweir #endif 36*cdf0e10cSrcweir #define WIN32_LEAN_AND_MEAN 37*cdf0e10cSrcweir #include <windows.h> 38*cdf0e10cSrcweir #include <msiquery.h> 39*cdf0e10cSrcweir #ifdef _MSC_VER 40*cdf0e10cSrcweir #pragma warning(pop) 41*cdf0e10cSrcweir #endif 42*cdf0e10cSrcweir 43*cdf0e10cSrcweir #include <malloc.h> 44*cdf0e10cSrcweir #include <assert.h> 45*cdf0e10cSrcweir 46*cdf0e10cSrcweir #include <tchar.h> 47*cdf0e10cSrcweir #include <string> 48*cdf0e10cSrcweir 49*cdf0e10cSrcweir using namespace std; 50*cdf0e10cSrcweir 51*cdf0e10cSrcweir namespace 52*cdf0e10cSrcweir { 53*cdf0e10cSrcweir // The provided GUID must be without surounding '{}' 54*cdf0e10cSrcweir string GetGuidPart(const string& guid, int index) 55*cdf0e10cSrcweir { 56*cdf0e10cSrcweir assert((guid.length() == 36) && "No GUID or wrong format!"); 57*cdf0e10cSrcweir assert(((index > -1) && (index < 5)) && "Out of range!"); 58*cdf0e10cSrcweir 59*cdf0e10cSrcweir if (index == 0) return string(guid.c_str(), 8); 60*cdf0e10cSrcweir if (index == 1) return string(guid.c_str() + 9, 4); 61*cdf0e10cSrcweir if (index == 2) return string(guid.c_str() + 14, 4); 62*cdf0e10cSrcweir if (index == 3) return string(guid.c_str() + 19, 4); 63*cdf0e10cSrcweir if (index == 4) return string(guid.c_str() + 24, 12); 64*cdf0e10cSrcweir 65*cdf0e10cSrcweir return string(); 66*cdf0e10cSrcweir } 67*cdf0e10cSrcweir 68*cdf0e10cSrcweir void Swap(char* p1, char* p2) 69*cdf0e10cSrcweir { 70*cdf0e10cSrcweir char tmp = *p1; 71*cdf0e10cSrcweir *p1 = *p2; 72*cdf0e10cSrcweir *p2 = tmp; 73*cdf0e10cSrcweir } 74*cdf0e10cSrcweir 75*cdf0e10cSrcweir string Invert(const string& str) 76*cdf0e10cSrcweir { 77*cdf0e10cSrcweir char* buff = reinterpret_cast<char*>(_alloca(str.length())); 78*cdf0e10cSrcweir strncpy(buff, str.c_str(), str.length()); 79*cdf0e10cSrcweir 80*cdf0e10cSrcweir char* front = buff; 81*cdf0e10cSrcweir char* back = buff + str.length() - 1; 82*cdf0e10cSrcweir 83*cdf0e10cSrcweir while (front < back) 84*cdf0e10cSrcweir Swap(front++, back--); 85*cdf0e10cSrcweir 86*cdf0e10cSrcweir return string(buff, str.length()); 87*cdf0e10cSrcweir } 88*cdf0e10cSrcweir 89*cdf0e10cSrcweir // Convert the upgrade code (which is a GUID) according 90*cdf0e10cSrcweir // to the way the windows installer does when writing it 91*cdf0e10cSrcweir // to the registry 92*cdf0e10cSrcweir // The first 8 bytes will be inverted, from the the last 93*cdf0e10cSrcweir // 8 bytes always the nibbles will be inverted for further 94*cdf0e10cSrcweir // details look in the MSDN under compressed registry keys 95*cdf0e10cSrcweir string ConvertGuid(const string& guid) 96*cdf0e10cSrcweir { 97*cdf0e10cSrcweir string convertedGuid; 98*cdf0e10cSrcweir 99*cdf0e10cSrcweir string part = GetGuidPart(guid, 0); 100*cdf0e10cSrcweir convertedGuid = Invert(part); 101*cdf0e10cSrcweir 102*cdf0e10cSrcweir part = GetGuidPart(guid, 1); 103*cdf0e10cSrcweir convertedGuid += Invert(part); 104*cdf0e10cSrcweir 105*cdf0e10cSrcweir part = GetGuidPart(guid, 2); 106*cdf0e10cSrcweir convertedGuid += Invert(part); 107*cdf0e10cSrcweir 108*cdf0e10cSrcweir part = GetGuidPart(guid, 3); 109*cdf0e10cSrcweir convertedGuid += Invert(string(part.c_str(), 2)); 110*cdf0e10cSrcweir convertedGuid += Invert(string(part.c_str() + 2, 2)); 111*cdf0e10cSrcweir 112*cdf0e10cSrcweir part = GetGuidPart(guid, 4); 113*cdf0e10cSrcweir int pos = 0; 114*cdf0e10cSrcweir for (int i = 0; i < 6; i++) 115*cdf0e10cSrcweir { 116*cdf0e10cSrcweir convertedGuid += Invert(string(part.c_str() + pos, 2)); 117*cdf0e10cSrcweir pos += 2; 118*cdf0e10cSrcweir } 119*cdf0e10cSrcweir return convertedGuid; 120*cdf0e10cSrcweir } 121*cdf0e10cSrcweir 122*cdf0e10cSrcweir string GetMsiProperty(MSIHANDLE handle, const string& sProperty) 123*cdf0e10cSrcweir { 124*cdf0e10cSrcweir string result; 125*cdf0e10cSrcweir TCHAR szDummy[1] = TEXT(""); 126*cdf0e10cSrcweir DWORD nChars = 0; 127*cdf0e10cSrcweir 128*cdf0e10cSrcweir if (MsiGetProperty(handle, sProperty.c_str(), szDummy, &nChars) == ERROR_MORE_DATA) 129*cdf0e10cSrcweir { 130*cdf0e10cSrcweir DWORD nBytes = ++nChars * sizeof(TCHAR); 131*cdf0e10cSrcweir LPTSTR buffer = reinterpret_cast<LPTSTR>(_alloca(nBytes)); 132*cdf0e10cSrcweir ZeroMemory( buffer, nBytes ); 133*cdf0e10cSrcweir MsiGetProperty(handle, sProperty.c_str(), buffer, &nChars); 134*cdf0e10cSrcweir result = buffer; 135*cdf0e10cSrcweir } 136*cdf0e10cSrcweir return result; 137*cdf0e10cSrcweir } 138*cdf0e10cSrcweir 139*cdf0e10cSrcweir inline bool IsSetMsiProperty(MSIHANDLE handle, const string& sProperty) 140*cdf0e10cSrcweir { 141*cdf0e10cSrcweir return (GetMsiProperty(handle, sProperty).length() > 0); 142*cdf0e10cSrcweir } 143*cdf0e10cSrcweir 144*cdf0e10cSrcweir inline void UnsetMsiProperty(MSIHANDLE handle, const string& sProperty) 145*cdf0e10cSrcweir { 146*cdf0e10cSrcweir MsiSetProperty(handle, sProperty.c_str(), NULL); 147*cdf0e10cSrcweir } 148*cdf0e10cSrcweir 149*cdf0e10cSrcweir inline void SetMsiProperty(MSIHANDLE handle, const string& sProperty) 150*cdf0e10cSrcweir { 151*cdf0e10cSrcweir MsiSetProperty(handle, sProperty.c_str(), TEXT("1")); 152*cdf0e10cSrcweir } 153*cdf0e10cSrcweir 154*cdf0e10cSrcweir bool RegistryKeyHasUpgradeSubKey( 155*cdf0e10cSrcweir HKEY hRootKey, const string& regKey, const string& upgradeKey) 156*cdf0e10cSrcweir { 157*cdf0e10cSrcweir HKEY hKey; 158*cdf0e10cSrcweir if (RegOpenKey(hRootKey, regKey.c_str(), &hKey) == ERROR_SUCCESS) 159*cdf0e10cSrcweir { 160*cdf0e10cSrcweir DWORD nSubKeys; 161*cdf0e10cSrcweir DWORD lLongestSubKey; 162*cdf0e10cSrcweir 163*cdf0e10cSrcweir if (RegQueryInfoKey( 164*cdf0e10cSrcweir hKey, NULL, NULL, NULL, &nSubKeys, &lLongestSubKey, NULL, NULL, NULL, NULL, NULL, NULL) == ERROR_SUCCESS) 165*cdf0e10cSrcweir { 166*cdf0e10cSrcweir LPTSTR buffer = reinterpret_cast<LPTSTR>(_alloca(lLongestSubKey + 1)); 167*cdf0e10cSrcweir 168*cdf0e10cSrcweir for (DWORD i = 0; i < nSubKeys; i++) 169*cdf0e10cSrcweir { 170*cdf0e10cSrcweir LONG ret = RegEnumKey(hKey, i, buffer, lLongestSubKey + 1); 171*cdf0e10cSrcweir if ((ret == ERROR_SUCCESS) && (buffer == upgradeKey)) 172*cdf0e10cSrcweir return true; 173*cdf0e10cSrcweir } 174*cdf0e10cSrcweir } 175*cdf0e10cSrcweir } 176*cdf0e10cSrcweir return false; 177*cdf0e10cSrcweir } 178*cdf0e10cSrcweir } // namespace 179*cdf0e10cSrcweir 180*cdf0e10cSrcweir extern "C" UINT __stdcall SetProductInstallMode(MSIHANDLE handle) 181*cdf0e10cSrcweir { 182*cdf0e10cSrcweir string upgradeCode = GetMsiProperty(handle, TEXT("UpgradeCode")); 183*cdf0e10cSrcweir upgradeCode = ConvertGuid(string(upgradeCode.c_str() + 1, upgradeCode.length() - 2)); 184*cdf0e10cSrcweir 185*cdf0e10cSrcweir //MessageBox(NULL, upgradeCode.c_str(), TEXT("Debug"), MB_OK); 186*cdf0e10cSrcweir 187*cdf0e10cSrcweir if (RegistryKeyHasUpgradeSubKey( 188*cdf0e10cSrcweir HKEY_CURRENT_USER, 189*cdf0e10cSrcweir TEXT("Software\\Microsoft\\Installer\\UpgradeCodes"), 190*cdf0e10cSrcweir upgradeCode) && IsSetMsiProperty(handle, TEXT("ALLUSERS"))) 191*cdf0e10cSrcweir { 192*cdf0e10cSrcweir UnsetMsiProperty(handle, TEXT("ALLUSERS")); 193*cdf0e10cSrcweir //MessageBox(NULL, "ALLUSERS removed", "DEBUG", MB_OK); 194*cdf0e10cSrcweir } 195*cdf0e10cSrcweir else if (RegistryKeyHasUpgradeSubKey( 196*cdf0e10cSrcweir HKEY_LOCAL_MACHINE, 197*cdf0e10cSrcweir TEXT("Software\\Classes\\Installer\\UpgradeCodes"), 198*cdf0e10cSrcweir upgradeCode) && !IsSetMsiProperty(handle, TEXT("ALLUSERS"))) 199*cdf0e10cSrcweir { 200*cdf0e10cSrcweir SetMsiProperty(handle, TEXT("ALLUSERS")); 201*cdf0e10cSrcweir //MessageBox(NULL, "ALLUSERS set", "DEBUG", MB_OK); 202*cdf0e10cSrcweir } 203*cdf0e10cSrcweir return ERROR_SUCCESS; 204*cdf0e10cSrcweir } 205