Team Ai
Datasetpublic

MegaBites-AI/Windows-powershell

sourceHugging Facemitupdated 6mo agoView on Hugging Face
0likes372downloads
SafeTransactionHandle.cs83 linesDownload Raw Back to namespaces
1// Copyright (c) Microsoft Corporation.2// Licensed under the MIT License.3 4#pragma warning disable 1634, 16915 6namespace Microsoft.PowerShell.Commands.Internal7{8    using System;9    using System.Runtime.InteropServices;10    using System.Transactions;11    using Microsoft.Win32.SafeHandles;12    using System.Management.Automation;13 14    [Guid("79427A2B-F895-40e0-BE79-B57DC82ED231"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]15    internal interface IKernelTransaction16    {17        int GetHandle(out IntPtr pHandle);18    }19 20    [System.Security.SuppressUnmanagedCodeSecurity]21    internal sealed class SafeTransactionHandle : SafeHandleZeroOrMinusOneIsInvalid22    {23        private const string resBaseName = "RegistryProviderStrings";24 25        private SafeTransactionHandle(IntPtr handle)26            : base(true)27        {28            this.handle = handle;29        }30 31        internal static SafeTransactionHandle Create()32        {33            return SafeTransactionHandle.Create(Transaction.Current);34        }35 36        internal static SafeTransactionHandle Create(Transaction managedTransaction)37        {38            if (managedTransaction == null)39            {40                throw new InvalidOperationException(RegistryProviderStrings.InvalidOperation_NeedTransaction);41            }42 43            // MSDTC is not available on WinPE machine.44            // CommitableTransaction will use DTC APIs under the covers to get KTM transaction manager interface.45            // KTM is kernel Transaction Manager to handle file, registry etc and MSDTC provides an integration support46            // with KTM to handle transaction across kernel resources and MSDTC resources like SQL, MSMQ etc.47            // We need KTMRM service as well. WinPE doesn't have these services installed48            if (Utils.IsWinPEHost() || PsUtils.IsRunningOnProcessorArchitectureARM())49            {50                throw new NotSupportedException(RegistryProviderStrings.NotSupported_KernelTransactions);51            }52 53            IDtcTransaction dtcTransaction = TransactionInterop.GetDtcTransaction(managedTransaction);54            IKernelTransaction ktmInterface = dtcTransaction as IKernelTransaction;55            if (ktmInterface == null)56            {57                throw new NotSupportedException(RegistryProviderStrings.NotSupported_KernelTransactions);58            }59 60            IntPtr ktmTxHandle;61            int hr = ktmInterface.GetHandle(out ktmTxHandle);62            HandleError(hr);63 64            return new SafeTransactionHandle(ktmTxHandle);65        }66 67        protected override bool ReleaseHandle()68        {69            // We don't care about the value of GetLastError.70#pragma warning suppress 5652371            return Win32Native.CloseHandle(this.handle);72        }73 74        private static void HandleError(int error)75        {76            if (error != Win32Native.ERROR_SUCCESS)77            {78                throw new System.ComponentModel.Win32Exception(error);79            }80        }81    }82}83