From 0fe498be75a17810c86c9f9def2a40f4a6fc3b23 Mon Sep 17 00:00:00 2001 From: Jeremy Koritzinsky Date: Tue, 28 Jun 2022 16:55:42 -0700 Subject: [PATCH 1/3] Update custom marshallers for source-generated interop to the V2 shapes. Update shape to match the final shapes Fix enum name to match approved name --- .../IIS/IIS/src/Core/IISConfigurationData.cs | 72 +++++++++---------- .../src/IISExpressDeployer.cs | 23 +++--- 2 files changed, 49 insertions(+), 46 deletions(-) diff --git a/src/Servers/IIS/IIS/src/Core/IISConfigurationData.cs b/src/Servers/IIS/IIS/src/Core/IISConfigurationData.cs index 612c2072c4d0..f7493edd20b6 100644 --- a/src/Servers/IIS/IIS/src/Core/IISConfigurationData.cs +++ b/src/Servers/IIS/IIS/src/Core/IISConfigurationData.cs @@ -19,8 +19,8 @@ internal struct IISConfigurationData public string pwzBindings; public uint maxRequestBodySize; - [CustomTypeMarshaller(typeof(IISConfigurationData), CustomTypeMarshallerKind.Value, Features = CustomTypeMarshallerFeatures.UnmanagedResources | CustomTypeMarshallerFeatures.TwoStageMarshalling)] - public unsafe ref struct Marshaller + [CustomMarshaller(typeof(IISConfigurationData), MarshalMode.Default, typeof(Marshaller))] + public static class Marshaller { public struct Native { @@ -36,51 +36,49 @@ public struct Native private Native _native; - public Marshaller(IISConfigurationData managed) + public static Native ConvertToUnmanaged(IISConfigurationData managed) { - _native.pNativeApplication = managed.pNativeApplication; - _native.pwzFullApplicationPath = managed.pwzFullApplicationPath is null ? IntPtr.Zero : Marshal.StringToBSTR(managed.pwzFullApplicationPath); - _native.pwzVirtualApplicationPath = managed.pwzVirtualApplicationPath is null ? IntPtr.Zero : Marshal.StringToBSTR(managed.pwzVirtualApplicationPath); - _native.fWindowsAuthEnabled = managed.fWindowsAuthEnabled ? 1 : 0; - _native.fBasicAuthEnabled = managed.fBasicAuthEnabled ? 1 : 0; - _native.fAnonymousAuthEnable = managed.fAnonymousAuthEnable ? 1 : 0; - _native.pwzBindings = managed.pwzBindings is null ? IntPtr.Zero : Marshal.StringToBSTR(managed.pwzBindings); - _native.maxRequestBodySize = managed.maxRequestBodySize; + Native native; + native.pNativeApplication = managed.pNativeApplication; + native.pwzFullApplicationPath = managed.pwzFullApplicationPath is null ? IntPtr.Zero : Marshal.StringToBSTR(managed.pwzFullApplicationPath); + native.pwzVirtualApplicationPath = managed.pwzVirtualApplicationPath is null ? IntPtr.Zero : Marshal.StringToBSTR(managed.pwzVirtualApplicationPath); + native.fWindowsAuthEnabled = managed.fWindowsAuthEnabled ? 1 : 0; + native.fBasicAuthEnabled = managed.fBasicAuthEnabled ? 1 : 0; + native.fAnonymousAuthEnable = managed.fAnonymousAuthEnable ? 1 : 0; + native.pwzBindings = managed.pwzBindings is null ? IntPtr.Zero : Marshal.StringToBSTR(managed.pwzBindings); + native.maxRequestBodySize = managed.maxRequestBodySize; + return native; } - public Native ToNativeValue() => _native; - - public void FromNativeValue(Native value) => _native = value; - - public IISConfigurationData ToManaged() - { - return new() - { - pNativeApplication = _native.pNativeApplication, - pwzFullApplicationPath = _native.pwzFullApplicationPath == IntPtr.Zero ? string.Empty : Marshal.PtrToStringBSTR(_native.pwzFullApplicationPath), - pwzVirtualApplicationPath = _native.pwzVirtualApplicationPath == IntPtr.Zero ? string.Empty : Marshal.PtrToStringBSTR(_native.pwzVirtualApplicationPath), - fWindowsAuthEnabled = _native.fWindowsAuthEnabled != 0, - fBasicAuthEnabled = _native.fBasicAuthEnabled != 0, - fAnonymousAuthEnable = _native.fAnonymousAuthEnable != 0, - pwzBindings = _native.pwzBindings == IntPtr.Zero ? string.Empty : Marshal.PtrToStringBSTR(_native.pwzBindings), - maxRequestBodySize = _native.maxRequestBodySize - }; - } - - public void FreeNative() + public static void Free(Native native) { - if (_native.pwzFullApplicationPath != IntPtr.Zero) + if (native.pwzFullApplicationPath != IntPtr.Zero) { - Marshal.FreeBSTR(_native.pwzFullApplicationPath); + Marshal.FreeBSTR(native.pwzFullApplicationPath); } - if (_native.pwzVirtualApplicationPath != IntPtr.Zero) + if (native.pwzVirtualApplicationPath != IntPtr.Zero) { - Marshal.FreeBSTR(_native.pwzVirtualApplicationPath); + Marshal.FreeBSTR(native.pwzVirtualApplicationPath); } - if (_native.pwzBindings != IntPtr.Zero) + if (native.pwzBindings != IntPtr.Zero) { - Marshal.FreeBSTR(_native.pwzBindings); + Marshal.FreeBSTR(native.pwzBindings); } } + + public static IISConfigurationData ConvertToManaged(Native native) + { + return new() + { + pNativeApplication = native.pNativeApplication, + pwzFullApplicationPath = native.pwzFullApplicationPath == IntPtr.Zero ? string.Empty : Marshal.PtrToStringBSTR(_native.pwzFullApplicationPath), + pwzVirtualApplicationPath = native.pwzVirtualApplicationPath == IntPtr.Zero ? string.Empty : Marshal.PtrToStringBSTR(_native.pwzVirtualApplicationPath), + fWindowsAuthEnabled = native.fWindowsAuthEnabled != 0, + fBasicAuthEnabled = native.fBasicAuthEnabled != 0, + fAnonymousAuthEnable = native.fAnonymousAuthEnable != 0, + pwzBindings = native.pwzBindings == IntPtr.Zero ? string.Empty : Marshal.PtrToStringBSTR(_native.pwzBindings), + maxRequestBodySize = native.maxRequestBodySize + }; + } } } diff --git a/src/Servers/IIS/IntegrationTesting.IIS/src/IISExpressDeployer.cs b/src/Servers/IIS/IntegrationTesting.IIS/src/IISExpressDeployer.cs index 82e10049729a..2916dfc7b372 100644 --- a/src/Servers/IIS/IntegrationTesting.IIS/src/IISExpressDeployer.cs +++ b/src/Servers/IIS/IntegrationTesting.IIS/src/IISExpressDeployer.cs @@ -455,19 +455,24 @@ private sealed partial class WindowsNativeMethods [LibraryImport("user32.dll", EntryPoint = "GetClassNameW", SetLastError = true)] internal static partial int GetClassName(IntPtr hWnd, [Out, MarshalAs(UnmanagedType.LPArray, ArraySubType = UnmanagedType.U2)] char[] lpClassName, int nMaxCount); - [CustomTypeMarshaller(typeof(HandleRef), Direction = CustomTypeMarshallerDirection.In, Features = CustomTypeMarshallerFeatures.UnmanagedResources | CustomTypeMarshallerFeatures.TwoStageMarshalling)] - internal struct HandleRefMarshaller + [CustomMarshaller(typeof(HandleRef), MarshalMode.ManagedToUnmanagedIn, typeof(ManagedToUnmanagedIn))] + internal static class HandleRefMarshaller { - private readonly HandleRef _handle; - - public HandleRefMarshaller(HandleRef handle) + internal struct ManagedToUnmanagedIn { - _handle = handle; - } + private HandleRef _handle; - public IntPtr ToNativeValue() => _handle.Handle; + public void FromManaged(HandleRef handle) + { + _handle = handle; + } - public void FreeNative() => GC.KeepAlive(_handle.Wrapper); + public IntPtr ToUnmanaged() => _handle.Handle; + + public void OnInvoked() => GC.KeepAlive(_handle.Wrapper); + + public void Free() {} + } } } From 7ac80b28489ad5891d53d965e58f440d1ab9f8b6 Mon Sep 17 00:00:00 2001 From: Jeremy Koritzinsky Date: Thu, 7 Jul 2022 13:44:34 -0700 Subject: [PATCH 2/3] Update src/Servers/IIS/IIS/src/Core/IISConfigurationData.cs Co-authored-by: Aaron Robinson --- src/Servers/IIS/IIS/src/Core/IISConfigurationData.cs | 1 - 1 file changed, 1 deletion(-) diff --git a/src/Servers/IIS/IIS/src/Core/IISConfigurationData.cs b/src/Servers/IIS/IIS/src/Core/IISConfigurationData.cs index f7493edd20b6..d8f723eff1a3 100644 --- a/src/Servers/IIS/IIS/src/Core/IISConfigurationData.cs +++ b/src/Servers/IIS/IIS/src/Core/IISConfigurationData.cs @@ -34,7 +34,6 @@ public struct Native public uint maxRequestBodySize; } - private Native _native; public static Native ConvertToUnmanaged(IISConfigurationData managed) { From cd6a0f483c4e1eb5a4173efc38fbf852f3d7fa1f Mon Sep 17 00:00:00 2001 From: Jeremy Koritzinsky Date: Thu, 7 Jul 2022 14:01:05 -0700 Subject: [PATCH 3/3] Fix build --- src/Servers/IIS/IIS/src/Core/IISConfigurationData.cs | 7 +++---- .../IIS/IntegrationTesting.IIS/src/IISExpressDeployer.cs | 2 ++ 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/src/Servers/IIS/IIS/src/Core/IISConfigurationData.cs b/src/Servers/IIS/IIS/src/Core/IISConfigurationData.cs index d8f723eff1a3..41f3aba13f46 100644 --- a/src/Servers/IIS/IIS/src/Core/IISConfigurationData.cs +++ b/src/Servers/IIS/IIS/src/Core/IISConfigurationData.cs @@ -34,7 +34,6 @@ public struct Native public uint maxRequestBodySize; } - public static Native ConvertToUnmanaged(IISConfigurationData managed) { Native native; @@ -70,12 +69,12 @@ public static IISConfigurationData ConvertToManaged(Native native) return new() { pNativeApplication = native.pNativeApplication, - pwzFullApplicationPath = native.pwzFullApplicationPath == IntPtr.Zero ? string.Empty : Marshal.PtrToStringBSTR(_native.pwzFullApplicationPath), - pwzVirtualApplicationPath = native.pwzVirtualApplicationPath == IntPtr.Zero ? string.Empty : Marshal.PtrToStringBSTR(_native.pwzVirtualApplicationPath), + pwzFullApplicationPath = native.pwzFullApplicationPath == IntPtr.Zero ? string.Empty : Marshal.PtrToStringBSTR(native.pwzFullApplicationPath), + pwzVirtualApplicationPath = native.pwzVirtualApplicationPath == IntPtr.Zero ? string.Empty : Marshal.PtrToStringBSTR(native.pwzVirtualApplicationPath), fWindowsAuthEnabled = native.fWindowsAuthEnabled != 0, fBasicAuthEnabled = native.fBasicAuthEnabled != 0, fAnonymousAuthEnable = native.fAnonymousAuthEnable != 0, - pwzBindings = native.pwzBindings == IntPtr.Zero ? string.Empty : Marshal.PtrToStringBSTR(_native.pwzBindings), + pwzBindings = native.pwzBindings == IntPtr.Zero ? string.Empty : Marshal.PtrToStringBSTR(native.pwzBindings), maxRequestBodySize = native.maxRequestBodySize }; } diff --git a/src/Servers/IIS/IntegrationTesting.IIS/src/IISExpressDeployer.cs b/src/Servers/IIS/IntegrationTesting.IIS/src/IISExpressDeployer.cs index 2916dfc7b372..951491335c54 100644 --- a/src/Servers/IIS/IntegrationTesting.IIS/src/IISExpressDeployer.cs +++ b/src/Servers/IIS/IntegrationTesting.IIS/src/IISExpressDeployer.cs @@ -2,6 +2,7 @@ // The .NET Foundation licenses this file to you under the MIT license. using System.Diagnostics; +using System.Diagnostics.CodeAnalysis; using System.Globalization; using System.Runtime.InteropServices; using System.Runtime.InteropServices.Marshalling; @@ -471,6 +472,7 @@ public void FromManaged(HandleRef handle) public void OnInvoked() => GC.KeepAlive(_handle.Wrapper); + [SuppressMessage("Performance", "CA1822:Mark members as static", Justification = "This method is part of the marshaller shape and is required to be an instance method.")] public void Free() {} } }