1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include "shlxtmsi.hxx"
27 // The provided GUID must be without surrounding '{}'
28 std::wstring
GetGuidPart(const std::wstring
& guid
, int index
)
30 assert((guid
.length() == 36) && "No GUID or wrong format!");
31 assert(((index
> -1) && (index
< 5)) && "Out of range!");
33 if (index
== 0) return guid
.substr(0, 8);
34 if (index
== 1) return guid
.substr(9, 4);
35 if (index
== 2) return guid
.substr(14, 4);
36 if (index
== 3) return guid
.substr(19, 4);
37 if (index
== 4) return guid
.substr(24, 12);
39 return std::wstring();
42 void Swap(wchar_t* p1
, wchar_t* p2
)
49 std::wstring
Invert(const std::wstring
& str
)
51 wchar_t* buff
= static_cast<wchar_t*>(_alloca(str
.length()*sizeof(wchar_t)));
52 wcsncpy(buff
, str
.c_str(), str
.length());
54 wchar_t* front
= buff
;
55 wchar_t* back
= buff
+ str
.length() - 1;
58 Swap(front
++, back
--);
60 return std::wstring(buff
, str
.length());
63 // Convert the upgrade code (which is a GUID) according
64 // to the way the windows installer does when writing it
66 // The first 8 bytes will be inverted, from the last
67 // 8 bytes always the nibbles will be inverted for further
68 // details look in the MSDN under compressed registry keys
69 std::wstring
ConvertGuid(const std::wstring
& guid
)
71 std::wstring convertedGuid
;
73 std::wstring part
= GetGuidPart(guid
, 0);
74 convertedGuid
= Invert(part
);
76 part
= GetGuidPart(guid
, 1);
77 convertedGuid
+= Invert(part
);
79 part
= GetGuidPart(guid
, 2);
80 convertedGuid
+= Invert(part
);
82 part
= GetGuidPart(guid
, 3);
83 convertedGuid
+= Invert(part
.substr(0, 2));
84 convertedGuid
+= Invert(part
.substr(2, 2));
86 part
= GetGuidPart(guid
, 4);
88 for (int i
= 0; i
< 6; i
++)
90 convertedGuid
+= Invert(std::wstring(part
.c_str() + pos
, 2));
96 bool IsSetMsiPropertyW(MSIHANDLE handle
, const std::wstring
& sProperty
)
98 return (GetMsiPropertyW(handle
, sProperty
).length() > 0);
101 void SetMsiPropertyW(MSIHANDLE handle
, const std::wstring
& sProperty
)
103 MsiSetPropertyW(handle
, sProperty
.c_str(), L
"1");
106 bool RegistryKeyHasUpgradeSubKey(
107 HKEY hRootKey
, const std::wstring
& regKey
, const std::wstring
& upgradeKey
)
110 if (RegOpenKeyW(hRootKey
, regKey
.c_str(), &hKey
) == ERROR_SUCCESS
)
113 DWORD lLongestSubKey
;
115 if (RegQueryInfoKeyW(
116 hKey
, nullptr, nullptr, nullptr, &nSubKeys
, &lLongestSubKey
, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr) == ERROR_SUCCESS
)
118 LPWSTR buffer
= static_cast<LPWSTR
>(_alloca((lLongestSubKey
+ 1)*sizeof(WCHAR
)));
120 for (DWORD i
= 0; i
< nSubKeys
; i
++)
122 LONG ret
= RegEnumKeyW(hKey
, i
, buffer
, lLongestSubKey
+ 1);
123 if ((ret
== ERROR_SUCCESS
) && (buffer
== upgradeKey
))
132 extern "C" __declspec(dllexport
) UINT __stdcall
SetProductInstallMode(MSIHANDLE handle
)
134 std::wstring upgradeCode
= GetMsiPropertyW(handle
, L
"UpgradeCode");
135 upgradeCode
= ConvertGuid(std::wstring(upgradeCode
.c_str() + 1, upgradeCode
.length() - 2));
137 // MessageBoxW(NULL, upgradeCode.c_str(), "Debug", MB_OK);
139 if (RegistryKeyHasUpgradeSubKey(
141 L
"Software\\Microsoft\\Installer\\UpgradeCodes",
142 upgradeCode
) && IsSetMsiPropertyW(handle
, L
"ALLUSERS"))
144 UnsetMsiPropertyW(handle
, L
"ALLUSERS");
145 // MessageBoxW(NULL, L"ALLUSERS removed", L"DEBUG", MB_OK);
147 else if (RegistryKeyHasUpgradeSubKey(
149 L
"Software\\Classes\\Installer\\UpgradeCodes",
150 upgradeCode
) && !IsSetMsiPropertyW(handle
, L
"ALLUSERS"))
152 SetMsiPropertyW(handle
, L
"ALLUSERS");
153 // MessageBoxW(NULL, L"ALLUSERS set", L"DEBUG", MB_OK);
155 return ERROR_SUCCESS
;
158 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */