cmake/Utilities/Release/WiX/CustomAction/detect_nsis_overwrite.cpp

45 lines
1.1 KiB
C++
Raw Normal View History

2020-02-01 23:06:01 +01:00
#include <string>
#include <vector>
2016-03-13 13:35:51 +01:00
#include <windows.h>
2016-07-09 11:21:54 +02:00
2016-03-13 13:35:51 +01:00
#include <msi.h>
#include <msiquery.h>
std::wstring get_property(MSIHANDLE msi_handle, std::wstring const& name)
{
DWORD size = 0;
2020-02-01 23:06:01 +01:00
WCHAR value_buffer[] = L"";
UINT status = MsiGetPropertyW(msi_handle, name.c_str(), value_buffer, &size);
2016-03-13 13:35:51 +01:00
2016-07-09 11:21:54 +02:00
if (status == ERROR_MORE_DATA) {
2016-03-13 13:35:51 +01:00
std::vector<wchar_t> buffer(size + 1);
MsiGetPropertyW(msi_handle, name.c_str(), &buffer[0], &size);
return std::wstring(&buffer[0]);
2016-07-09 11:21:54 +02:00
} else {
2016-03-13 13:35:51 +01:00
return std::wstring();
2016-07-09 11:21:54 +02:00
}
2016-03-13 13:35:51 +01:00
}
2016-07-09 11:21:54 +02:00
void set_property(MSIHANDLE msi_handle, std::wstring const& name,
std::wstring const& value)
2016-03-13 13:35:51 +01:00
{
MsiSetPropertyW(msi_handle, name.c_str(), value.c_str());
}
extern "C" UINT __stdcall DetectNsisOverwrite(MSIHANDLE msi_handle)
{
std::wstring install_root = get_property(msi_handle, L"INSTALL_ROOT");
std::wstring uninstall_exe = install_root + L"\\uninstall.exe";
bool uninstall_exe_exists =
2016-07-09 11:21:54 +02:00
GetFileAttributesW(uninstall_exe.c_str()) != INVALID_FILE_ATTRIBUTES;
2016-03-13 13:35:51 +01:00
set_property(msi_handle, L"CMAKE_NSIS_OVERWRITE_DETECTED",
2016-07-09 11:21:54 +02:00
uninstall_exe_exists ? L"1" : L"0");
2016-03-13 13:35:51 +01:00
return ERROR_SUCCESS;
}