detect_nsis_overwrite.cpp 1.1 KB

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