cmVSSetupHelper.cxx 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623
  1. /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. file Copyright.txt or https://cmake.org/licensing for details. */
  3. #include "cmVSSetupHelper.h"
  4. #include <utility>
  5. #if !defined(CMAKE_BOOTSTRAP)
  6. # include <cm3p/json/reader.h>
  7. # include <cm3p/json/value.h>
  8. #endif
  9. #include "cmsys/Encoding.hxx"
  10. #include "cmsys/FStream.hxx"
  11. #include "cmStringAlgorithms.h"
  12. #include "cmSystemTools.h"
  13. #ifndef VSSetupConstants
  14. # define VSSetupConstants
  15. /* clang-format off */
  16. const IID IID_ISetupConfiguration = {
  17. 0x42843719, 0xDB4C, 0x46C2,
  18. { 0x8E, 0x7C, 0x64, 0xF1, 0x81, 0x6E, 0xFD, 0x5B }
  19. };
  20. const IID IID_ISetupConfiguration2 = {
  21. 0x26AAB78C, 0x4A60, 0x49D6,
  22. { 0xAF, 0x3B, 0x3C, 0x35, 0xBC, 0x93, 0x36, 0x5D }
  23. };
  24. const IID IID_ISetupPackageReference = {
  25. 0xda8d8a16, 0xb2b6, 0x4487,
  26. { 0xa2, 0xf1, 0x59, 0x4c, 0xcc, 0xcd, 0x6b, 0xf5 }
  27. };
  28. const IID IID_ISetupHelper = {
  29. 0x42b21b78, 0x6192, 0x463e,
  30. { 0x87, 0xbf, 0xd5, 0x77, 0x83, 0x8f, 0x1d, 0x5c }
  31. };
  32. const IID IID_IEnumSetupInstances = {
  33. 0x6380BCFF, 0x41D3, 0x4B2E,
  34. { 0x8B, 0x2E, 0xBF, 0x8A, 0x68, 0x10, 0xC8, 0x48 }
  35. };
  36. const IID IID_ISetupInstance2 = {
  37. 0x89143C9A, 0x05AF, 0x49B0,
  38. { 0xB7, 0x17, 0x72, 0xE2, 0x18, 0xA2, 0x18, 0x5C }
  39. };
  40. const IID IID_ISetupInstance = {
  41. 0xB41463C3, 0x8866, 0x43B5,
  42. { 0xBC, 0x33, 0x2B, 0x06, 0x76, 0xF7, 0xF4, 0x2E }
  43. };
  44. const CLSID CLSID_SetupConfiguration = {
  45. 0x177F0C4A, 0x1CD3, 0x4DE7,
  46. { 0xA3, 0x2C, 0x71, 0xDB, 0xBB, 0x9F, 0xA3, 0x6D }
  47. };
  48. /* clang-format on */
  49. #endif
  50. namespace {
  51. const WCHAR* Win10SDKComponent =
  52. L"Microsoft.VisualStudio.Component.Windows10SDK";
  53. const WCHAR* Win81SDKComponent =
  54. L"Microsoft.VisualStudio.Component.Windows81SDK";
  55. const WCHAR* ComponentType = L"Component";
  56. bool LoadVSInstanceVCToolsetVersion(VSInstanceInfo& vsInstanceInfo)
  57. {
  58. std::string const vcRoot = vsInstanceInfo.GetInstallLocation();
  59. std::string vcToolsVersionFile =
  60. vcRoot + "/VC/Auxiliary/Build/Microsoft.VCToolsVersion.default.txt";
  61. std::string vcToolsVersion;
  62. cmsys::ifstream fin(vcToolsVersionFile.c_str());
  63. if (!fin || !cmSystemTools::GetLineFromStream(fin, vcToolsVersion)) {
  64. return false;
  65. }
  66. vcToolsVersion = cmTrimWhitespace(vcToolsVersion);
  67. std::string const vcToolsDir = vcRoot + "/VC/Tools/MSVC/" + vcToolsVersion;
  68. if (!cmSystemTools::FileIsDirectory(vcToolsDir)) {
  69. return false;
  70. }
  71. vsInstanceInfo.VCToolsetVersion = vcToolsVersion;
  72. return true;
  73. }
  74. }
  75. std::string VSInstanceInfo::GetInstallLocation() const
  76. {
  77. return this->VSInstallLocation;
  78. }
  79. cmVSSetupAPIHelper::cmVSSetupAPIHelper(unsigned int version)
  80. : Version(version)
  81. , setupConfig(nullptr)
  82. , setupConfig2(nullptr)
  83. , setupHelper(nullptr)
  84. {
  85. comInitialized = CoInitializeEx(nullptr, 0);
  86. if (SUCCEEDED(comInitialized)) {
  87. Initialize();
  88. } else {
  89. initializationFailure = true;
  90. }
  91. }
  92. cmVSSetupAPIHelper::~cmVSSetupAPIHelper()
  93. {
  94. setupHelper = nullptr;
  95. setupConfig2 = nullptr;
  96. setupConfig = nullptr;
  97. if (SUCCEEDED(comInitialized))
  98. CoUninitialize();
  99. }
  100. bool cmVSSetupAPIHelper::SetVSInstance(std::string const& vsInstallLocation,
  101. std::string const& vsInstallVersion)
  102. {
  103. this->SpecifiedVSInstallLocation = vsInstallLocation;
  104. cmSystemTools::ConvertToUnixSlashes(this->SpecifiedVSInstallLocation);
  105. this->SpecifiedVSInstallVersion = vsInstallVersion;
  106. chosenInstanceInfo = VSInstanceInfo();
  107. return this->EnumerateAndChooseVSInstance();
  108. }
  109. bool cmVSSetupAPIHelper::IsVSInstalled()
  110. {
  111. return this->EnumerateAndChooseVSInstance();
  112. }
  113. bool cmVSSetupAPIHelper::IsWin10SDKInstalled()
  114. {
  115. return (this->EnumerateAndChooseVSInstance() &&
  116. chosenInstanceInfo.IsWin10SDKInstalled);
  117. }
  118. bool cmVSSetupAPIHelper::IsWin81SDKInstalled()
  119. {
  120. return (this->EnumerateAndChooseVSInstance() &&
  121. chosenInstanceInfo.IsWin81SDKInstalled);
  122. }
  123. bool cmVSSetupAPIHelper::CheckInstalledComponent(
  124. SmartCOMPtr<ISetupPackageReference> package, bool& bWin10SDK,
  125. bool& bWin81SDK)
  126. {
  127. bool ret = false;
  128. bWin10SDK = bWin81SDK = false;
  129. SmartBSTR bstrId;
  130. if (FAILED(package->GetId(&bstrId))) {
  131. return ret;
  132. }
  133. SmartBSTR bstrType;
  134. if (FAILED(package->GetType(&bstrType))) {
  135. return ret;
  136. }
  137. std::wstring id = std::wstring(bstrId);
  138. std::wstring type = std::wstring(bstrType);
  139. // Checks for any version of Win10 SDK. The version is appended at the end of
  140. // the
  141. // component name ex: Microsoft.VisualStudio.Component.Windows10SDK.10240
  142. if (id.find(Win10SDKComponent) != std::wstring::npos &&
  143. type.compare(ComponentType) == 0) {
  144. bWin10SDK = true;
  145. ret = true;
  146. }
  147. if (id.compare(Win81SDKComponent) == 0 && type.compare(ComponentType) == 0) {
  148. bWin81SDK = true;
  149. ret = true;
  150. }
  151. return ret;
  152. }
  153. // Gather additional info such as if VCToolset, WinSDKs are installed, location
  154. // of VS and version information.
  155. bool cmVSSetupAPIHelper::GetVSInstanceInfo(
  156. SmartCOMPtr<ISetupInstance2> pInstance, VSInstanceInfo& vsInstanceInfo)
  157. {
  158. if (pInstance == nullptr)
  159. return false;
  160. InstanceState state;
  161. if (FAILED(pInstance->GetState(&state))) {
  162. return false;
  163. }
  164. SmartBSTR bstrVersion;
  165. if (FAILED(pInstance->GetInstallationVersion(&bstrVersion))) {
  166. return false;
  167. } else {
  168. vsInstanceInfo.Version =
  169. cmsys::Encoding::ToNarrow(std::wstring(bstrVersion));
  170. }
  171. // Reboot may have been required before the installation path was created.
  172. SmartBSTR bstrInstallationPath;
  173. if ((eLocal & state) == eLocal) {
  174. if (FAILED(pInstance->GetInstallationPath(&bstrInstallationPath))) {
  175. return false;
  176. } else {
  177. vsInstanceInfo.VSInstallLocation =
  178. cmsys::Encoding::ToNarrow(std::wstring(bstrInstallationPath));
  179. cmSystemTools::ConvertToUnixSlashes(vsInstanceInfo.VSInstallLocation);
  180. }
  181. }
  182. // Check if a compiler is installed with this instance.
  183. if (!LoadVSInstanceVCToolsetVersion(vsInstanceInfo)) {
  184. return false;
  185. }
  186. // Reboot may have been required before the product package was registered
  187. // (last).
  188. if ((eRegistered & state) == eRegistered) {
  189. SmartCOMPtr<ISetupPackageReference> product;
  190. if (FAILED(pInstance->GetProduct(&product)) || !product) {
  191. return false;
  192. }
  193. LPSAFEARRAY lpsaPackages;
  194. if (FAILED(pInstance->GetPackages(&lpsaPackages)) ||
  195. lpsaPackages == nullptr) {
  196. return false;
  197. }
  198. int lower = lpsaPackages->rgsabound[0].lLbound;
  199. int upper = lpsaPackages->rgsabound[0].cElements + lower;
  200. IUnknown** ppData = (IUnknown**)lpsaPackages->pvData;
  201. for (int i = lower; i < upper; i++) {
  202. SmartCOMPtr<ISetupPackageReference> package = nullptr;
  203. if (FAILED(ppData[i]->QueryInterface(IID_ISetupPackageReference,
  204. (void**)&package)) ||
  205. package == nullptr)
  206. continue;
  207. bool win10SDKInstalled = false;
  208. bool win81SDkInstalled = false;
  209. bool ret =
  210. CheckInstalledComponent(package, win10SDKInstalled, win81SDkInstalled);
  211. if (ret) {
  212. vsInstanceInfo.IsWin10SDKInstalled |= win10SDKInstalled;
  213. vsInstanceInfo.IsWin81SDKInstalled |= win81SDkInstalled;
  214. }
  215. }
  216. SafeArrayDestroy(lpsaPackages);
  217. }
  218. return true;
  219. }
  220. bool cmVSSetupAPIHelper::GetVSInstanceInfo(std::string& vsInstallLocation)
  221. {
  222. vsInstallLocation.clear();
  223. bool isInstalled = this->EnumerateAndChooseVSInstance();
  224. if (isInstalled) {
  225. vsInstallLocation = chosenInstanceInfo.GetInstallLocation();
  226. }
  227. return isInstalled;
  228. }
  229. bool cmVSSetupAPIHelper::GetVSInstanceVersion(std::string& vsInstanceVersion)
  230. {
  231. vsInstanceVersion.clear();
  232. bool isInstalled = this->EnumerateAndChooseVSInstance();
  233. if (isInstalled) {
  234. vsInstanceVersion = chosenInstanceInfo.Version;
  235. }
  236. return isInstalled;
  237. }
  238. bool cmVSSetupAPIHelper::GetVCToolsetVersion(std::string& vsToolsetVersion)
  239. {
  240. vsToolsetVersion.clear();
  241. bool isInstalled = this->EnumerateAndChooseVSInstance();
  242. if (isInstalled) {
  243. vsToolsetVersion = chosenInstanceInfo.VCToolsetVersion;
  244. }
  245. return isInstalled && !vsToolsetVersion.empty();
  246. }
  247. bool cmVSSetupAPIHelper::IsEWDKEnabled()
  248. {
  249. std::string envEnterpriseWDK, envDisableRegistryUse;
  250. cmSystemTools::GetEnv("EnterpriseWDK", envEnterpriseWDK);
  251. cmSystemTools::GetEnv("DisableRegistryUse", envDisableRegistryUse);
  252. if (!cmSystemTools::Strucmp(envEnterpriseWDK.c_str(), "True") &&
  253. !cmSystemTools::Strucmp(envDisableRegistryUse.c_str(), "True")) {
  254. return true;
  255. }
  256. return false;
  257. }
  258. #if !defined(CMAKE_BOOTSTRAP)
  259. namespace {
  260. std::string FindVsWhereCommand()
  261. {
  262. std::string vswhere;
  263. static const char* programFiles[] = { "ProgramFiles(x86)", "ProgramFiles" };
  264. for (const char* pf : programFiles) {
  265. if (cmSystemTools::GetEnv(pf, vswhere)) {
  266. vswhere += "/Microsoft Visual Studio/Installer/vswhere.exe";
  267. if (cmSystemTools::FileExists(vswhere)) {
  268. return vswhere;
  269. }
  270. }
  271. }
  272. vswhere = "vswhere.exe";
  273. return vswhere;
  274. }
  275. }
  276. #endif
  277. bool cmVSSetupAPIHelper::EnumerateVSInstancesWithVswhere(
  278. std::vector<VSInstanceInfo>& VSInstances)
  279. {
  280. #if !defined(CMAKE_BOOTSTRAP)
  281. // Construct vswhere command to get installed VS instances in JSON format
  282. std::string vswhereExe = FindVsWhereCommand();
  283. std::vector<std::string> vswhereCmd = { vswhereExe, "-format", "json" };
  284. // Execute vswhere command and capture JSON output
  285. std::string json_output;
  286. int retVal = 1;
  287. if (!cmSystemTools::RunSingleCommand(vswhereCmd, &json_output, &json_output,
  288. &retVal, nullptr,
  289. cmSystemTools::OUTPUT_NONE)) {
  290. return false;
  291. }
  292. // Parse JSON output and iterate over elements
  293. Json::CharReaderBuilder builder;
  294. auto jsonReader = std::unique_ptr<Json::CharReader>(builder.newCharReader());
  295. Json::Value json;
  296. std::string error;
  297. if (!jsonReader->parse(json_output.data(),
  298. json_output.data() + json_output.size(), &json,
  299. &error)) {
  300. return false;
  301. }
  302. for (const auto& item : json) {
  303. VSInstanceInfo instance;
  304. instance.Version = item["installationVersion"].asString();
  305. instance.VSInstallLocation = item["installationPath"].asString();
  306. instance.IsWin10SDKInstalled = true;
  307. instance.IsWin81SDKInstalled = false;
  308. cmSystemTools::ConvertToUnixSlashes(instance.VSInstallLocation);
  309. if (LoadVSInstanceVCToolsetVersion(instance)) {
  310. VSInstances.push_back(instance);
  311. }
  312. }
  313. return true;
  314. #else
  315. static_cast<void>(VSInstances);
  316. return false;
  317. #endif
  318. }
  319. bool cmVSSetupAPIHelper::EnumerateVSInstancesWithCOM(
  320. std::vector<VSInstanceInfo>& VSInstances)
  321. {
  322. if (initializationFailure || setupConfig == nullptr ||
  323. setupConfig2 == nullptr || setupHelper == nullptr)
  324. return false;
  325. SmartCOMPtr<IEnumSetupInstances> enumInstances = nullptr;
  326. if (FAILED(
  327. setupConfig2->EnumInstances((IEnumSetupInstances**)&enumInstances)) ||
  328. !enumInstances) {
  329. return false;
  330. }
  331. SmartCOMPtr<ISetupInstance> instance;
  332. while (SUCCEEDED(enumInstances->Next(1, &instance, nullptr)) && instance) {
  333. SmartCOMPtr<ISetupInstance2> instance2 = nullptr;
  334. if (FAILED(
  335. instance->QueryInterface(IID_ISetupInstance2, (void**)&instance2)) ||
  336. !instance2) {
  337. instance = nullptr;
  338. continue;
  339. }
  340. VSInstanceInfo instanceInfo;
  341. bool isInstalled = GetVSInstanceInfo(instance2, instanceInfo);
  342. instance = instance2 = nullptr;
  343. if (isInstalled)
  344. VSInstances.push_back(instanceInfo);
  345. }
  346. return true;
  347. }
  348. bool cmVSSetupAPIHelper::EnumerateAndChooseVSInstance()
  349. {
  350. bool isVSInstanceExists = false;
  351. if (chosenInstanceInfo.VSInstallLocation.compare("") != 0) {
  352. return true;
  353. }
  354. if (this->IsEWDKEnabled()) {
  355. std::string envWindowsSdkDir81, envVSVersion, envVsInstallDir;
  356. cmSystemTools::GetEnv("WindowsSdkDir_81", envWindowsSdkDir81);
  357. cmSystemTools::GetEnv("VisualStudioVersion", envVSVersion);
  358. cmSystemTools::GetEnv("VSINSTALLDIR", envVsInstallDir);
  359. if (envVSVersion.empty() || envVsInstallDir.empty())
  360. return false;
  361. chosenInstanceInfo.VSInstallLocation = envVsInstallDir;
  362. chosenInstanceInfo.Version = envVSVersion;
  363. if (!LoadVSInstanceVCToolsetVersion(chosenInstanceInfo)) {
  364. return false;
  365. }
  366. chosenInstanceInfo.IsWin10SDKInstalled = true;
  367. chosenInstanceInfo.IsWin81SDKInstalled = !envWindowsSdkDir81.empty();
  368. return true;
  369. }
  370. std::string envVSCommonToolsDir;
  371. std::string envVSCommonToolsDirEnvName =
  372. "VS" + std::to_string(this->Version) + "0COMNTOOLS";
  373. if (cmSystemTools::GetEnv(envVSCommonToolsDirEnvName.c_str(),
  374. envVSCommonToolsDir)) {
  375. cmSystemTools::ConvertToUnixSlashes(envVSCommonToolsDir);
  376. }
  377. std::string const wantVersion = std::to_string(this->Version) + '.';
  378. bool specifiedLocationNotSpecifiedVersion = false;
  379. SmartCOMPtr<ISetupInstance> instance;
  380. std::vector<VSInstanceInfo> vecVSInstancesAll;
  381. // Enumerate VS instances with either COM interface or Vswhere
  382. if (!EnumerateVSInstancesWithCOM(vecVSInstancesAll) &&
  383. !EnumerateVSInstancesWithVswhere(vecVSInstancesAll)) {
  384. return false;
  385. }
  386. std::vector<VSInstanceInfo> vecVSInstances;
  387. for (const auto& instanceInfo : vecVSInstancesAll) {
  388. // We are looking for a specific major version.
  389. if (instanceInfo.Version.size() < wantVersion.size() ||
  390. instanceInfo.Version.substr(0, wantVersion.size()) != wantVersion) {
  391. continue;
  392. }
  393. if (!this->SpecifiedVSInstallLocation.empty()) {
  394. // We are looking for a specific instance.
  395. std::string currentVSLocation = instanceInfo.GetInstallLocation();
  396. if (cmSystemTools::ComparePath(currentVSLocation,
  397. this->SpecifiedVSInstallLocation)) {
  398. if (this->SpecifiedVSInstallVersion.empty() ||
  399. instanceInfo.Version == this->SpecifiedVSInstallVersion) {
  400. chosenInstanceInfo = instanceInfo;
  401. return true;
  402. }
  403. specifiedLocationNotSpecifiedVersion = true;
  404. }
  405. } else if (!this->SpecifiedVSInstallVersion.empty()) {
  406. // We are looking for a specific version.
  407. if (instanceInfo.Version == this->SpecifiedVSInstallVersion) {
  408. chosenInstanceInfo = instanceInfo;
  409. return true;
  410. }
  411. } else {
  412. // We are not looking for a specific instance.
  413. // If we've been given a hint then use it.
  414. if (!envVSCommonToolsDir.empty()) {
  415. std::string currentVSLocation =
  416. cmStrCat(instanceInfo.GetInstallLocation(), "/Common7/Tools");
  417. if (cmSystemTools::ComparePath(currentVSLocation,
  418. envVSCommonToolsDir)) {
  419. chosenInstanceInfo = instanceInfo;
  420. return true;
  421. }
  422. }
  423. // Otherwise, add this to the list of candidates.
  424. vecVSInstances.push_back(instanceInfo);
  425. }
  426. }
  427. if (!this->SpecifiedVSInstallLocation.empty() &&
  428. !specifiedLocationNotSpecifiedVersion) {
  429. // The VS Installer does not know about the specified location.
  430. // Check for one directly on disk.
  431. return this->LoadSpecifiedVSInstanceFromDisk();
  432. }
  433. if (vecVSInstances.size() > 0) {
  434. isVSInstanceExists = true;
  435. int index = ChooseVSInstance(vecVSInstances);
  436. chosenInstanceInfo = vecVSInstances[index];
  437. }
  438. return isVSInstanceExists;
  439. }
  440. int cmVSSetupAPIHelper::ChooseVSInstance(
  441. const std::vector<VSInstanceInfo>& vecVSInstances)
  442. {
  443. if (vecVSInstances.size() == 0)
  444. return -1;
  445. if (vecVSInstances.size() == 1)
  446. return 0;
  447. unsigned int chosenIndex = 0;
  448. for (unsigned int i = 1; i < vecVSInstances.size(); i++) {
  449. // If the current has Win10 SDK but not the chosen one, then choose the
  450. // current VS instance
  451. if (!vecVSInstances[chosenIndex].IsWin10SDKInstalled &&
  452. vecVSInstances[i].IsWin10SDKInstalled) {
  453. chosenIndex = i;
  454. continue;
  455. }
  456. // If the chosen one has Win10 SDK but the current one is not, then look at
  457. // the next VS instance even the current
  458. // instance version may be higher
  459. if (vecVSInstances[chosenIndex].IsWin10SDKInstalled &&
  460. !vecVSInstances[i].IsWin10SDKInstalled) {
  461. continue;
  462. }
  463. // If both chosen one and current one doesn't have Win10 SDK but the
  464. // current one has Win8.1 SDK installed,
  465. // then choose the current one
  466. if (!vecVSInstances[chosenIndex].IsWin10SDKInstalled &&
  467. !vecVSInstances[i].IsWin10SDKInstalled &&
  468. !vecVSInstances[chosenIndex].IsWin81SDKInstalled &&
  469. vecVSInstances[i].IsWin81SDKInstalled) {
  470. chosenIndex = i;
  471. continue;
  472. }
  473. // If there is no difference in WinSDKs then look for the highest version
  474. // of installed VS
  475. if ((vecVSInstances[chosenIndex].IsWin10SDKInstalled ==
  476. vecVSInstances[i].IsWin10SDKInstalled) &&
  477. (vecVSInstances[chosenIndex].IsWin81SDKInstalled ==
  478. vecVSInstances[i].IsWin81SDKInstalled) &&
  479. vecVSInstances[chosenIndex].Version < vecVSInstances[i].Version) {
  480. chosenIndex = i;
  481. continue;
  482. }
  483. }
  484. return chosenIndex;
  485. }
  486. bool cmVSSetupAPIHelper::LoadSpecifiedVSInstanceFromDisk()
  487. {
  488. if (!cmSystemTools::FileIsDirectory(this->SpecifiedVSInstallLocation)) {
  489. return false;
  490. }
  491. VSInstanceInfo vsInstanceInfo;
  492. vsInstanceInfo.VSInstallLocation = this->SpecifiedVSInstallLocation;
  493. // FIXME: Is there a better way to get SDK information?
  494. vsInstanceInfo.IsWin10SDKInstalled = true;
  495. vsInstanceInfo.IsWin81SDKInstalled = false;
  496. if (!this->SpecifiedVSInstallVersion.empty()) {
  497. // Assume the version specified by the user is correct.
  498. vsInstanceInfo.Version = this->SpecifiedVSInstallVersion;
  499. } else {
  500. return false;
  501. }
  502. if (!LoadVSInstanceVCToolsetVersion(vsInstanceInfo)) {
  503. return false;
  504. }
  505. chosenInstanceInfo = std::move(vsInstanceInfo);
  506. return true;
  507. }
  508. bool cmVSSetupAPIHelper::Initialize()
  509. {
  510. if (initializationFailure)
  511. return false;
  512. if (FAILED(comInitialized)) {
  513. initializationFailure = true;
  514. return false;
  515. }
  516. if (FAILED(setupConfig.CoCreateInstance(CLSID_SetupConfiguration, nullptr,
  517. IID_ISetupConfiguration,
  518. CLSCTX_INPROC_SERVER)) ||
  519. setupConfig == nullptr) {
  520. initializationFailure = true;
  521. return false;
  522. }
  523. if (FAILED(setupConfig.QueryInterface(IID_ISetupConfiguration2,
  524. (void**)&setupConfig2)) ||
  525. setupConfig2 == nullptr) {
  526. initializationFailure = true;
  527. return false;
  528. }
  529. if (FAILED(
  530. setupConfig.QueryInterface(IID_ISetupHelper, (void**)&setupHelper)) ||
  531. setupHelper == nullptr) {
  532. initializationFailure = true;
  533. return false;
  534. }
  535. initializationFailure = false;
  536. return true;
  537. }