detect_nsis_overwrite.cpp 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  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. WCHAR value_buffer[] = L"";
  10. UINT status = MsiGetPropertyW(msi_handle, name.c_str(), value_buffer, &size);
  11. if (status == ERROR_MORE_DATA) {
  12. std::vector<wchar_t> buffer(size + 1);
  13. MsiGetPropertyW(msi_handle, name.c_str(), &buffer[0], &size);
  14. return std::wstring(&buffer[0]);
  15. } else {
  16. return std::wstring();
  17. }
  18. }
  19. void set_property(MSIHANDLE msi_handle, std::wstring const& name,
  20. std::wstring const& value)
  21. {
  22. MsiSetPropertyW(msi_handle, name.c_str(), value.c_str());
  23. }
  24. extern "C" UINT __stdcall DetectNsisOverwrite(MSIHANDLE msi_handle)
  25. {
  26. std::wstring install_root = get_property(msi_handle, L"INSTALL_ROOT");
  27. std::wstring uninstall_exe = install_root + L"\\uninstall.exe";
  28. bool uninstall_exe_exists =
  29. GetFileAttributesW(uninstall_exe.c_str()) != INVALID_FILE_ATTRIBUTES;
  30. set_property(msi_handle, L"CMAKE_NSIS_OVERWRITE_DETECTED",
  31. uninstall_exe_exists ? L"1" : L"0");
  32. return ERROR_SUCCESS;
  33. }