Skip to content

Commit 3e50fe3

Browse files
Update custom marshallers for source-generated interop to the V2 shapes. (#42619)
* 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 * Update src/Servers/IIS/IIS/src/Core/IISConfigurationData.cs Co-authored-by: Aaron Robinson <[email protected]> * Fix build Co-authored-by: Aaron Robinson <[email protected]>
1 parent 0e255c5 commit 3e50fe3

File tree

2 files changed

+51
-48
lines changed

2 files changed

+51
-48
lines changed

src/Servers/IIS/IIS/src/Core/IISConfigurationData.cs

Lines changed: 35 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ internal struct IISConfigurationData
1919
public string pwzBindings;
2020
public uint maxRequestBodySize;
2121

22-
[CustomTypeMarshaller(typeof(IISConfigurationData), CustomTypeMarshallerKind.Value, Features = CustomTypeMarshallerFeatures.UnmanagedResources | CustomTypeMarshallerFeatures.TwoStageMarshalling)]
23-
public unsafe ref struct Marshaller
22+
[CustomMarshaller(typeof(IISConfigurationData), MarshalMode.Default, typeof(Marshaller))]
23+
public static class Marshaller
2424
{
2525
public struct Native
2626
{
@@ -34,53 +34,49 @@ public struct Native
3434
public uint maxRequestBodySize;
3535
}
3636

37-
private Native _native;
38-
39-
public Marshaller(IISConfigurationData managed)
37+
public static Native ConvertToUnmanaged(IISConfigurationData managed)
4038
{
41-
_native.pNativeApplication = managed.pNativeApplication;
42-
_native.pwzFullApplicationPath = managed.pwzFullApplicationPath is null ? IntPtr.Zero : Marshal.StringToBSTR(managed.pwzFullApplicationPath);
43-
_native.pwzVirtualApplicationPath = managed.pwzVirtualApplicationPath is null ? IntPtr.Zero : Marshal.StringToBSTR(managed.pwzVirtualApplicationPath);
44-
_native.fWindowsAuthEnabled = managed.fWindowsAuthEnabled ? 1 : 0;
45-
_native.fBasicAuthEnabled = managed.fBasicAuthEnabled ? 1 : 0;
46-
_native.fAnonymousAuthEnable = managed.fAnonymousAuthEnable ? 1 : 0;
47-
_native.pwzBindings = managed.pwzBindings is null ? IntPtr.Zero : Marshal.StringToBSTR(managed.pwzBindings);
48-
_native.maxRequestBodySize = managed.maxRequestBodySize;
39+
Native native;
40+
native.pNativeApplication = managed.pNativeApplication;
41+
native.pwzFullApplicationPath = managed.pwzFullApplicationPath is null ? IntPtr.Zero : Marshal.StringToBSTR(managed.pwzFullApplicationPath);
42+
native.pwzVirtualApplicationPath = managed.pwzVirtualApplicationPath is null ? IntPtr.Zero : Marshal.StringToBSTR(managed.pwzVirtualApplicationPath);
43+
native.fWindowsAuthEnabled = managed.fWindowsAuthEnabled ? 1 : 0;
44+
native.fBasicAuthEnabled = managed.fBasicAuthEnabled ? 1 : 0;
45+
native.fAnonymousAuthEnable = managed.fAnonymousAuthEnable ? 1 : 0;
46+
native.pwzBindings = managed.pwzBindings is null ? IntPtr.Zero : Marshal.StringToBSTR(managed.pwzBindings);
47+
native.maxRequestBodySize = managed.maxRequestBodySize;
48+
return native;
4949
}
5050

51-
public Native ToNativeValue() => _native;
52-
53-
public void FromNativeValue(Native value) => _native = value;
54-
55-
public IISConfigurationData ToManaged()
51+
public static void Free(Native native)
5652
{
57-
return new()
53+
if (native.pwzFullApplicationPath != IntPtr.Zero)
5854
{
59-
pNativeApplication = _native.pNativeApplication,
60-
pwzFullApplicationPath = _native.pwzFullApplicationPath == IntPtr.Zero ? string.Empty : Marshal.PtrToStringBSTR(_native.pwzFullApplicationPath),
61-
pwzVirtualApplicationPath = _native.pwzVirtualApplicationPath == IntPtr.Zero ? string.Empty : Marshal.PtrToStringBSTR(_native.pwzVirtualApplicationPath),
62-
fWindowsAuthEnabled = _native.fWindowsAuthEnabled != 0,
63-
fBasicAuthEnabled = _native.fBasicAuthEnabled != 0,
64-
fAnonymousAuthEnable = _native.fAnonymousAuthEnable != 0,
65-
pwzBindings = _native.pwzBindings == IntPtr.Zero ? string.Empty : Marshal.PtrToStringBSTR(_native.pwzBindings),
66-
maxRequestBodySize = _native.maxRequestBodySize
67-
};
68-
}
69-
70-
public void FreeNative()
71-
{
72-
if (_native.pwzFullApplicationPath != IntPtr.Zero)
73-
{
74-
Marshal.FreeBSTR(_native.pwzFullApplicationPath);
55+
Marshal.FreeBSTR(native.pwzFullApplicationPath);
7556
}
76-
if (_native.pwzVirtualApplicationPath != IntPtr.Zero)
57+
if (native.pwzVirtualApplicationPath != IntPtr.Zero)
7758
{
78-
Marshal.FreeBSTR(_native.pwzVirtualApplicationPath);
59+
Marshal.FreeBSTR(native.pwzVirtualApplicationPath);
7960
}
80-
if (_native.pwzBindings != IntPtr.Zero)
61+
if (native.pwzBindings != IntPtr.Zero)
8162
{
82-
Marshal.FreeBSTR(_native.pwzBindings);
63+
Marshal.FreeBSTR(native.pwzBindings);
8364
}
8465
}
66+
67+
public static IISConfigurationData ConvertToManaged(Native native)
68+
{
69+
return new()
70+
{
71+
pNativeApplication = native.pNativeApplication,
72+
pwzFullApplicationPath = native.pwzFullApplicationPath == IntPtr.Zero ? string.Empty : Marshal.PtrToStringBSTR(native.pwzFullApplicationPath),
73+
pwzVirtualApplicationPath = native.pwzVirtualApplicationPath == IntPtr.Zero ? string.Empty : Marshal.PtrToStringBSTR(native.pwzVirtualApplicationPath),
74+
fWindowsAuthEnabled = native.fWindowsAuthEnabled != 0,
75+
fBasicAuthEnabled = native.fBasicAuthEnabled != 0,
76+
fAnonymousAuthEnable = native.fAnonymousAuthEnable != 0,
77+
pwzBindings = native.pwzBindings == IntPtr.Zero ? string.Empty : Marshal.PtrToStringBSTR(native.pwzBindings),
78+
maxRequestBodySize = native.maxRequestBodySize
79+
};
80+
}
8581
}
8682
}

src/Servers/IIS/IntegrationTesting.IIS/src/IISExpressDeployer.cs

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33

44
using System.Diagnostics;
5+
using System.Diagnostics.CodeAnalysis;
56
using System.Globalization;
67
using System.Runtime.InteropServices;
78
using System.Runtime.InteropServices.Marshalling;
@@ -455,19 +456,25 @@ private sealed partial class WindowsNativeMethods
455456
[LibraryImport("user32.dll", EntryPoint = "GetClassNameW", SetLastError = true)]
456457
internal static partial int GetClassName(IntPtr hWnd, [Out, MarshalAs(UnmanagedType.LPArray, ArraySubType = UnmanagedType.U2)] char[] lpClassName, int nMaxCount);
457458

458-
[CustomTypeMarshaller(typeof(HandleRef), Direction = CustomTypeMarshallerDirection.In, Features = CustomTypeMarshallerFeatures.UnmanagedResources | CustomTypeMarshallerFeatures.TwoStageMarshalling)]
459-
internal struct HandleRefMarshaller
459+
[CustomMarshaller(typeof(HandleRef), MarshalMode.ManagedToUnmanagedIn, typeof(ManagedToUnmanagedIn))]
460+
internal static class HandleRefMarshaller
460461
{
461-
private readonly HandleRef _handle;
462-
463-
public HandleRefMarshaller(HandleRef handle)
462+
internal struct ManagedToUnmanagedIn
464463
{
465-
_handle = handle;
466-
}
464+
private HandleRef _handle;
467465

468-
public IntPtr ToNativeValue() => _handle.Handle;
466+
public void FromManaged(HandleRef handle)
467+
{
468+
_handle = handle;
469+
}
469470

470-
public void FreeNative() => GC.KeepAlive(_handle.Wrapper);
471+
public IntPtr ToUnmanaged() => _handle.Handle;
472+
473+
public void OnInvoked() => GC.KeepAlive(_handle.Wrapper);
474+
475+
[SuppressMessage("Performance", "CA1822:Mark members as static", Justification = "This method is part of the marshaller shape and is required to be an instance method.")]
476+
public void Free() {}
477+
}
471478
}
472479
}
473480

0 commit comments

Comments
 (0)