From b1b7560bca925652a7928c32f3a35702c5d8f531 Mon Sep 17 00:00:00 2001 From: Dongbo Wang Date: Fri, 28 Jul 2017 13:37:43 -0700 Subject: [PATCH] Clean up ClrFacade.cs - remove ClrFacade.SizeOf() --- .../host/msh/ConsoleControl.cs | 2 +- .../LocalAccounts/Sam.cs | 6 ++--- .../engine/COM/ComInvoker.cs | 2 +- .../engine/COM/ComUtil.cs | 4 ++-- .../CommandCompletion/CompletionCompleters.cs | 2 +- .../engine/remoting/fanin/WSManNativeAPI.cs | 22 +++++++++---------- .../namespaces/FileSystemProvider.cs | 6 ++--- .../utils/ClrFacade.cs | 9 -------- .../utils/PlatformInvokes.cs | 4 ++-- 9 files changed, 24 insertions(+), 33 deletions(-) diff --git a/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleControl.cs b/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleControl.cs index c4aa8a794b2..2513fe9e963 100644 --- a/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleControl.cs +++ b/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleControl.cs @@ -3327,7 +3327,7 @@ private static HostException CreateHostException( internal static void MimicKeyPress(INPUT[] inputs) { Dbg.Assert(inputs != null && inputs.Length > 0, "inputs should not be null or empty"); - var numberOfSuccessfulEvents = NativeMethods.SendInput((uint)inputs.Length, inputs, ClrFacade.SizeOf()); + var numberOfSuccessfulEvents = NativeMethods.SendInput((uint)inputs.Length, inputs, Marshal.SizeOf()); if (numberOfSuccessfulEvents == 0) { diff --git a/src/Microsoft.PowerShell.LocalAccounts/LocalAccounts/Sam.cs b/src/Microsoft.PowerShell.LocalAccounts/LocalAccounts/Sam.cs index 20f582b6998..701db083191 100644 --- a/src/Microsoft.PowerShell.LocalAccounts/LocalAccounts/Sam.cs +++ b/src/Microsoft.PowerShell.LocalAccounts/LocalAccounts/Sam.cs @@ -1065,7 +1065,7 @@ private void OpenHandles() // Open the "BuiltIn" domain SecurityIdentifier sid = new SecurityIdentifier("S-1-5-32"); byte[] bSid = new byte[sid.BinaryLength]; - int size = ClrFacade.SizeOf() * bSid.Length; + int size = Marshal.SizeOf() * bSid.Length; pSid = Marshal.AllocHGlobal(size); @@ -2247,7 +2247,7 @@ private void SetUserData(IntPtr userHandle, if ((which & SamApi.USER_ALL_USERACCOUNTCONTROL) != 0) info.UserAccountControl = uac; - buffer = Marshal.AllocHGlobal(ClrFacade.SizeOf()); + buffer = Marshal.AllocHGlobal(Marshal.SizeOf()); ClrFacade.StructureToPtr(info, buffer, false); status = SamApi.SamSetInformationUser(userHandle, @@ -2389,7 +2389,7 @@ private void SetSamDacl(IntPtr objectHandle, RawAcl rawAcl) // create a new security descriptor var sd = new SECURITY_DESCRIPTOR() { Revision = 1 }; - ipsd = Marshal.AllocHGlobal(ClrFacade.SizeOf()); + ipsd = Marshal.AllocHGlobal(Marshal.SizeOf()); if (rawAcl != null && rawAcl.BinaryLength > 0) { diff --git a/src/System.Management.Automation/engine/COM/ComInvoker.cs b/src/System.Management.Automation/engine/COM/ComInvoker.cs index 12ef2dca6ab..9aa2a6c8e9c 100644 --- a/src/System.Management.Automation/engine/COM/ComInvoker.cs +++ b/src/System.Management.Automation/engine/COM/ComInvoker.cs @@ -24,7 +24,7 @@ internal static class ComInvoker // Alias of GUID_NULL. It's a GUID set to all zero private static readonly Guid s_IID_NULL = new Guid(); // Size of the Variant struct - private static readonly int s_variantSize = ClrFacade.SizeOf(); + private static readonly int s_variantSize = Marshal.SizeOf(); /// /// Make a by-Ref VARIANT value based on the passed-in VARIANT argument diff --git a/src/System.Management.Automation/engine/COM/ComUtil.cs b/src/System.Management.Automation/engine/COM/ComUtil.cs index d87429a6d91..0fe5841ce28 100644 --- a/src/System.Management.Automation/engine/COM/ComUtil.cs +++ b/src/System.Management.Automation/engine/COM/ComUtil.cs @@ -49,7 +49,7 @@ internal static string GetMethodSignatureFromFuncDesc(COM.ITypeInfo typeinfo, CO builder.Append(" ("); IntPtr ElementDescriptionArrayPtr = funcdesc.lprgelemdescParam; - int ElementDescriptionSize = ClrFacade.SizeOf(); + int ElementDescriptionSize = Marshal.SizeOf(); for (int i = 0; i < funcdesc.cParams; i++) { @@ -288,7 +288,7 @@ internal static ParameterInformation[] GetParameterInformation(COM.FUNCDESC func ParameterInformation[] parameters = new ParameterInformation[cParams]; IntPtr ElementDescriptionArrayPtr = funcdesc.lprgelemdescParam; - int ElementDescriptionSize = ClrFacade.SizeOf(); + int ElementDescriptionSize = Marshal.SizeOf(); for (int i = 0; i < cParams; i++) { diff --git a/src/System.Management.Automation/engine/CommandCompletion/CompletionCompleters.cs b/src/System.Management.Automation/engine/CommandCompletion/CompletionCompleters.cs index 6ce9ab17503..fb6e8913699 100644 --- a/src/System.Management.Automation/engine/CommandCompletion/CompletionCompleters.cs +++ b/src/System.Management.Automation/engine/CommandCompletion/CompletionCompleters.cs @@ -4468,7 +4468,7 @@ internal static List GetFileShares(string machine, bool ignoreHidden) { for (int i = 0; i < numEntries; ++i) { - IntPtr curInfoPtr = (IntPtr)((long)shBuf + (ClrFacade.SizeOf() * i)); + IntPtr curInfoPtr = (IntPtr)((long)shBuf + (Marshal.SizeOf() * i)); SHARE_INFO_1 shareInfo = ClrFacade.PtrToStructure(curInfoPtr); diff --git a/src/System.Management.Automation/engine/remoting/fanin/WSManNativeAPI.cs b/src/System.Management.Automation/engine/remoting/fanin/WSManNativeAPI.cs index 9b0e3afe1a1..44fda4966e0 100644 --- a/src/System.Management.Automation/engine/remoting/fanin/WSManNativeAPI.cs +++ b/src/System.Management.Automation/engine/remoting/fanin/WSManNativeAPI.cs @@ -176,7 +176,7 @@ internal MarshalledObject(IntPtr dataPtr) /// MarshalledObject internal static MarshalledObject Create(T obj) { - IntPtr ptr = Marshal.AllocHGlobal(ClrFacade.SizeOf()); + IntPtr ptr = Marshal.AllocHGlobal(Marshal.SizeOf()); Marshal.StructureToPtr(obj, ptr, false); // Now create the MarshalledObject and return. @@ -665,7 +665,7 @@ internal WSManData_ManToUn(byte[] data) _marshalledBuffer = dataToSendPtr; // Stored directly to enable graceful clean up during finalizer scenarios Marshal.Copy(data, 0, _internalData.binaryOrTextData.data, _internalData.binaryOrTextData.bufferLength); - _marshalledObject = Marshal.AllocHGlobal(ClrFacade.SizeOf()); + _marshalledObject = Marshal.AllocHGlobal(Marshal.SizeOf()); Marshal.StructureToPtr(_internalData, _marshalledObject, false); } @@ -686,7 +686,7 @@ internal WSManData_ManToUn(string data) // marshal text data _internalData.binaryOrTextData.data = Marshal.StringToHGlobalUni(data); _marshalledBuffer = _internalData.binaryOrTextData.data; // Stored directly to enable graceful clean up during finalizer scenarios - _marshalledObject = Marshal.AllocHGlobal(ClrFacade.SizeOf()); + _marshalledObject = Marshal.AllocHGlobal(Marshal.SizeOf()); Marshal.StructureToPtr(_internalData, _marshalledObject, false); } @@ -945,7 +945,7 @@ internal WSManStreamIDSet_ManToUn(string[] streamIds) { Dbg.Assert(streamIds != null, "stream ids cannot be null or empty"); - int sizeOfIntPtr = ClrFacade.SizeOf(); + int sizeOfIntPtr = Marshal.SizeOf(); _streamSetInfo = new WSManStreamIDSetStruct(); _streamSetInfo.streamIDsCount = streamIds.Length; _streamSetInfo.streamIDs = Marshal.AllocHGlobal(sizeOfIntPtr * streamIds.Length); @@ -965,7 +965,7 @@ internal void Dispose() { if (IntPtr.Zero != _streamSetInfo.streamIDs) { - int sizeOfIntPtr = ClrFacade.SizeOf(); + int sizeOfIntPtr = Marshal.SizeOf(); for (int index = 0; index < _streamSetInfo.streamIDsCount; index++) { IntPtr streamAddress = IntPtr.Zero; @@ -1028,7 +1028,7 @@ internal static WSManStreamIDSet_UnToMan UnMarshal(IntPtr unmanagedData) /* * // TODO: Why didn't this work? It looks more efficient idsArray = new string[resultInternal.streamIDsCount]; - int sizeInBytes = ClrFacade.SizeOf(); + int sizeInBytes = Marshal.SizeOf(); IntPtr perElementPtr = resultInternal.streamIDs; for (int i = 0; i < resultInternal.streamIDsCount; i++) @@ -1103,7 +1103,7 @@ internal WSManOptionSet(WSManOption[] options) { Dbg.Assert(null != options, "options cannot be null"); - int sizeOfOption = ClrFacade.SizeOf(); + int sizeOfOption = Marshal.SizeOf(); _optionSet = new WSManOptionSetStruct(); _optionSet.optionsCount = options.Length; _optionSet.optionsMustUnderstand = true; @@ -1187,7 +1187,7 @@ internal static WSManOptionSet UnMarshal(WSManOptionSetStruct resultInternal) { tempOptions = new WSManOption[resultInternal.optionsCount]; - int sizeInBytes = ClrFacade.SizeOf(); + int sizeInBytes = Marshal.SizeOf(); IntPtr perElementPtr = resultInternal.options; for (int i = 0; i < resultInternal.optionsCount; i++) @@ -1230,7 +1230,7 @@ internal WSManCommandArgSet(byte[] firstArgument) { _internalData = new WSManCommandArgSetInternal(); _internalData.argsCount = 1; - _internalData.args = Marshal.AllocHGlobal(ClrFacade.SizeOf()); + _internalData.args = Marshal.AllocHGlobal(Marshal.SizeOf()); // argument set takes only strings..but powershell's serialized pipeline might contain // \0 (null characters) which are unacceptable in WSMan. So we are converting to Base64 @@ -1518,7 +1518,7 @@ internal static WSManEnvironmentVariableSet UnMarshal(IntPtr unmanagedData) if (resultInternal.varsCount > 0) { varsArray = new WSManEnvironmentVariableInternal[resultInternal.varsCount]; - int sizeInBytes = ClrFacade.SizeOf(); + int sizeInBytes = Marshal.SizeOf(); IntPtr perElementPtr = resultInternal.vars; for (int i = 0; i < resultInternal.varsCount; i++) @@ -2251,7 +2251,7 @@ internal static WSManSelectorSet UnMarshal(WSManSelectorSetStruct resultInternal if (resultInternal.numberKeys > 0) { tempKeys = new WSManKeyStruct[resultInternal.numberKeys]; - int sizeInBytes = ClrFacade.SizeOf(); + int sizeInBytes = Marshal.SizeOf(); IntPtr perElementPtr = resultInternal.keys; for (int i = 0; i < resultInternal.numberKeys; i++) diff --git a/src/System.Management.Automation/namespaces/FileSystemProvider.cs b/src/System.Management.Automation/namespaces/FileSystemProvider.cs index c4cb76d0c8e..5e086911f73 100644 --- a/src/System.Management.Automation/namespaces/FileSystemProvider.cs +++ b/src/System.Management.Automation/namespaces/FileSystemProvider.cs @@ -8144,7 +8144,7 @@ private static string WinInternalGetLinkType(string filePath) using (SafeFileHandle handle = OpenReparsePoint(filePath, FileDesiredAccess.GenericRead)) { - int outBufferSize = ClrFacade.SizeOf(); + int outBufferSize = Marshal.SizeOf(); IntPtr outBuffer = Marshal.AllocHGlobal(outBufferSize); bool success = false; @@ -8378,7 +8378,7 @@ private static string InternalGetTarget(SafeFileHandle handle) [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Reliability", "CA2001:AvoidCallingProblematicMethods")] private static string WinInternalGetTarget(SafeFileHandle handle) { - int outBufferSize = ClrFacade.SizeOf(); + int outBufferSize = Marshal.SizeOf(); IntPtr outBuffer = Marshal.AllocHGlobal(outBufferSize); bool success = false; @@ -8546,7 +8546,7 @@ internal static bool DeleteJunction(string junctionPath) using (SafeHandle handle = OpenReparsePoint(junctionPath, FileDesiredAccess.GenericWrite)) { bool success = false; - int inOutBufferSize = ClrFacade.SizeOf(); + int inOutBufferSize = Marshal.SizeOf(); IntPtr outBuffer = Marshal.AllocHGlobal(inOutBufferSize); IntPtr inBuffer = Marshal.AllocHGlobal(inOutBufferSize); diff --git a/src/System.Management.Automation/utils/ClrFacade.cs b/src/System.Management.Automation/utils/ClrFacade.cs index 1f3ae196b0a..d4dc8fe8bff 100644 --- a/src/System.Management.Automation/utils/ClrFacade.cs +++ b/src/System.Management.Automation/utils/ClrFacade.cs @@ -69,15 +69,6 @@ internal static SafeHandle GetSafeProcessHandle(Process process) #region Marshal - /// - /// Facade for Marshal.SizeOf - /// - internal static int SizeOf() - { - // Marshal.SizeOf(Type) is obsolete in CoreCLR - return Marshal.SizeOf(); - } - /// /// Facade for Marshal.DestroyStructure /// diff --git a/src/System.Management.Automation/utils/PlatformInvokes.cs b/src/System.Management.Automation/utils/PlatformInvokes.cs index 701770bf38c..d29b2ee1b55 100644 --- a/src/System.Management.Automation/utils/PlatformInvokes.cs +++ b/src/System.Management.Automation/utils/PlatformInvokes.cs @@ -337,7 +337,7 @@ internal static bool EnableTokenPrivilege(string privilegeName, ref TOKEN_PRIVIL // The specified privilege is not enabled yet. Enable it. newPrivilegeState.PrivilegeCount = 1; newPrivilegeState.Privilege.Attributes = SE_PRIVILEGE_ENABLED; - int bufferSize = ClrFacade.SizeOf(); + int bufferSize = Marshal.SizeOf(); int returnSize = 0; // enable the specified privilege @@ -406,7 +406,7 @@ internal static bool RestoreTokenPrivilege(string privilegeName, ref TOKEN_PRIVI IntPtr tokenHandler = IntPtr.Zero; if (OpenProcessToken(processHandler, TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, out tokenHandler)) { - int bufferSize = ClrFacade.SizeOf(); + int bufferSize = Marshal.SizeOf(); int returnSize = 0; // restore the privilege state back to the previous privilege state