cmVSSetupHelper.cxx 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621
  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. }
  168. vsInstanceInfo.Version =
  169. cmsys::Encoding::ToNarrow(std::wstring(bstrVersion));
  170. // Reboot may have been required before the installation path was created.
  171. SmartBSTR bstrInstallationPath;
  172. if ((eLocal & state) == eLocal) {
  173. if (FAILED(pInstance->GetInstallationPath(&bstrInstallationPath))) {
  174. return false;
  175. }
  176. vsInstanceInfo.VSInstallLocation =
  177. cmsys::Encoding::ToNarrow(std::wstring(bstrInstallationPath));
  178. cmSystemTools::ConvertToUnixSlashes(vsInstanceInfo.VSInstallLocation);
  179. }
  180. // Check if a compiler is installed with this instance.
  181. if (!LoadVSInstanceVCToolsetVersion(vsInstanceInfo)) {
  182. return false;
  183. }
  184. // Reboot may have been required before the product package was registered
  185. // (last).
  186. if ((eRegistered & state) == eRegistered) {
  187. SmartCOMPtr<ISetupPackageReference> product;
  188. if (FAILED(pInstance->GetProduct(&product)) || !product) {
  189. return false;
  190. }
  191. LPSAFEARRAY lpsaPackages;
  192. if (FAILED(pInstance->GetPackages(&lpsaPackages)) ||
  193. lpsaPackages == nullptr) {
  194. return false;
  195. }
  196. int lower = lpsaPackages->rgsabound[0].lLbound;
  197. int upper = lpsaPackages->rgsabound[0].cElements + lower;
  198. IUnknown** ppData = (IUnknown**)lpsaPackages->pvData;
  199. for (int i = lower; i < upper; i++) {
  200. SmartCOMPtr<ISetupPackageReference> package = nullptr;
  201. if (FAILED(ppData[i]->QueryInterface(IID_ISetupPackageReference,
  202. (void**)&package)) ||
  203. package == nullptr)
  204. continue;
  205. bool win10SDKInstalled = false;
  206. bool win81SDkInstalled = false;
  207. bool ret =
  208. CheckInstalledComponent(package, win10SDKInstalled, win81SDkInstalled);
  209. if (ret) {
  210. vsInstanceInfo.IsWin10SDKInstalled |= win10SDKInstalled;
  211. vsInstanceInfo.IsWin81SDKInstalled |= win81SDkInstalled;
  212. }
  213. }
  214. SafeArrayDestroy(lpsaPackages);
  215. }
  216. return true;
  217. }
  218. bool cmVSSetupAPIHelper::GetVSInstanceInfo(std::string& vsInstallLocation)
  219. {
  220. vsInstallLocation.clear();
  221. bool isInstalled = this->EnumerateAndChooseVSInstance();
  222. if (isInstalled) {
  223. vsInstallLocation = chosenInstanceInfo.GetInstallLocation();
  224. }
  225. return isInstalled;
  226. }
  227. bool cmVSSetupAPIHelper::GetVSInstanceVersion(std::string& vsInstanceVersion)
  228. {
  229. vsInstanceVersion.clear();
  230. bool isInstalled = this->EnumerateAndChooseVSInstance();
  231. if (isInstalled) {
  232. vsInstanceVersion = chosenInstanceInfo.Version;
  233. }
  234. return isInstalled;
  235. }
  236. bool cmVSSetupAPIHelper::GetVCToolsetVersion(std::string& vsToolsetVersion)
  237. {
  238. vsToolsetVersion.clear();
  239. bool isInstalled = this->EnumerateAndChooseVSInstance();
  240. if (isInstalled) {
  241. vsToolsetVersion = chosenInstanceInfo.VCToolsetVersion;
  242. }
  243. return isInstalled && !vsToolsetVersion.empty();
  244. }
  245. bool cmVSSetupAPIHelper::IsEWDKEnabled()
  246. {
  247. std::string envEnterpriseWDK, envDisableRegistryUse;
  248. cmSystemTools::GetEnv("EnterpriseWDK", envEnterpriseWDK);
  249. cmSystemTools::GetEnv("DisableRegistryUse", envDisableRegistryUse);
  250. if (!cmSystemTools::Strucmp(envEnterpriseWDK.c_str(), "True") &&
  251. !cmSystemTools::Strucmp(envDisableRegistryUse.c_str(), "True")) {
  252. return true;
  253. }
  254. return false;
  255. }
  256. #if !defined(CMAKE_BOOTSTRAP)
  257. namespace {
  258. std::string FindVsWhereCommand()
  259. {
  260. std::string vswhere;
  261. static const char* programFiles[] = { "ProgramFiles(x86)", "ProgramFiles" };
  262. for (const char* pf : programFiles) {
  263. if (cmSystemTools::GetEnv(pf, vswhere)) {
  264. vswhere += "/Microsoft Visual Studio/Installer/vswhere.exe";
  265. if (cmSystemTools::FileExists(vswhere)) {
  266. return vswhere;
  267. }
  268. }
  269. }
  270. vswhere = "vswhere.exe";
  271. return vswhere;
  272. }
  273. }
  274. #endif
  275. bool cmVSSetupAPIHelper::EnumerateVSInstancesWithVswhere(
  276. std::vector<VSInstanceInfo>& VSInstances)
  277. {
  278. #if !defined(CMAKE_BOOTSTRAP)
  279. // Construct vswhere command to get installed VS instances in JSON format
  280. std::string vswhereExe = FindVsWhereCommand();
  281. std::vector<std::string> vswhereCmd = { vswhereExe, "-format", "json" };
  282. // Execute vswhere command and capture JSON output
  283. std::string json_output;
  284. int retVal = 1;
  285. if (!cmSystemTools::RunSingleCommand(vswhereCmd, &json_output, &json_output,
  286. &retVal, nullptr,
  287. cmSystemTools::OUTPUT_NONE)) {
  288. return false;
  289. }
  290. // Parse JSON output and iterate over elements
  291. Json::CharReaderBuilder builder;
  292. auto jsonReader = std::unique_ptr<Json::CharReader>(builder.newCharReader());
  293. Json::Value json;
  294. std::string error;
  295. if (!jsonReader->parse(json_output.data(),
  296. json_output.data() + json_output.size(), &json,
  297. &error)) {
  298. return false;
  299. }
  300. for (const auto& item : json) {
  301. VSInstanceInfo instance;
  302. instance.Version = item["installationVersion"].asString();
  303. instance.VSInstallLocation = item["installationPath"].asString();
  304. instance.IsWin10SDKInstalled = true;
  305. instance.IsWin81SDKInstalled = false;
  306. cmSystemTools::ConvertToUnixSlashes(instance.VSInstallLocation);
  307. if (LoadVSInstanceVCToolsetVersion(instance)) {
  308. VSInstances.push_back(instance);
  309. }
  310. }
  311. return true;
  312. #else
  313. static_cast<void>(VSInstances);
  314. return false;
  315. #endif
  316. }
  317. bool cmVSSetupAPIHelper::EnumerateVSInstancesWithCOM(
  318. std::vector<VSInstanceInfo>& VSInstances)
  319. {
  320. if (initializationFailure || setupConfig == nullptr ||
  321. setupConfig2 == nullptr || setupHelper == nullptr)
  322. return false;
  323. SmartCOMPtr<IEnumSetupInstances> enumInstances = nullptr;
  324. if (FAILED(
  325. setupConfig2->EnumInstances((IEnumSetupInstances**)&enumInstances)) ||
  326. !enumInstances) {
  327. return false;
  328. }
  329. SmartCOMPtr<ISetupInstance> instance;
  330. while (SUCCEEDED(enumInstances->Next(1, &instance, nullptr)) && instance) {
  331. SmartCOMPtr<ISetupInstance2> instance2 = nullptr;
  332. if (FAILED(
  333. instance->QueryInterface(IID_ISetupInstance2, (void**)&instance2)) ||
  334. !instance2) {
  335. instance = nullptr;
  336. continue;
  337. }
  338. VSInstanceInfo instanceInfo;
  339. bool isInstalled = GetVSInstanceInfo(instance2, instanceInfo);
  340. instance = instance2 = nullptr;
  341. if (isInstalled)
  342. VSInstances.push_back(instanceInfo);
  343. }
  344. return true;
  345. }
  346. bool cmVSSetupAPIHelper::EnumerateAndChooseVSInstance()
  347. {
  348. bool isVSInstanceExists = false;
  349. if (chosenInstanceInfo.VSInstallLocation.compare("") != 0) {
  350. return true;
  351. }
  352. if (this->IsEWDKEnabled()) {
  353. std::string envWindowsSdkDir81, envVSVersion, envVsInstallDir;
  354. cmSystemTools::GetEnv("WindowsSdkDir_81", envWindowsSdkDir81);
  355. cmSystemTools::GetEnv("VisualStudioVersion", envVSVersion);
  356. cmSystemTools::GetEnv("VSINSTALLDIR", envVsInstallDir);
  357. if (envVSVersion.empty() || envVsInstallDir.empty())
  358. return false;
  359. chosenInstanceInfo.VSInstallLocation = envVsInstallDir;
  360. chosenInstanceInfo.Version = envVSVersion;
  361. if (!LoadVSInstanceVCToolsetVersion(chosenInstanceInfo)) {
  362. return false;
  363. }
  364. chosenInstanceInfo.IsWin10SDKInstalled = true;
  365. chosenInstanceInfo.IsWin81SDKInstalled = !envWindowsSdkDir81.empty();
  366. return true;
  367. }
  368. std::string envVSCommonToolsDir;
  369. std::string envVSCommonToolsDirEnvName =
  370. "VS" + std::to_string(this->Version) + "0COMNTOOLS";
  371. if (cmSystemTools::GetEnv(envVSCommonToolsDirEnvName.c_str(),
  372. envVSCommonToolsDir)) {
  373. cmSystemTools::ConvertToUnixSlashes(envVSCommonToolsDir);
  374. }
  375. std::string const wantVersion = std::to_string(this->Version) + '.';
  376. bool specifiedLocationNotSpecifiedVersion = false;
  377. SmartCOMPtr<ISetupInstance> instance;
  378. std::vector<VSInstanceInfo> vecVSInstancesAll;
  379. // Enumerate VS instances with either COM interface or Vswhere
  380. if (!EnumerateVSInstancesWithCOM(vecVSInstancesAll) &&
  381. !EnumerateVSInstancesWithVswhere(vecVSInstancesAll)) {
  382. return false;
  383. }
  384. std::vector<VSInstanceInfo> vecVSInstances;
  385. for (const auto& instanceInfo : vecVSInstancesAll) {
  386. // We are looking for a specific major version.
  387. if (instanceInfo.Version.size() < wantVersion.size() ||
  388. instanceInfo.Version.substr(0, wantVersion.size()) != wantVersion) {
  389. continue;
  390. }
  391. if (!this->SpecifiedVSInstallLocation.empty()) {
  392. // We are looking for a specific instance.
  393. std::string currentVSLocation = instanceInfo.GetInstallLocation();
  394. if (cmSystemTools::ComparePath(currentVSLocation,
  395. this->SpecifiedVSInstallLocation)) {
  396. if (this->SpecifiedVSInstallVersion.empty() ||
  397. instanceInfo.Version == this->SpecifiedVSInstallVersion) {
  398. chosenInstanceInfo = instanceInfo;
  399. return true;
  400. }
  401. specifiedLocationNotSpecifiedVersion = true;
  402. }
  403. } else if (!this->SpecifiedVSInstallVersion.empty()) {
  404. // We are looking for a specific version.
  405. if (instanceInfo.Version == this->SpecifiedVSInstallVersion) {
  406. chosenInstanceInfo = instanceInfo;
  407. return true;
  408. }
  409. } else {
  410. // We are not looking for a specific instance.
  411. // If we've been given a hint then use it.
  412. if (!envVSCommonToolsDir.empty()) {
  413. std::string currentVSLocation =
  414. cmStrCat(instanceInfo.GetInstallLocation(), "/Common7/Tools");
  415. if (cmSystemTools::ComparePath(currentVSLocation,
  416. envVSCommonToolsDir)) {
  417. chosenInstanceInfo = instanceInfo;
  418. return true;
  419. }
  420. }
  421. // Otherwise, add this to the list of candidates.
  422. vecVSInstances.push_back(instanceInfo);
  423. }
  424. }
  425. if (!this->SpecifiedVSInstallLocation.empty() &&
  426. !specifiedLocationNotSpecifiedVersion) {
  427. // The VS Installer does not know about the specified location.
  428. // Check for one directly on disk.
  429. return this->LoadSpecifiedVSInstanceFromDisk();
  430. }
  431. if (!vecVSInstances.empty()) {
  432. isVSInstanceExists = true;
  433. int index = ChooseVSInstance(vecVSInstances);
  434. chosenInstanceInfo = vecVSInstances[index];
  435. }
  436. return isVSInstanceExists;
  437. }
  438. int cmVSSetupAPIHelper::ChooseVSInstance(
  439. const std::vector<VSInstanceInfo>& vecVSInstances)
  440. {
  441. if (vecVSInstances.empty())
  442. return -1;
  443. if (vecVSInstances.size() == 1)
  444. return 0;
  445. unsigned int chosenIndex = 0;
  446. for (unsigned int i = 1; i < vecVSInstances.size(); i++) {
  447. // If the current has Win10 SDK but not the chosen one, then choose the
  448. // current VS instance
  449. if (!vecVSInstances[chosenIndex].IsWin10SDKInstalled &&
  450. vecVSInstances[i].IsWin10SDKInstalled) {
  451. chosenIndex = i;
  452. continue;
  453. }
  454. // If the chosen one has Win10 SDK but the current one is not, then look at
  455. // the next VS instance even the current
  456. // instance version may be higher
  457. if (vecVSInstances[chosenIndex].IsWin10SDKInstalled &&
  458. !vecVSInstances[i].IsWin10SDKInstalled) {
  459. continue;
  460. }
  461. // If both chosen one and current one doesn't have Win10 SDK but the
  462. // current one has Win8.1 SDK installed,
  463. // then choose the current one
  464. if (!vecVSInstances[chosenIndex].IsWin10SDKInstalled &&
  465. !vecVSInstances[i].IsWin10SDKInstalled &&
  466. !vecVSInstances[chosenIndex].IsWin81SDKInstalled &&
  467. vecVSInstances[i].IsWin81SDKInstalled) {
  468. chosenIndex = i;
  469. continue;
  470. }
  471. // If there is no difference in WinSDKs then look for the highest version
  472. // of installed VS
  473. if ((vecVSInstances[chosenIndex].IsWin10SDKInstalled ==
  474. vecVSInstances[i].IsWin10SDKInstalled) &&
  475. (vecVSInstances[chosenIndex].IsWin81SDKInstalled ==
  476. vecVSInstances[i].IsWin81SDKInstalled) &&
  477. vecVSInstances[chosenIndex].Version < vecVSInstances[i].Version) {
  478. chosenIndex = i;
  479. continue;
  480. }
  481. }
  482. return chosenIndex;
  483. }
  484. bool cmVSSetupAPIHelper::LoadSpecifiedVSInstanceFromDisk()
  485. {
  486. if (!cmSystemTools::FileIsDirectory(this->SpecifiedVSInstallLocation)) {
  487. return false;
  488. }
  489. VSInstanceInfo vsInstanceInfo;
  490. vsInstanceInfo.VSInstallLocation = this->SpecifiedVSInstallLocation;
  491. // FIXME: Is there a better way to get SDK information?
  492. vsInstanceInfo.IsWin10SDKInstalled = true;
  493. vsInstanceInfo.IsWin81SDKInstalled = false;
  494. if (!this->SpecifiedVSInstallVersion.empty()) {
  495. // Assume the version specified by the user is correct.
  496. vsInstanceInfo.Version = this->SpecifiedVSInstallVersion;
  497. } else {
  498. return false;
  499. }
  500. if (!LoadVSInstanceVCToolsetVersion(vsInstanceInfo)) {
  501. return false;
  502. }
  503. chosenInstanceInfo = std::move(vsInstanceInfo);
  504. return true;
  505. }
  506. bool cmVSSetupAPIHelper::Initialize()
  507. {
  508. if (initializationFailure)
  509. return false;
  510. if (FAILED(comInitialized)) {
  511. initializationFailure = true;
  512. return false;
  513. }
  514. if (FAILED(setupConfig.CoCreateInstance(CLSID_SetupConfiguration, nullptr,
  515. IID_ISetupConfiguration,
  516. CLSCTX_INPROC_SERVER)) ||
  517. setupConfig == nullptr) {
  518. initializationFailure = true;
  519. return false;
  520. }
  521. if (FAILED(setupConfig.QueryInterface(IID_ISetupConfiguration2,
  522. (void**)&setupConfig2)) ||
  523. setupConfig2 == nullptr) {
  524. initializationFailure = true;
  525. return false;
  526. }
  527. if (FAILED(
  528. setupConfig.QueryInterface(IID_ISetupHelper, (void**)&setupHelper)) ||
  529. setupHelper == nullptr) {
  530. initializationFailure = true;
  531. return false;
  532. }
  533. initializationFailure = false;
  534. return true;
  535. }