cmVSSetupHelper.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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() { ptr = NULL; }
  17. SmartCOMPtr(T* p)
  18. {
  19. ptr = p;
  20. if (ptr != NULL)
  21. ptr->AddRef();
  22. }
  23. SmartCOMPtr(const SmartCOMPtr<T>& sptr)
  24. {
  25. ptr = sptr.ptr;
  26. if (ptr != NULL)
  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 != NULL)
  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 != NULL) {
  45. return ptr->QueryInterface(rclsid, (void**)pp);
  46. } else {
  47. return E_FAIL;
  48. }
  49. }
  50. HRESULT CoCreateInstance(REFCLSID clsid, IUnknown* pUnknown,
  51. REFIID interfaceId, DWORD dwClsContext = CLSCTX_ALL)
  52. {
  53. HRESULT hr = ::CoCreateInstance(clsid, pUnknown, dwClsContext, interfaceId,
  54. (void**)&ptr);
  55. return hr;
  56. }
  57. ~SmartCOMPtr()
  58. {
  59. if (ptr != NULL)
  60. ptr->Release();
  61. }
  62. private:
  63. T* ptr;
  64. };
  65. class SmartBSTR
  66. {
  67. public:
  68. SmartBSTR() { str = NULL; }
  69. SmartBSTR(const SmartBSTR& src) = delete;
  70. SmartBSTR& operator=(const SmartBSTR& src) = delete;
  71. operator BSTR() const { return str; }
  72. BSTR* operator&() throw() { return &str; }
  73. ~SmartBSTR() throw() { ::SysFreeString(str); }
  74. private:
  75. BSTR str;
  76. };
  77. struct VSInstanceInfo
  78. {
  79. std::wstring InstanceId;
  80. std::wstring VSInstallLocation;
  81. std::wstring Version;
  82. std::string VCToolsetVersion;
  83. ULONGLONG ullVersion = 0; // A.B.C.D = (A<<48)|(B<<32)|(C<<16)|D
  84. bool IsWin10SDKInstalled = false;
  85. bool IsWin81SDKInstalled = false;
  86. std::string GetInstallLocation() const;
  87. };
  88. class cmVSSetupAPIHelper
  89. {
  90. public:
  91. cmVSSetupAPIHelper(unsigned int version);
  92. ~cmVSSetupAPIHelper();
  93. bool SetVSInstance(std::string const& vsInstallLocation);
  94. bool IsVSInstalled();
  95. bool GetVSInstanceInfo(std::string& vsInstallLocation);
  96. bool GetVSInstanceVersion(std::string& vsInstanceVersion);
  97. bool GetVCToolsetVersion(std::string& vsToolsetVersion);
  98. bool IsWin10SDKInstalled();
  99. bool IsWin81SDKInstalled();
  100. private:
  101. bool Initialize();
  102. bool GetVSInstanceInfo(SmartCOMPtr<ISetupInstance2> instance2,
  103. VSInstanceInfo& vsInstanceInfo);
  104. bool CheckInstalledComponent(SmartCOMPtr<ISetupPackageReference> package,
  105. bool& bWin10SDK, bool& bWin81SDK);
  106. int ChooseVSInstance(const std::vector<VSInstanceInfo>& vecVSInstances);
  107. bool EnumerateAndChooseVSInstance();
  108. unsigned int Version;
  109. // COM ptrs to query about VS instances
  110. SmartCOMPtr<ISetupConfiguration> setupConfig;
  111. SmartCOMPtr<ISetupConfiguration2> setupConfig2;
  112. SmartCOMPtr<ISetupHelper> setupHelper;
  113. // used to indicate failure in Initialize(), so we don't have to call again
  114. bool initializationFailure;
  115. // indicated if COM initialization is successful
  116. HRESULT comInitialized;
  117. // current best instance of VS selected
  118. VSInstanceInfo chosenInstanceInfo;
  119. bool IsEWDKEnabled();
  120. std::string SpecifiedVSInstallLocation;
  121. };