|
| 1 | +// Copyright 2017 - Refael Ackermann |
| 2 | +// Distributed under MIT style license |
| 3 | +// See accompanying file LICENSE at https://github.com/node4good/windows-autoconf |
| 4 | + |
| 5 | +// Usage: |
| 6 | +// powershell -ExecutionPolicy Unrestricted -Version "2.0" -Command "&{Add-Type -Path Find-VS2017.cs; [VisualStudioConfiguration.Main]::Query()}" |
| 7 | +using System; |
| 8 | +using System.Text; |
| 9 | +using System.Runtime.InteropServices; |
| 10 | + |
| 11 | +namespace VisualStudioConfiguration |
| 12 | +{ |
| 13 | + [Flags] |
| 14 | + public enum InstanceState : uint |
| 15 | + { |
| 16 | + None = 0, |
| 17 | + Local = 1, |
| 18 | + Registered = 2, |
| 19 | + NoRebootRequired = 4, |
| 20 | + NoErrors = 8, |
| 21 | + Complete = 4294967295, |
| 22 | + } |
| 23 | + |
| 24 | + [Guid("6380BCFF-41D3-4B2E-8B2E-BF8A6810C848")] |
| 25 | + [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)] |
| 26 | + [ComImport] |
| 27 | + public interface IEnumSetupInstances |
| 28 | + { |
| 29 | + |
| 30 | + void Next([MarshalAs(UnmanagedType.U4), In] int celt, |
| 31 | + [MarshalAs(UnmanagedType.LPArray, ArraySubType = UnmanagedType.Interface), Out] ISetupInstance[] rgelt, |
| 32 | + [MarshalAs(UnmanagedType.U4)] out int pceltFetched); |
| 33 | + |
| 34 | + void Skip([MarshalAs(UnmanagedType.U4), In] int celt); |
| 35 | + |
| 36 | + void Reset(); |
| 37 | + |
| 38 | + [return: MarshalAs(UnmanagedType.Interface)] |
| 39 | + IEnumSetupInstances Clone(); |
| 40 | + } |
| 41 | + |
| 42 | + [Guid("42843719-DB4C-46C2-8E7C-64F1816EFD5B")] |
| 43 | + [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)] |
| 44 | + [ComImport] |
| 45 | + public interface ISetupConfiguration |
| 46 | + { |
| 47 | + } |
| 48 | + |
| 49 | + [Guid("26AAB78C-4A60-49D6-AF3B-3C35BC93365D")] |
| 50 | + [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)] |
| 51 | + [ComImport] |
| 52 | + public interface ISetupConfiguration2 : ISetupConfiguration |
| 53 | + { |
| 54 | + |
| 55 | + [return: MarshalAs(UnmanagedType.Interface)] |
| 56 | + IEnumSetupInstances EnumInstances(); |
| 57 | + |
| 58 | + [return: MarshalAs(UnmanagedType.Interface)] |
| 59 | + ISetupInstance GetInstanceForCurrentProcess(); |
| 60 | + |
| 61 | + [return: MarshalAs(UnmanagedType.Interface)] |
| 62 | + ISetupInstance GetInstanceForPath([MarshalAs(UnmanagedType.LPWStr), In] string path); |
| 63 | + |
| 64 | + [return: MarshalAs(UnmanagedType.Interface)] |
| 65 | + IEnumSetupInstances EnumAllInstances(); |
| 66 | + } |
| 67 | + |
| 68 | + [Guid("B41463C3-8866-43B5-BC33-2B0676F7F42E")] |
| 69 | + [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)] |
| 70 | + [ComImport] |
| 71 | + public interface ISetupInstance |
| 72 | + { |
| 73 | + } |
| 74 | + |
| 75 | + [Guid("89143C9A-05AF-49B0-B717-72E218A2185C")] |
| 76 | + [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)] |
| 77 | + [ComImport] |
| 78 | + public interface ISetupInstance2 : ISetupInstance |
| 79 | + { |
| 80 | + [return: MarshalAs(UnmanagedType.BStr)] |
| 81 | + string GetInstanceId(); |
| 82 | + |
| 83 | + [return: MarshalAs(UnmanagedType.Struct)] |
| 84 | + System.Runtime.InteropServices.ComTypes.FILETIME GetInstallDate(); |
| 85 | + |
| 86 | + [return: MarshalAs(UnmanagedType.BStr)] |
| 87 | + string GetInstallationName(); |
| 88 | + |
| 89 | + [return: MarshalAs(UnmanagedType.BStr)] |
| 90 | + string GetInstallationPath(); |
| 91 | + |
| 92 | + [return: MarshalAs(UnmanagedType.BStr)] |
| 93 | + string GetInstallationVersion(); |
| 94 | + |
| 95 | + [return: MarshalAs(UnmanagedType.BStr)] |
| 96 | + string GetDisplayName([MarshalAs(UnmanagedType.U4), In] int lcid); |
| 97 | + |
| 98 | + [return: MarshalAs(UnmanagedType.BStr)] |
| 99 | + string GetDescription([MarshalAs(UnmanagedType.U4), In] int lcid); |
| 100 | + |
| 101 | + [return: MarshalAs(UnmanagedType.BStr)] |
| 102 | + string ResolvePath([MarshalAs(UnmanagedType.LPWStr), In] string pwszRelativePath); |
| 103 | + |
| 104 | + [return: MarshalAs(UnmanagedType.U4)] |
| 105 | + InstanceState GetState(); |
| 106 | + |
| 107 | + [return: MarshalAs(UnmanagedType.SafeArray, SafeArraySubType = VarEnum.VT_UNKNOWN)] |
| 108 | + ISetupPackageReference[] GetPackages(); |
| 109 | + |
| 110 | + ISetupPackageReference GetProduct(); |
| 111 | + |
| 112 | + [return: MarshalAs(UnmanagedType.BStr)] |
| 113 | + string GetProductPath(); |
| 114 | + |
| 115 | + [return: MarshalAs(UnmanagedType.VariantBool)] |
| 116 | + bool IsLaunchable(); |
| 117 | + |
| 118 | + [return: MarshalAs(UnmanagedType.VariantBool)] |
| 119 | + bool IsComplete(); |
| 120 | + |
| 121 | + [return: MarshalAs(UnmanagedType.SafeArray, SafeArraySubType = VarEnum.VT_UNKNOWN)] |
| 122 | + ISetupPropertyStore GetProperties(); |
| 123 | + |
| 124 | + [return: MarshalAs(UnmanagedType.BStr)] |
| 125 | + string GetEnginePath(); |
| 126 | + } |
| 127 | + |
| 128 | + [Guid("DA8D8A16-B2B6-4487-A2F1-594CCCCD6BF5")] |
| 129 | + [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)] |
| 130 | + [ComImport] |
| 131 | + public interface ISetupPackageReference |
| 132 | + { |
| 133 | + |
| 134 | + [return: MarshalAs(UnmanagedType.BStr)] |
| 135 | + string GetId(); |
| 136 | + |
| 137 | + [return: MarshalAs(UnmanagedType.BStr)] |
| 138 | + string GetVersion(); |
| 139 | + |
| 140 | + [return: MarshalAs(UnmanagedType.BStr)] |
| 141 | + string GetChip(); |
| 142 | + |
| 143 | + [return: MarshalAs(UnmanagedType.BStr)] |
| 144 | + string GetLanguage(); |
| 145 | + |
| 146 | + [return: MarshalAs(UnmanagedType.BStr)] |
| 147 | + string GetBranch(); |
| 148 | + |
| 149 | + [return: MarshalAs(UnmanagedType.BStr)] |
| 150 | + string GetType(); |
| 151 | + |
| 152 | + [return: MarshalAs(UnmanagedType.BStr)] |
| 153 | + string GetUniqueId(); |
| 154 | + |
| 155 | + [return: MarshalAs(UnmanagedType.VariantBool)] |
| 156 | + bool GetIsExtension(); |
| 157 | + } |
| 158 | + |
| 159 | + [Guid("c601c175-a3be-44bc-91f6-4568d230fc83")] |
| 160 | + [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)] |
| 161 | + [ComImport] |
| 162 | + public interface ISetupPropertyStore |
| 163 | + { |
| 164 | + |
| 165 | + [return: MarshalAs(UnmanagedType.SafeArray, SafeArraySubType = VarEnum.VT_BSTR)] |
| 166 | + string[] GetNames(); |
| 167 | + |
| 168 | + object GetValue([MarshalAs(UnmanagedType.LPWStr), In] string pwszName); |
| 169 | + } |
| 170 | + |
| 171 | + [Guid("42843719-DB4C-46C2-8E7C-64F1816EFD5B")] |
| 172 | + [CoClass(typeof(SetupConfigurationClass))] |
| 173 | + [ComImport] |
| 174 | + public interface SetupConfiguration : ISetupConfiguration2, ISetupConfiguration |
| 175 | + { |
| 176 | + } |
| 177 | + |
| 178 | + [Guid("177F0C4A-1CD3-4DE7-A32C-71DBBB9FA36D")] |
| 179 | + [ClassInterface(ClassInterfaceType.None)] |
| 180 | + [ComImport] |
| 181 | + public class SetupConfigurationClass |
| 182 | + { |
| 183 | + } |
| 184 | + |
| 185 | + public static class Main |
| 186 | + { |
| 187 | + public static void Query() |
| 188 | + { |
| 189 | + ISetupConfiguration query = new SetupConfiguration(); |
| 190 | + ISetupConfiguration2 query2 = (ISetupConfiguration2)query; |
| 191 | + IEnumSetupInstances e = query2.EnumAllInstances(); |
| 192 | + |
| 193 | + int pceltFetched; |
| 194 | + ISetupInstance2[] rgelt = new ISetupInstance2[1]; |
| 195 | + while (true) |
| 196 | + { |
| 197 | + e.Next(1, rgelt, out pceltFetched); |
| 198 | + if (pceltFetched <= 0) |
| 199 | + { |
| 200 | + Console.WriteLine("No usable installation of VS2017 found"); |
| 201 | + return; |
| 202 | + } |
| 203 | + if (CheckInstance(rgelt[0])) |
| 204 | + return; |
| 205 | + } |
| 206 | + } |
| 207 | + |
| 208 | + private static bool CheckInstance(ISetupInstance2 setupInstance2) |
| 209 | + { |
| 210 | + // Visual Studio Community 2017 component directory: |
| 211 | + // https://www.visualstudio.com/en-us/productinfo/vs2017-install-product-Community.workloads |
| 212 | + |
| 213 | + string path = setupInstance2.GetInstallationPath(); |
| 214 | + Console.WriteLine(String.Format("Found VS2017 installation at: {0}", path)); |
| 215 | + |
| 216 | + bool hasMSBuild = false; |
| 217 | + bool hasVCTools = false; |
| 218 | + uint Win10SDKVer = 0; |
| 219 | + bool hasWin8SDK = false; |
| 220 | + |
| 221 | + foreach (ISetupPackageReference package in setupInstance2.GetPackages()) |
| 222 | + { |
| 223 | + const string Win10SDKPrefix = "Microsoft.VisualStudio.Component.Windows10SDK."; |
| 224 | + |
| 225 | + string id = package.GetId(); |
| 226 | + if (id == "Microsoft.VisualStudio.VC.MSBuild.Base") |
| 227 | + hasMSBuild = true; |
| 228 | + else if (id == "Microsoft.VisualStudio.Component.VC.Tools.x86.x64") |
| 229 | + hasVCTools = true; |
| 230 | + else if (id.StartsWith(Win10SDKPrefix)) |
| 231 | + Win10SDKVer = Math.Max(Win10SDKVer, UInt32.Parse(id.Substring(Win10SDKPrefix.Length))); |
| 232 | + else if (id == "Microsoft.VisualStudio.Component.Windows81SDK") |
| 233 | + hasWin8SDK = true; |
| 234 | + else |
| 235 | + continue; |
| 236 | + |
| 237 | + Console.WriteLine(String.Format(" - Found {0}", id)); |
| 238 | + } |
| 239 | + |
| 240 | + if (!hasMSBuild) |
| 241 | + Console.WriteLine(" - Missing Visual Studio C++ core features (Microsoft.VisualStudio.VC.MSBuild.Base)"); |
| 242 | + if (!hasVCTools) |
| 243 | + Console.WriteLine(" - Missing VC++ 2017 v141 toolset (x86,x64) (Microsoft.VisualStudio.Component.VC.Tools.x86.x64)"); |
| 244 | + if ((Win10SDKVer == 0) && (!hasWin8SDK)) |
| 245 | + Console.WriteLine(" - Missing a Windows SDK (Microsoft.VisualStudio.Component.Windows10SDK.* or Microsoft.VisualStudio.Component.Windows81SDK)"); |
| 246 | + |
| 247 | + if (hasMSBuild && hasVCTools) |
| 248 | + { |
| 249 | + if (Win10SDKVer > 0) |
| 250 | + { |
| 251 | + Console.WriteLine(" - Using this installation with Windows 10 SDK"); |
| 252 | + string[] lines = { String.Format("set \"VS2017_INSTALL={0}\"", path), String.Format("set \"VS2017_SDK=10.0.{0}.0\"", Win10SDKVer) }; |
| 253 | + System.IO.File.WriteAllLines(@"Set_VS2017.bat", lines); |
| 254 | + return true; |
| 255 | + } |
| 256 | + else if (hasWin8SDK) |
| 257 | + { |
| 258 | + Console.WriteLine(" - Using this installation with Windows 8.1 SDK"); |
| 259 | + string[] lines = { String.Format("set \"VS2017_INSTALL={0}\"", path), "set \"VS2017_SDK=8.1\"" }; |
| 260 | + System.IO.File.WriteAllLines(@"Set_VS2017.bat", lines); |
| 261 | + return true; |
| 262 | + } |
| 263 | + } |
| 264 | + |
| 265 | + Console.WriteLine(" - Some required components are missing, not using this installation"); |
| 266 | + return false; |
| 267 | + } |
| 268 | + } |
| 269 | +} |
0 commit comments