| 1 | // Copyright (c) .NET Foundation and contributors. All rights reserved. Licensed under the Microsoft Reciprocal License. See LICENSE.TXT file in the project root for full license information. |
| 2 | |
| 3 | namespace WixToolset.BootstrapperApplicationApi |
| 4 | { |
| 5 | using System; |
| 6 | using System.Runtime.InteropServices; |
| 7 | using System.Text; |
| 8 | |
| 9 | /// <summary> |
| 10 | /// Managed wrapper for verutil. |
| 11 | /// </summary> |
| 12 | public static class VerUtil |
| 13 | { |
| 14 | [DllImport("mbanative.dll", ExactSpelling = true, PreserveSig = false)] |
| 15 | internal static extern int VerCompareParsedVersions( |
| 16 | VersionHandle pVersion1, |
| 17 | VersionHandle pVersion2 |
| 18 | ); |
| 19 | |
| 20 | [DllImport("mbanative.dll", ExactSpelling = true, PreserveSig = false)] |
| 21 | internal static extern int VerCompareStringVersions( |
| 22 | [MarshalAs(UnmanagedType.LPWStr)] string wzVersion1, |
| 23 | [MarshalAs(UnmanagedType.LPWStr)] string wzVersion2, |
| 24 | [MarshalAs(UnmanagedType.Bool)] bool fStrict |
| 25 | ); |
| 26 | |
| 27 | [DllImport("mbanative.dll", ExactSpelling = true, PreserveSig = false)] |
| 28 | internal static extern VersionHandle VerCopyVersion( |
| 29 | VersionHandle pSource |
| 30 | ); |
| 31 | |
| 32 | [DllImport("mbanative.dll", ExactSpelling = true)] |
| 33 | internal static extern void VerFreeVersion( |
| 34 | IntPtr pVersion |
| 35 | ); |
| 36 | |
| 37 | [DllImport("mbanative.dll", ExactSpelling = true, PreserveSig = false)] |
| 38 | internal static extern VersionHandle VerParseVersion( |
| 39 | [MarshalAs(UnmanagedType.LPWStr)] string wzVersion, |
| 40 | [MarshalAs(UnmanagedType.U4)] uint cchValue, |
| 41 | [MarshalAs(UnmanagedType.Bool)] bool fStrict |
| 42 | ); |
| 43 | |
| 44 | [DllImport("mbanative.dll", ExactSpelling = true, PreserveSig = false)] |
| 45 | internal static extern VersionHandle VerVersionFromQword( |
| 46 | [MarshalAs(UnmanagedType.I8)] long qwVersion |
| 47 | ); |
| 48 | |
| 49 | [StructLayout(LayoutKind.Sequential)] |
| 50 | internal struct VersionReleaseLabelStruct |
| 51 | { |
| 52 | public bool fNumeric; |
| 53 | public uint dwValue; |
| 54 | public IntPtr cchLabelOffset; |
| 55 | public int cchLabel; |
| 56 | } |
| 57 | |
| 58 | [StructLayout(LayoutKind.Sequential)] |
| 59 | internal struct VersionStruct |
| 60 | { |
| 61 | public IntPtr sczVersion; |
| 62 | public char chPrefix; |
| 63 | public uint dwMajor; |
| 64 | public uint dwMinor; |
| 65 | public uint dwPatch; |
| 66 | public uint dwRevision; |
| 67 | public int cReleaseLabels; |
| 68 | public IntPtr rgReleaseLabels; |
| 69 | public IntPtr cchMetadataOffset; |
| 70 | public bool fInvalid; |
| 71 | public bool fHasMajor; |
| 72 | public bool fHasMinor; |
| 73 | public bool fHasPatch; |
| 74 | public bool fHasRevision; |
| 75 | } |
| 76 | |
| 77 | internal static string VersionStringFromOffset(IntPtr wzVersion, IntPtr cchOffset, int? cchLength = null) |
| 78 | { |
| 79 | var offset = cchOffset.ToInt64() * UnicodeEncoding.CharSize; |
| 80 | var wz = new IntPtr(wzVersion.ToInt64() + offset); |
| 81 | if (cchLength.HasValue) |
| 82 | { |
| 83 | return Marshal.PtrToStringUni(wz, (int)cchLength); |
| 84 | } |
| 85 | else |
| 86 | { |
| 87 | return Marshal.PtrToStringUni(wz); |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | internal sealed class VersionHandle : SafeHandleZeroIsDefaultAndInvalid |
| 92 | { |
| 93 | protected override bool ReleaseHandle() |
| 94 | { |
| 95 | VerFreeVersion(this.handle); |
| 96 | return true; |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | /// <returns>0 if equal, 1 if version1 > version2, -1 if version1 < version2</returns> |
| 101 | public static int CompareParsedVersions(VerUtilVersion version1, VerUtilVersion version2) |
| 102 | { |
| 103 | return VerCompareParsedVersions(version1.GetHandle(), version2.GetHandle()); |
| 104 | } |
| 105 | |
| 106 | /// <returns>0 if equal, 1 if version1 > version2, -1 if version1 < version2</returns> |
| 107 | public static int CompareStringVersions(string version1, string version2, bool strict) |
| 108 | { |
| 109 | return VerCompareStringVersions(version1, version2, strict); |
| 110 | } |
| 111 | |
| 112 | /// <summary> |
| 113 | /// Clone the version. |
| 114 | /// </summary> |
| 115 | /// <param name="version">Source version</param> |
| 116 | /// <returns>Cloned version</returns> |
| 117 | public static VerUtilVersion CopyVersion(VerUtilVersion version) |
| 118 | { |
| 119 | var handle = VerCopyVersion(version.GetHandle()); |
| 120 | return new VerUtilVersion(handle); |
| 121 | } |
| 122 | |
| 123 | /// <summary> |
| 124 | /// Parse a version. |
| 125 | /// </summary> |
| 126 | /// <param name="version">Source version</param> |
| 127 | /// <param name="strict">Whether to throw exception on invalid version.</param> |
| 128 | /// <returns>Parsed version</returns> |
| 129 | public static VerUtilVersion ParseVersion(string version, bool strict) |
| 130 | { |
| 131 | var handle = VerParseVersion(version, 0, strict); |
| 132 | return new VerUtilVersion(handle); |
| 133 | } |
| 134 | |
| 135 | /// <summary> |
| 136 | /// Parse version from qword. |
| 137 | /// </summary> |
| 138 | /// <param name="version">Source version</param> |
| 139 | /// <returns>Parsed version</returns> |
| 140 | public static VerUtilVersion VersionFromQword(long version) |
| 141 | { |
| 142 | var handle = VerVersionFromQword(version); |
| 143 | return new VerUtilVersion(handle); |
| 144 | } |
| 145 | } |
| 146 | } |