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 std::wstring(guid.c_str(), 8);
34 if (index == 1)
return std::wstring(guid.c_str() + 9, 4);
35 if (index == 2)
return std::wstring(guid.c_str() + 14, 4);
36 if (index == 3)
return std::wstring(guid.c_str() + 19, 4);
37 if (index == 4)
return std::wstring(guid.c_str() + 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());
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(std::wstring(part.c_str(), 2));
84 convertedGuid +=
Invert(std::wstring(part.c_str() + 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)
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))
132extern "C" __declspec(dllexport) UINT __stdcall SetProductInstallMode(MSIHANDLE handle)
135 upgradeCode = ConvertGuid(std::wstring(upgradeCode.c_str() + 1, upgradeCode.length() - 2));
139 if (RegistryKeyHasUpgradeSubKey(
141 L
"Software\\Microsoft\\Installer\\UpgradeCodes",
142 upgradeCode) && IsSetMsiPropertyW(handle, L
"ALLUSERS"))
147 else if (RegistryKeyHasUpgradeSubKey(
149 L
"Software\\Classes\\Installer\\UpgradeCodes",
150 upgradeCode) && !IsSetMsiPropertyW(handle, L
"ALLUSERS"))
155 return ERROR_SUCCESS;
static std::wstring GetMsiPropertyW(MSIHANDLE handle, const std::wstring &sProperty)
static void SetMsiPropertyW(MSIHANDLE handle, const std::wstring &sProperty, const std::wstring &sValue)
static void UnsetMsiPropertyW(MSIHANDLE handle, const std::wstring &sProperty)
__declspec(dllexport) UINT __stdcall SetProductInstallMode(MSIHANDLE handle)