| 1 |
#include <string> |
| 2 |
#include <vector> |
| 3 |
|
| 4 |
#include <windows.h> |
| 5 |
|
| 6 |
#include <msi.h> |
| 7 |
#include <msiquery.h> |
| 8 |
|
| 9 |
std::wstring get_property(MSIHANDLE msi_handle, std::wstring const& name) |
| 10 |
{ |
| 11 |
DWORD size = 0; |
| 12 |
|
| 13 |
WCHAR value_buffer[] = L""; |
| 14 |
UINT status = MsiGetPropertyW(msi_handle, name.c_str(), value_buffer, &size); |
| 15 |
|
| 16 |
if (status == ERROR_MORE_DATA) { |
| 17 |
std::vector<wchar_t> buffer(size + 1); |
| 18 |
MsiGetPropertyW(msi_handle, name.c_str(), &buffer[0], &size); |
| 19 |
return std::wstring(&buffer[0]); |
| 20 |
} else { |
| 21 |
return std::wstring(); |
| 22 |
} |
| 23 |
} |
| 24 |
|
| 25 |
void set_property(MSIHANDLE msi_handle, std::wstring const& name, |
| 26 |
std::wstring const& value) |
| 27 |
{ |
| 28 |
MsiSetPropertyW(msi_handle, name.c_str(), value.c_str()); |
| 29 |
} |
| 30 |
|
| 31 |
extern "C" UINT __stdcall DetectNsisOverwrite(MSIHANDLE msi_handle) |
| 32 |
{ |
| 33 |
std::wstring install_root = get_property(msi_handle, L"INSTALL_ROOT"); |
| 34 |
|
| 35 |
std::wstring uninstall_exe = install_root + L"\\uninstall.exe"; |
| 36 |
|
| 37 |
bool uninstall_exe_exists = |
| 38 |
GetFileAttributesW(uninstall_exe.c_str()) != INVALID_FILE_ATTRIBUTES; |
| 39 |
|
| 40 |
set_property(msi_handle, L"CMAKE_NSIS_OVERWRITE_DETECTED", |
| 41 |
uninstall_exe_exists ? L"1" : L"0"); |
| 42 |
|
| 43 |
return ERROR_SUCCESS; |
| 44 |
} |