cmVSSetupHelper.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. file Copyright.txt or https://cmake.org/licensing for details. */
  3. #pragma once
  4. #ifndef NOMINMAX
  5. # define NOMINMAX // Undefine min and max defined by windows.h
  6. #endif
  7. // Published by Visual Studio Setup team
  8. #include <cm3p/Setup.Configuration.h>
  9. #include <string>
  10. #include <vector>
  11. #include <windows.h>
  12. template <class T>
  13. class SmartCOMPtr
  14. {
  15. public:
  16. SmartCOMPtr() = default;
  17. SmartCOMPtr(T* p)
  18. {
  19. ptr = p;
  20. if (ptr != nullptr)
  21. ptr->AddRef();
  22. }
  23. SmartCOMPtr(const SmartCOMPtr<T>& sptr)
  24. {
  25. ptr = sptr.ptr;
  26. if (ptr != nullptr)
  27. ptr->AddRef();
  28. }
  29. T** operator&() { return &ptr; }
  30. T* operator->() { return ptr; }
  31. T* operator=(T* p)
  32. {
  33. if (*this != p) {
  34. ptr = p;
  35. if (ptr != nullptr)
  36. ptr->AddRef();
  37. }
  38. return *this;
  39. }
  40. operator T*() const { return ptr; }
  41. template <class I>
  42. HRESULT QueryInterface(REFCLSID rclsid, I** pp)
  43. {
  44. if (pp != nullptr) {
  45. return ptr->QueryInterface(rclsid, (void**)pp);
  46. }
  47. return E_FAIL;
  48. }
  49. HRESULT CoCreateInstance(REFCLSID clsid, IUnknown* pUnknown,
  50. REFIID interfaceId, DWORD dwClsContext = CLSCTX_ALL)
  51. {
  52. HRESULT hr = ::CoCreateInstance(clsid, pUnknown, dwClsContext, interfaceId,
  53. (void**)&ptr);
  54. return hr;
  55. }
  56. ~SmartCOMPtr()
  57. {
  58. if (ptr != nullptr)
  59. ptr->Release();
  60. }
  61. private:
  62. T* ptr = nullptr;
  63. };
  64. class SmartBSTR
  65. {
  66. public:
  67. SmartBSTR() = default;
  68. SmartBSTR(const SmartBSTR& src) = delete;
  69. SmartBSTR& operator=(const SmartBSTR& src) = delete;
  70. operator BSTR() const { return str; }
  71. BSTR* operator&() throw() { return &str; }
  72. ~SmartBSTR() throw() { ::SysFreeString(str); }
  73. private:
  74. BSTR str = nullptr;
  75. };
  76. struct VSInstanceInfo
  77. {
  78. std::string VSInstallLocation;
  79. std::string Version;
  80. std::string VCToolsetVersion;
  81. bool IsWin10SDKInstalled = false;
  82. bool IsWin81SDKInstalled = false;
  83. std::string GetInstallLocation() const;
  84. };
  85. class cmVSSetupAPIHelper
  86. {
  87. public:
  88. cmVSSetupAPIHelper(unsigned int version);
  89. ~cmVSSetupAPIHelper();
  90. bool SetVSInstance(std::string const& vsInstallLocation,
  91. std::string const& vsInstallVersion);
  92. bool IsVSInstalled();
  93. bool GetVSInstanceInfo(std::string& vsInstallLocation);
  94. bool GetVSInstanceVersion(std::string& vsInstanceVersion);
  95. bool GetVCToolsetVersion(std::string& vsToolsetVersion);
  96. bool IsWin10SDKInstalled();
  97. bool IsWin81SDKInstalled();
  98. private:
  99. bool Initialize();
  100. bool GetVSInstanceInfo(SmartCOMPtr<ISetupInstance2> instance2,
  101. VSInstanceInfo& vsInstanceInfo);
  102. bool CheckInstalledComponent(SmartCOMPtr<ISetupPackageReference> package,
  103. bool& bWin10SDK, bool& bWin81SDK);
  104. int ChooseVSInstance(const std::vector<VSInstanceInfo>& vecVSInstances);
  105. bool EnumerateAndChooseVSInstance();
  106. bool LoadSpecifiedVSInstanceFromDisk();
  107. bool EnumerateVSInstancesWithVswhere(
  108. std::vector<VSInstanceInfo>& VSInstances);
  109. bool EnumerateVSInstancesWithCOM(std::vector<VSInstanceInfo>& VSInstances);
  110. unsigned int Version;
  111. // COM ptrs to query about VS instances
  112. SmartCOMPtr<ISetupConfiguration> setupConfig;
  113. SmartCOMPtr<ISetupConfiguration2> setupConfig2;
  114. SmartCOMPtr<ISetupHelper> setupHelper;
  115. // used to indicate failure in Initialize(), so we don't have to call again
  116. bool initializationFailure = false;
  117. // indicated if COM initialization is successful
  118. HRESULT comInitialized;
  119. // current best instance of VS selected
  120. VSInstanceInfo chosenInstanceInfo;
  121. bool IsEWDKEnabled();
  122. std::string SpecifiedVSInstallLocation;
  123. std::string SpecifiedVSInstallVersion;
  124. };