Team Ai
Datasetpublic

MegaBites-AI/Windows-powershell

sourceHugging Facemitupdated 6mo agoView on Hugging Face
0likes372downloads
ExcepInfo.cs92 linesDownload Raw Back to ComInterop
1// Licensed to the .NET Foundation under one or more agreements.2// The .NET Foundation licenses this file to you under the MIT license.3 4using System;5using System.Diagnostics;6using System.Reflection;7using System.Runtime.InteropServices;8using ComTypes = System.Runtime.InteropServices.ComTypes;9 10namespace System.Management.Automation.ComInterop11{12    /// <summary>13    /// This is similar to ComTypes.EXCEPINFO, but lets us do our own custom marshalling.14    /// </summary>15    [StructLayout(LayoutKind.Sequential)]16    internal struct ExcepInfo17    {18        private short _wCode;19        private short _wReserved;20        private IntPtr _bstrSource;21        private IntPtr _bstrDescription;22        private IntPtr _bstrHelpFile;23        private int _dwHelpContext;24        private IntPtr _pvReserved;25        private IntPtr _pfnDeferredFillIn;26        private int _scode;27 28#if DEBUG29        static ExcepInfo()30        {31            Debug.Assert(Marshal.SizeOf(typeof(ExcepInfo)) == Marshal.SizeOf(typeof(ComTypes.EXCEPINFO)));32        }33#endif34 35        private static string ConvertAndFreeBstr(ref IntPtr bstr)36        {37            if (bstr == IntPtr.Zero)38            {39                return null;40            }41 42            string result = Marshal.PtrToStringBSTR(bstr);43            Marshal.FreeBSTR(bstr);44            bstr = IntPtr.Zero;45            return result;46        }47 48        internal Exception GetException()49        {50            Debug.Assert(_pfnDeferredFillIn == IntPtr.Zero);51#if DEBUG52            System.Diagnostics.Debug.Assert(_wReserved != -1);53            _wReserved = -1; // to ensure that the method gets called only once54#endif55 56            int errorCode = (_scode != 0) ? _scode : _wCode;57            Exception exception = Marshal.GetExceptionForHR(errorCode);58 59            string message = ConvertAndFreeBstr(ref _bstrDescription);60            if (message != null)61            {62                // If we have a custom message, create a new Exception object with the message set correctly.63                // We need to create a new object because "exception.Message" is a read-only property.64                if (exception is COMException)65                {66                    exception = new COMException(message, errorCode);67                }68                else69                {70                    Type exceptionType = exception.GetType();71                    ConstructorInfo ctor = exceptionType.GetConstructor(new Type[] { typeof(string) });72                    if (ctor != null)73                    {74                        exception = (Exception)ctor.Invoke(new object[] { message });75                    }76                }77            }78 79            exception.Source = ConvertAndFreeBstr(ref _bstrSource);80 81            string helpLink = ConvertAndFreeBstr(ref _bstrHelpFile);82            if (helpLink != null && _dwHelpContext != 0)83            {84                helpLink += "#" + _dwHelpContext;85            }86            exception.HelpLink = helpLink;87 88            return exception;89        }90    }91}92