aboutsummaryrefslogtreecommitdiff
path: root/src/WixToolset.Mba.Core/VerUtil.cs
diff options
context:
space:
mode:
Diffstat (limited to 'src/WixToolset.Mba.Core/VerUtil.cs')
-rw-r--r--src/WixToolset.Mba.Core/VerUtil.cs126
1 files changed, 126 insertions, 0 deletions
diff --git a/src/WixToolset.Mba.Core/VerUtil.cs b/src/WixToolset.Mba.Core/VerUtil.cs
new file mode 100644
index 00000000..dcc9dee2
--- /dev/null
+++ b/src/WixToolset.Mba.Core/VerUtil.cs
@@ -0,0 +1,126 @@
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
3namespace WixToolset.Mba.Core
4{
5 using System;
6 using System.Runtime.InteropServices;
7 using System.Text;
8
9 public static class VerUtil
10 {
11 [DllImport("mbanative.dll", ExactSpelling = true, PreserveSig = false)]
12 internal static extern int VerCompareParsedVersions(
13 VersionHandle pVersion1,
14 VersionHandle pVersion2
15 );
16
17 [DllImport("mbanative.dll", ExactSpelling = true, PreserveSig = false)]
18 internal static extern int VerCompareStringVersions(
19 [MarshalAs(UnmanagedType.LPWStr)] string wzVersion1,
20 [MarshalAs(UnmanagedType.LPWStr)] string wzVersion2,
21 [MarshalAs(UnmanagedType.Bool)] bool fStrict
22 );
23
24 [DllImport("mbanative.dll", ExactSpelling = true, PreserveSig = false)]
25 internal static extern VersionHandle VerCopyVersion(
26 VersionHandle pSource
27 );
28
29 [DllImport("mbanative.dll", ExactSpelling = true)]
30 internal static extern void VerFreeVersion(
31 IntPtr pVersion
32 );
33
34 [DllImport("mbanative.dll", ExactSpelling = true, PreserveSig = false)]
35 internal static extern VersionHandle VerParseVersion(
36 [MarshalAs(UnmanagedType.LPWStr)] string wzVersion,
37 [MarshalAs(UnmanagedType.U4)] uint cchValue,
38 [MarshalAs(UnmanagedType.Bool)] bool fStrict
39 );
40
41 [DllImport("mbanative.dll", ExactSpelling = true, PreserveSig = false)]
42 internal static extern VersionHandle VerVersionFromQword(
43 [MarshalAs(UnmanagedType.I8)] long qwVersion
44 );
45
46 [StructLayout(LayoutKind.Sequential)]
47 internal struct VersionReleaseLabelStruct
48 {
49 public bool fNumeric;
50 public uint dwValue;
51 public IntPtr cchLabelOffset;
52 public int cchLabel;
53 }
54
55 [StructLayout(LayoutKind.Sequential)]
56 internal struct VersionStruct
57 {
58 public IntPtr sczVersion;
59 public uint dwMajor;
60 public uint dwMinor;
61 public uint dwPatch;
62 public uint dwRevision;
63 public int cReleaseLabels;
64 public IntPtr rgReleaseLabels;
65 public IntPtr cchMetadataOffset;
66 public bool fInvalid;
67 }
68
69 internal static string VersionStringFromOffset(IntPtr wzVersion, IntPtr cchOffset, int? cchLength = null)
70 {
71 var offset = cchOffset.ToInt64() * UnicodeEncoding.CharSize;
72 var wz = new IntPtr(wzVersion.ToInt64() + offset);
73 if (cchLength.HasValue)
74 {
75 return Marshal.PtrToStringUni(wz, (int)cchLength);
76 }
77 else
78 {
79 return Marshal.PtrToStringUni(wz);
80 }
81 }
82
83 internal sealed class VersionHandle : SafeHandle
84 {
85 public VersionHandle() : base(IntPtr.Zero, true) { }
86
87 public override bool IsInvalid => false;
88
89 protected override bool ReleaseHandle()
90 {
91 VerFreeVersion(this.handle);
92 return true;
93 }
94 }
95
96 /// <returns>0 if equal, 1 if version1 &gt; version2, -1 if version1 &lt; version2</returns>
97 public static int CompareParsedVersions(VerUtilVersion version1, VerUtilVersion version2)
98 {
99 return VerCompareParsedVersions(version1.GetHandle(), version2.GetHandle());
100 }
101
102 /// <returns>0 if equal, 1 if version1 &gt; version2, -1 if version1 &lt; version2</returns>
103 public static int CompareStringVersions(string version1, string version2, bool strict)
104 {
105 return VerCompareStringVersions(version1, version2, strict);
106 }
107
108 public static VerUtilVersion CopyVersion(VerUtilVersion version)
109 {
110 var handle = VerCopyVersion(version.GetHandle());
111 return new VerUtilVersion(handle);
112 }
113
114 public static VerUtilVersion ParseVersion(string version, bool strict)
115 {
116 var handle = VerParseVersion(version, 0, strict);
117 return new VerUtilVersion(handle);
118 }
119
120 public static VerUtilVersion VersionFromQword(long version)
121 {
122 var handle = VerVersionFromQword(version);
123 return new VerUtilVersion(handle);
124 }
125 }
126}