Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update custom marshallers for source-generated interop to the V2 shapes. #42619

Merged
merged 3 commits into from
Jul 9, 2022
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 35 additions & 37 deletions src/Servers/IIS/IIS/src/Core/IISConfigurationData.cs
Original file line number Diff line number Diff line change
@@ -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;
jkoritzinsky marked this conversation as resolved.
Show resolved Hide resolved

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
};
}
}
}
Original file line number Diff line number Diff line change
@@ -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() {}
}
}
}