1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: upgrade.cxx,v $
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 ************************************************************************/
34 #define _WIN32_WINDOWS 0x0410
37 #pragma warning(push, 1) /* disable warnings within system headers */
39 #define WIN32_LEAN_AND_MEAN
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);
71 void Swap(char* p1
, char* p2
)
78 string
Invert(const string
& str
)
80 char* buff
= reinterpret_cast<char*>(_alloca(str
.length()));
81 strncpy(buff
, str
.c_str(), str
.length());
84 char* back
= buff
+ str
.length() - 1;
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
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);
117 for (int i
= 0; i
< 6; i
++)
119 convertedGuid
+= Invert(string(part
.c_str() + pos
, 2));
122 return convertedGuid
;
125 string
GetMsiProperty(MSIHANDLE handle
, const string
& sProperty
)
128 TCHAR szDummy
[1] = TEXT("");
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
);
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
)
161 if (RegOpenKey(hRootKey
, regKey
.c_str(), &hKey
) == ERROR_SUCCESS
)
164 DWORD lLongestSubKey
;
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
))
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(
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(
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
;