Update ooo320-m1
[ooovba.git] / setup_native / source / win32 / customactions / shellextensions / upgrade.cxx
blobed07ba809d95cccb44b7293fac37150cfafe5d4e
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: upgrade.cxx,v $
10 * $Revision: 1.8 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 #undef UNICODE
32 #undef _UNICODE
34 #define _WIN32_WINDOWS 0x0410
36 #ifdef _MSC_VER
37 #pragma warning(push, 1) /* disable warnings within system headers */
38 #endif
39 #define WIN32_LEAN_AND_MEAN
40 #include <windows.h>
41 #include <msiquery.h>
42 #ifdef _MSC_VER
43 #pragma warning(pop)
44 #endif
46 #include <malloc.h>
47 #include <assert.h>
49 #include <tchar.h>
50 #include <string>
52 using namespace std;
54 namespace
56 // The provided GUID must be without surounding '{}'
57 string GetGuidPart(const string& guid, int index)
59 assert((guid.length() == 36) && "No GUID or wrong format!");
60 assert(((index > -1) && (index < 5)) && "Out of range!");
62 if (index == 0) return string(guid.c_str(), 8);
63 if (index == 1) return string(guid.c_str() + 9, 4);
64 if (index == 2) return string(guid.c_str() + 14, 4);
65 if (index == 3) return string(guid.c_str() + 19, 4);
66 if (index == 4) return string(guid.c_str() + 24, 12);
68 return string();
71 void Swap(char* p1, char* p2)
73 char tmp = *p1;
74 *p1 = *p2;
75 *p2 = tmp;
78 string Invert(const string& str)
80 char* buff = reinterpret_cast<char*>(_alloca(str.length()));
81 strncpy(buff, str.c_str(), str.length());
83 char* front = buff;
84 char* back = buff + str.length() - 1;
86 while (front < back)
87 Swap(front++, back--);
89 return string(buff, str.length());
92 // Convert the upgrade code (which is a GUID) according
93 // to the way the windows installer does when writing it
94 // to the registry
95 // The first 8 bytes will be inverted, from the the last
96 // 8 bytes always the nibbles will be inverted for further
97 // details look in the MSDN under compressed registry keys
98 string ConvertGuid(const string& guid)
100 string convertedGuid;
102 string part = GetGuidPart(guid, 0);
103 convertedGuid = Invert(part);
105 part = GetGuidPart(guid, 1);
106 convertedGuid += Invert(part);
108 part = GetGuidPart(guid, 2);
109 convertedGuid += Invert(part);
111 part = GetGuidPart(guid, 3);
112 convertedGuid += Invert(string(part.c_str(), 2));
113 convertedGuid += Invert(string(part.c_str() + 2, 2));
115 part = GetGuidPart(guid, 4);
116 int pos = 0;
117 for (int i = 0; i < 6; i++)
119 convertedGuid += Invert(string(part.c_str() + pos, 2));
120 pos += 2;
122 return convertedGuid;
125 string GetMsiProperty(MSIHANDLE handle, const string& sProperty)
127 string result;
128 TCHAR szDummy[1] = TEXT("");
129 DWORD nChars = 0;
131 if (MsiGetProperty(handle, sProperty.c_str(), szDummy, &nChars) == ERROR_MORE_DATA)
133 DWORD nBytes = ++nChars * sizeof(TCHAR);
134 LPTSTR buffer = reinterpret_cast<LPTSTR>(_alloca(nBytes));
135 ZeroMemory( buffer, nBytes );
136 MsiGetProperty(handle, sProperty.c_str(), buffer, &nChars);
137 result = buffer;
139 return result;
142 inline bool IsSetMsiProperty(MSIHANDLE handle, const string& sProperty)
144 return (GetMsiProperty(handle, sProperty).length() > 0);
147 inline void UnsetMsiProperty(MSIHANDLE handle, const string& sProperty)
149 MsiSetProperty(handle, sProperty.c_str(), NULL);
152 inline void SetMsiProperty(MSIHANDLE handle, const string& sProperty)
154 MsiSetProperty(handle, sProperty.c_str(), TEXT("1"));
157 bool RegistryKeyHasUpgradeSubKey(
158 HKEY hRootKey, const string& regKey, const string& upgradeKey)
160 HKEY hKey;
161 if (RegOpenKey(hRootKey, regKey.c_str(), &hKey) == ERROR_SUCCESS)
163 DWORD nSubKeys;
164 DWORD lLongestSubKey;
166 if (RegQueryInfoKey(
167 hKey, NULL, NULL, NULL, &nSubKeys, &lLongestSubKey, NULL, NULL, NULL, NULL, NULL, NULL) == ERROR_SUCCESS)
169 LPTSTR buffer = reinterpret_cast<LPTSTR>(_alloca(lLongestSubKey + 1));
171 for (DWORD i = 0; i < nSubKeys; i++)
173 LONG ret = RegEnumKey(hKey, i, buffer, lLongestSubKey + 1);
174 if ((ret == ERROR_SUCCESS) && (buffer == upgradeKey))
175 return true;
179 return false;
181 } // namespace
183 extern "C" UINT __stdcall SetProductInstallMode(MSIHANDLE handle)
185 string upgradeCode = GetMsiProperty(handle, TEXT("UpgradeCode"));
186 upgradeCode = ConvertGuid(string(upgradeCode.c_str() + 1, upgradeCode.length() - 2));
188 //MessageBox(NULL, upgradeCode.c_str(), TEXT("Debug"), MB_OK);
190 if (RegistryKeyHasUpgradeSubKey(
191 HKEY_CURRENT_USER,
192 TEXT("Software\\Microsoft\\Installer\\UpgradeCodes"),
193 upgradeCode) && IsSetMsiProperty(handle, TEXT("ALLUSERS")))
195 UnsetMsiProperty(handle, TEXT("ALLUSERS"));
196 //MessageBox(NULL, "ALLUSERS removed", "DEBUG", MB_OK);
198 else if (RegistryKeyHasUpgradeSubKey(
199 HKEY_LOCAL_MACHINE,
200 TEXT("Software\\Classes\\Installer\\UpgradeCodes"),
201 upgradeCode) && !IsSetMsiProperty(handle, TEXT("ALLUSERS")))
203 SetMsiProperty(handle, TEXT("ALLUSERS"));
204 //MessageBox(NULL, "ALLUSERS set", "DEBUG", MB_OK);
206 return ERROR_SUCCESS;