From dbde9e7104b907bbbaea17e21247d8cafc8b3a4c Mon Sep 17 00:00:00 2001 From: Rob Mensching Date: Sat, 14 Oct 2017 16:12:07 -0700 Subject: Massive refactoring to introduce the concept of IBackend --- .../Msi/MsiException.cs | 78 ++++++++++++++++++++++ 1 file changed, 78 insertions(+) create mode 100644 src/WixToolset.Core.WindowsInstaller/Msi/MsiException.cs (limited to 'src/WixToolset.Core.WindowsInstaller/Msi/MsiException.cs') diff --git a/src/WixToolset.Core.WindowsInstaller/Msi/MsiException.cs b/src/WixToolset.Core.WindowsInstaller/Msi/MsiException.cs new file mode 100644 index 00000000..b33bf27a --- /dev/null +++ b/src/WixToolset.Core.WindowsInstaller/Msi/MsiException.cs @@ -0,0 +1,78 @@ +// 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. + +namespace WixToolset.Msi +{ + using System; + using System.ComponentModel; + using WixToolset.Core.Native; + + /// + /// Exception that wraps MsiGetLastError(). + /// + [Serializable] + public class MsiException : Win32Exception + { + /// + /// Instantiate a new MsiException with a given error. + /// + /// The error code from the MsiXxx() function call. + public MsiException(int error) : base(error) + { + uint handle = MsiInterop.MsiGetLastErrorRecord(); + if (0 != handle) + { + using (Record record = new Record(handle)) + { + this.MsiError = record.GetInteger(1); + + int errorInfoCount = record.GetFieldCount() - 1; + this.ErrorInfo = new string[errorInfoCount]; + for (int i = 0; i < errorInfoCount; ++i) + { + this.ErrorInfo[i] = record.GetString(i + 2); + } + } + } + else + { + this.MsiError = 0; + this.ErrorInfo = new string[0]; + } + + this.Error = error; + } + + /// + /// Gets the error number. + /// + public int Error { get; private set; } + + /// + /// Gets the internal MSI error number. + /// + public int MsiError { get; private set; } + + /// + /// Gets any additional the error information. + /// + public string[] ErrorInfo { get; private set; } + + /// + /// Overrides Message property to return useful error message. + /// + public override string Message + { + get + { + if (0 == this.MsiError) + { + return base.Message; + } + else + { + return String.Format("Internal MSI failure. Win32 error: {0}, MSI error: {1}, detail: {2}", this.Error, this.MsiError, String.Join(", ", this.ErrorInfo)); + } + } + } + } +} -- cgit v1.2.3-55-g6feb