diff --git a/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.ComInterfaceGenerator/SharedTypes.ComInterfaces.IArrayOfStatelessElements.cs b/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.ComInterfaceGenerator/SharedTypes.ComInterfaces.IArrayOfStatelessElements.cs index b187078..85ffe63 100644 --- a/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.ComInterfaceGenerator/SharedTypes.ComInterfaces.IArrayOfStatelessElements.cs +++ b/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.ComInterfaceGenerator/SharedTypes.ComInterfaces.IArrayOfStatelessElements.cs @@ -157,7 +157,6 @@ } } - __param_native__numElements = size; global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.Free(__param_native); } } @@ -215,7 +214,6 @@ } } - __param_native__numElements = size; global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.Free(__param_native); } } @@ -418,6 +416,7 @@ internal static int ABI_Method(System.Runtime.InteropServices.ComWrappers.ComInt catch (System.Exception __exception) { __retVal = System.Runtime.InteropServices.Marshalling.ExceptionAsHResultMarshaller.ConvertToUnmanaged(__exception); + return __retVal; } return __retVal; @@ -427,7 +426,7 @@ internal static int ABI_Method(System.Runtime.InteropServices.ComWrappers.ComInt internal static int ABI_MethodIn(System.Runtime.InteropServices.ComWrappers.ComInterfaceDispatch* __this_native, nint** __param_native__param, int size) { global::SharedTypes.ComInterfaces.IArrayOfStatelessElements @this = default; - ref nint* __param_native = ref *__param_native__param; + nint* __param_native = *__param_native__param; global::SharedTypes.ComInterfaces.StatelessType[] param = default; int __retVal = default; // Setup - Perform required setup. @@ -454,6 +453,7 @@ internal static int ABI_MethodIn(System.Runtime.InteropServices.ComWrappers.ComI catch (System.Exception __exception) { __retVal = System.Runtime.InteropServices.Marshalling.ExceptionAsHResultMarshaller.ConvertToUnmanaged(__exception); + return __retVal; } return __retVal; @@ -463,7 +463,7 @@ internal static int ABI_MethodIn(System.Runtime.InteropServices.ComWrappers.ComI internal static int ABI_MethodOut(System.Runtime.InteropServices.ComWrappers.ComInterfaceDispatch* __this_native, nint** __param_native__param, int size) { global::SharedTypes.ComInterfaces.IArrayOfStatelessElements @this = default; - ref nint* __param_native = ref *__param_native__param; + nint* __param_native__out = default; global::SharedTypes.ComInterfaces.StatelessType[] param = default; int __retVal = default; // Setup - Perform required setup. @@ -476,20 +476,34 @@ internal static int ABI_MethodOut(System.Runtime.InteropServices.ComWrappers.Com @this = System.Runtime.InteropServices.ComWrappers.ComInterfaceDispatch.GetInstance(__this_native); @this.MethodOut(out param, size); // Marshal - Convert managed data to native data. - __param_native = global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.AllocateContainerForUnmanagedElements(param, out __param_native__numElements); + __param_native__out = global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.AllocateContainerForUnmanagedElements(param, out __param_native__numElements); { System.ReadOnlySpan __param_native__managedSpan = global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.GetManagedValuesSource(param); - System.Span __param_native__nativeSpan = global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.GetUnmanagedValuesDestination(__param_native, __param_native__numElements); + System.Span __param_native__nativeSpan = global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.GetUnmanagedValuesDestination(__param_native__out, __param_native__numElements); __param_native__nativeSpan.Clear(); for (int __i0 = 0; __i0 < __param_native__managedSpan.Length; ++__i0) { __param_native__nativeSpan[__i0] = global::SharedTypes.ComInterfaces.StatelessTypeMarshaller.ConvertToUnmanaged(__param_native__managedSpan[__i0]); } } + + // AssignOut - Assign to parameters + *__param_native__param = __param_native__out; } catch (System.Exception __exception) { __retVal = System.Runtime.InteropServices.Marshalling.ExceptionAsHResultMarshaller.ConvertToUnmanaged(__exception); + // CleanupFailure - Perform required cleanup. + { + System.ReadOnlySpan __param_native__nativeSpan = global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.GetUnmanagedValuesSource(__param_native__out, __param_native__numElements); + for (int __i0 = 0; __i0 < __param_native__nativeSpan.Length; ++__i0) + { + global::SharedTypes.ComInterfaces.StatelessTypeMarshaller.Free(__param_native__nativeSpan[__i0]); + } + } + + global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.Free(__param_native__out); + return __retVal; } return __retVal; @@ -499,14 +513,13 @@ internal static int ABI_MethodOut(System.Runtime.InteropServices.ComWrappers.Com internal static int ABI_MethodRef(System.Runtime.InteropServices.ComWrappers.ComInterfaceDispatch* __this_native, nint** __param_native__param, int size) { global::SharedTypes.ComInterfaces.IArrayOfStatelessElements @this = default; - ref nint* __param_native = ref *__param_native__param; + nint* __param_native__out = default; + nint* __param_native = *__param_native__param; global::SharedTypes.ComInterfaces.StatelessType[] param = default; int __retVal = default; // Setup - Perform required setup. int __param_native__numElements; System.Runtime.CompilerServices.Unsafe.SkipInit(out __param_native__numElements); - bool __param_native__ownOriginal = false; - nint* __param_native__original = __param_native; int __param_native__lastIndexMarshalled = 0; try { @@ -526,37 +539,43 @@ internal static int ABI_MethodRef(System.Runtime.InteropServices.ComWrappers.Com @this = System.Runtime.InteropServices.ComWrappers.ComInterfaceDispatch.GetInstance(__this_native); @this.MethodRef(ref param, size); // Marshal - Convert managed data to native data. - __param_native = global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.AllocateContainerForUnmanagedElements(param, out __param_native__numElements); - __param_native__ownOriginal = true; + __param_native__out = global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.AllocateContainerForUnmanagedElements(param, out __param_native__numElements); { System.ReadOnlySpan __param_native__managedSpan = global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.GetManagedValuesSource(param); - System.Span __param_native__nativeSpan = global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.GetUnmanagedValuesDestination(__param_native, __param_native__numElements); + System.Span __param_native__nativeSpan = global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.GetUnmanagedValuesDestination(__param_native__out, __param_native__numElements); for (int __i0 = 0; __i0 < __param_native__managedSpan.Length; ++__i0, ++__param_native__lastIndexMarshalled) { __param_native__nativeSpan[__i0] = global::SharedTypes.ComInterfaces.StatelessTypeMarshaller.ConvertToUnmanaged(__param_native__managedSpan[__i0]); } } + + // AssignOut - Assign to parameters + *__param_native__param = __param_native__out; + // Cleanup - Perform required cleanup. + { + System.ReadOnlySpan __param_native__nativeSpan = global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.GetUnmanagedValuesSource(__param_native, __param_native__numElements); + for (int __i0 = 0; __i0 < __param_native__lastIndexMarshalled; ++__i0) + { + global::SharedTypes.ComInterfaces.StatelessTypeMarshaller.Free(__param_native__nativeSpan[__i0]); + } + } + + global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.Free(__param_native); } catch (System.Exception __exception) { __retVal = System.Runtime.InteropServices.Marshalling.ExceptionAsHResultMarshaller.ConvertToUnmanaged(__exception); - } - finally - { - // Cleanup - Perform required cleanup. - if (__param_native__ownOriginal) + // CleanupFailure - Perform required cleanup. { + System.ReadOnlySpan __param_native__nativeSpan = global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.GetUnmanagedValuesSource(__param_native__out, __param_native__numElements); + for (int __i0 = 0; __i0 < __param_native__lastIndexMarshalled; ++__i0) { - System.ReadOnlySpan __param_native__nativeSpan = global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.GetUnmanagedValuesSource(__param_native__original, __param_native__numElements); - for (int __i0 = 0; __i0 < __param_native__lastIndexMarshalled; ++__i0) - { - global::SharedTypes.ComInterfaces.StatelessTypeMarshaller.Free(__param_native__nativeSpan[__i0]); - } + global::SharedTypes.ComInterfaces.StatelessTypeMarshaller.Free(__param_native__nativeSpan[__i0]); } - - __param_native__numElements = size; - global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.Free(__param_native__original); } + + global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.Free(__param_native__out); + return __retVal; } return __retVal; @@ -592,6 +611,7 @@ internal static int ABI_MethodContentsIn(System.Runtime.InteropServices.ComWrapp catch (System.Exception __exception) { __retVal = System.Runtime.InteropServices.Marshalling.ExceptionAsHResultMarshaller.ConvertToUnmanaged(__exception); + return __retVal; } return __retVal; @@ -606,29 +626,46 @@ internal static int ABI_MethodContentsOut(System.Runtime.InteropServices.ComWrap // Setup - Perform required setup. int __param_native__numElements; System.Runtime.CompilerServices.Unsafe.SkipInit(out __param_native__numElements); + scoped System.Span __param_native__out = default; try { // Unmarshal - Convert native data to managed data. __retVal = 0; // S_OK global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.GetManagedValuesDestination(param).Clear(); + __param_native__numElements = size; + param = global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.AllocateContainerForManagedElements(__param_native, __param_native__numElements); @this = System.Runtime.InteropServices.ComWrappers.ComInterfaceDispatch.GetInstance(__this_native); @this.MethodContentsOut(param, size); // Marshal - Convert managed data to native data. { __param_native__numElements = global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.GetManagedValuesDestination(param).Length; - System.Span __param_native__nativeSpan = System.Runtime.InteropServices.MemoryMarshal.CreateSpan(ref System.Runtime.CompilerServices.Unsafe.AsRef(in global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.GetUnmanagedValuesSource(__param_native, __param_native__numElements).GetPinnableReference()), __param_native__numElements); + __param_native__out = stackalloc nint[__param_native__numElements]; + System.Span __param_native__nativeSpan = __param_native__out; System.Span __param_native__managedSpan = global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.GetManagedValuesDestination(param); for (int __i0 = 0; __i0 < __param_native__numElements; ++__i0) { nint __param_native__nativeSpan____i0__original = __param_native__nativeSpan[__i0]; __param_native__nativeSpan[__i0] = global::SharedTypes.ComInterfaces.StatelessTypeMarshaller.ConvertToUnmanaged(__param_native__managedSpan[__i0]); - global::SharedTypes.ComInterfaces.StatelessTypeMarshaller.Free(__param_native__nativeSpan____i0__original); } } + + // AssignOut - Assign to parameters + __param_native__out.CopyTo(System.Runtime.InteropServices.MemoryMarshal.CreateSpan(ref System.Runtime.CompilerServices.Unsafe.AsRef(in global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.GetUnmanagedValuesSource(__param_native, __param_native__numElements).GetPinnableReference()), __param_native__numElements)); } catch (System.Exception __exception) { __retVal = System.Runtime.InteropServices.Marshalling.ExceptionAsHResultMarshaller.ConvertToUnmanaged(__exception); + // CleanupFailure - Perform required cleanup. + { + System.ReadOnlySpan __param_native__nativeSpan = __param_native__out; + for (int __i0 = 0; __i0 < __param_native__nativeSpan.Length; ++__i0) + { + global::SharedTypes.ComInterfaces.StatelessTypeMarshaller.Free(__param_native__nativeSpan[__i0]); + } + } + + __param_native__numElements = size; + return __retVal; } return __retVal; @@ -643,6 +680,7 @@ internal static int ABI_MethodContentsInOut(System.Runtime.InteropServices.ComWr // Setup - Perform required setup. int __param_native__numElements; System.Runtime.CompilerServices.Unsafe.SkipInit(out __param_native__numElements); + scoped System.Span __param_native__out = default; try { // Unmarshal - Convert native data to managed data. @@ -663,19 +701,43 @@ internal static int ABI_MethodContentsInOut(System.Runtime.InteropServices.ComWr // Marshal - Convert managed data to native data. { __param_native__numElements = global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.GetManagedValuesDestination(param).Length; - System.Span __param_native__nativeSpan = System.Runtime.InteropServices.MemoryMarshal.CreateSpan(ref System.Runtime.CompilerServices.Unsafe.AsRef(in global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.GetUnmanagedValuesSource(__param_native, __param_native__numElements).GetPinnableReference()), __param_native__numElements); + __param_native__out = stackalloc nint[__param_native__numElements]; + System.Span __param_native__nativeSpan = __param_native__out; System.Span __param_native__managedSpan = global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.GetManagedValuesDestination(param); for (int __i0 = 0; __i0 < __param_native__numElements; ++__i0) { nint __param_native__nativeSpan____i0__original = __param_native__nativeSpan[__i0]; __param_native__nativeSpan[__i0] = global::SharedTypes.ComInterfaces.StatelessTypeMarshaller.ConvertToUnmanaged(__param_native__managedSpan[__i0]); - global::SharedTypes.ComInterfaces.StatelessTypeMarshaller.Free(__param_native__nativeSpan____i0__original); } } + + // AssignOut - Assign to parameters + __param_native__out.CopyTo(System.Runtime.InteropServices.MemoryMarshal.CreateSpan(ref System.Runtime.CompilerServices.Unsafe.AsRef(in global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.GetUnmanagedValuesSource(__param_native, __param_native__numElements).GetPinnableReference()), __param_native__numElements)); + // Cleanup - Perform required cleanup. + { + System.ReadOnlySpan __param_native__nativeSpan = global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.GetUnmanagedValuesSource(__param_native, __param_native__numElements); + for (int __i0 = 0; __i0 < __param_native__nativeSpan.Length; ++__i0) + { + global::SharedTypes.ComInterfaces.StatelessTypeMarshaller.Free(__param_native__nativeSpan[__i0]); + } + } + + __param_native__numElements = size; } catch (System.Exception __exception) { __retVal = System.Runtime.InteropServices.Marshalling.ExceptionAsHResultMarshaller.ConvertToUnmanaged(__exception); + // CleanupFailure - Perform required cleanup. + { + System.ReadOnlySpan __param_native__nativeSpan = __param_native__out; + for (int __i0 = 0; __i0 < __param_native__nativeSpan.Length; ++__i0) + { + global::SharedTypes.ComInterfaces.StatelessTypeMarshaller.Free(__param_native__nativeSpan[__i0]); + } + } + + __param_native__numElements = size; + return __retVal; } return __retVal; diff --git a/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.ComInterfaceGenerator/SharedTypes.ComInterfaces.IBool.cs b/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.ComInterfaceGenerator/SharedTypes.ComInterfaces.IBool.cs index c0be69e..e41bf13 100644 --- a/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.ComInterfaceGenerator/SharedTypes.ComInterfaces.IBool.cs +++ b/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.ComInterfaceGenerator/SharedTypes.ComInterfaces.IBool.cs @@ -55,7 +55,7 @@ file unsafe partial interface InterfaceImplementation internal static int ABI_Get(System.Runtime.InteropServices.ComWrappers.ComInterfaceDispatch* __this_native, sbyte* __invokeRetValUnmanaged__param) { global::SharedTypes.ComInterfaces.IBool @this = default; - ref sbyte __invokeRetValUnmanaged = ref *__invokeRetValUnmanaged__param; + sbyte __invokeRetValUnmanaged__out = default; bool __invokeRetVal = default; int __retVal = default; try @@ -65,11 +65,14 @@ internal static int ABI_Get(System.Runtime.InteropServices.ComWrappers.ComInterf @this = System.Runtime.InteropServices.ComWrappers.ComInterfaceDispatch.GetInstance(__this_native); __invokeRetVal = @this.Get(); // Marshal - Convert managed data to native data. - __invokeRetValUnmanaged = (sbyte)(__invokeRetVal ? 1 : 0); + __invokeRetValUnmanaged__out = (sbyte)(__invokeRetVal ? 1 : 0); + // AssignOut - Assign to parameters + *__invokeRetValUnmanaged__param = __invokeRetValUnmanaged__out; } catch (System.Exception __exception) { __retVal = System.Runtime.InteropServices.Marshalling.ExceptionAsHResultMarshaller.ConvertToUnmanaged(__exception); + return __retVal; } return __retVal; @@ -92,6 +95,7 @@ internal static int ABI_Set(System.Runtime.InteropServices.ComWrappers.ComInterf catch (System.Exception __exception) { __retVal = System.Runtime.InteropServices.Marshalling.ExceptionAsHResultMarshaller.ConvertToUnmanaged(__exception); + return __retVal; } return __retVal; diff --git a/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.ComInterfaceGenerator/SharedTypes.ComInterfaces.ICustomStringMarshallingUtf16.cs b/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.ComInterfaceGenerator/SharedTypes.ComInterfaces.ICustomStringMarshallingUtf16.cs index 350e9ab..61bccd7 100644 --- a/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.ComInterfaceGenerator/SharedTypes.ComInterfaces.ICustomStringMarshallingUtf16.cs +++ b/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.ComInterfaceGenerator/SharedTypes.ComInterfaces.ICustomStringMarshallingUtf16.cs @@ -63,7 +63,7 @@ file unsafe partial interface InterfaceImplementation internal static int ABI_GetString(System.Runtime.InteropServices.ComWrappers.ComInterfaceDispatch* __this_native, ushort** __invokeRetValUnmanaged__param) { global::SharedTypes.ComInterfaces.ICustomStringMarshallingUtf16 @this = default; - ref ushort* __invokeRetValUnmanaged = ref *__invokeRetValUnmanaged__param; + ushort* __invokeRetValUnmanaged__out = default; string __invokeRetVal = default; int __retVal = default; try @@ -73,11 +73,16 @@ internal static int ABI_GetString(System.Runtime.InteropServices.ComWrappers.Com @this = System.Runtime.InteropServices.ComWrappers.ComInterfaceDispatch.GetInstance(__this_native); __invokeRetVal = @this.GetString(); // Marshal - Convert managed data to native data. - __invokeRetValUnmanaged = (ushort*)global::System.Runtime.InteropServices.Marshalling.Utf16StringMarshaller.ConvertToUnmanaged(__invokeRetVal); + __invokeRetValUnmanaged__out = (ushort*)global::System.Runtime.InteropServices.Marshalling.Utf16StringMarshaller.ConvertToUnmanaged(__invokeRetVal); + // AssignOut - Assign to parameters + *__invokeRetValUnmanaged__param = __invokeRetValUnmanaged__out; } catch (System.Exception __exception) { __retVal = System.Runtime.InteropServices.Marshalling.ExceptionAsHResultMarshaller.ConvertToUnmanaged(__exception); + // CleanupFailure - Perform required cleanup. + global::System.Runtime.InteropServices.Marshalling.Utf16StringMarshaller.Free(__invokeRetValUnmanaged__out); + return __retVal; } return __retVal; @@ -100,6 +105,7 @@ internal static int ABI_SetString(System.Runtime.InteropServices.ComWrappers.Com catch (System.Exception __exception) { __retVal = System.Runtime.InteropServices.Marshalling.ExceptionAsHResultMarshaller.ConvertToUnmanaged(__exception); + return __retVal; } return __retVal; diff --git a/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.ComInterfaceGenerator/SharedTypes.ComInterfaces.IDerived.cs b/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.ComInterfaceGenerator/SharedTypes.ComInterfaces.IDerived.cs index 68edcfb..40a1478 100644 --- a/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.ComInterfaceGenerator/SharedTypes.ComInterfaces.IDerived.cs +++ b/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.ComInterfaceGenerator/SharedTypes.ComInterfaces.IDerived.cs @@ -111,6 +111,7 @@ internal static int ABI_SetName(System.Runtime.InteropServices.ComWrappers.ComIn catch (System.Exception __exception) { __retVal = System.Runtime.InteropServices.Marshalling.ExceptionAsHResultMarshaller.ConvertToUnmanaged(__exception); + return __retVal; } return __retVal; @@ -120,7 +121,7 @@ internal static int ABI_SetName(System.Runtime.InteropServices.ComWrappers.ComIn internal static int ABI_GetName(System.Runtime.InteropServices.ComWrappers.ComInterfaceDispatch* __this_native, ushort** __invokeRetValUnmanaged__param) { global::SharedTypes.ComInterfaces.IDerived @this = default; - ref ushort* __invokeRetValUnmanaged = ref *__invokeRetValUnmanaged__param; + ushort* __invokeRetValUnmanaged__out = default; string __invokeRetVal = default; int __retVal = default; try @@ -130,11 +131,16 @@ internal static int ABI_GetName(System.Runtime.InteropServices.ComWrappers.ComIn @this = System.Runtime.InteropServices.ComWrappers.ComInterfaceDispatch.GetInstance(__this_native); __invokeRetVal = @this.GetName(); // Marshal - Convert managed data to native data. - __invokeRetValUnmanaged = (ushort*)global::System.Runtime.InteropServices.Marshalling.Utf16StringMarshaller.ConvertToUnmanaged(__invokeRetVal); + __invokeRetValUnmanaged__out = (ushort*)global::System.Runtime.InteropServices.Marshalling.Utf16StringMarshaller.ConvertToUnmanaged(__invokeRetVal); + // AssignOut - Assign to parameters + *__invokeRetValUnmanaged__param = __invokeRetValUnmanaged__out; } catch (System.Exception __exception) { __retVal = System.Runtime.InteropServices.Marshalling.ExceptionAsHResultMarshaller.ConvertToUnmanaged(__exception); + // CleanupFailure - Perform required cleanup. + global::System.Runtime.InteropServices.Marshalling.Utf16StringMarshaller.Free(__invokeRetValUnmanaged__out); + return __retVal; } return __retVal; diff --git a/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.ComInterfaceGenerator/SharedTypes.ComInterfaces.IEnumUnknown.cs b/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.ComInterfaceGenerator/SharedTypes.ComInterfaces.IEnumUnknown.cs index ec54555..10a9f34 100644 --- a/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.ComInterfaceGenerator/SharedTypes.ComInterfaces.IEnumUnknown.cs +++ b/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.ComInterfaceGenerator/SharedTypes.ComInterfaces.IEnumUnknown.cs @@ -129,36 +129,54 @@ internal static int ABI_Next(System.Runtime.InteropServices.ComWrappers.ComInter { global::SharedTypes.ComInterfaces.IEnumUnknown @this = default; object[] rgelt = default; - ref uint __pceltFetched_native = ref *__pceltFetched_native__param; + uint __pceltFetched_native__out = default; uint pceltFetched = default; int __retVal = default; // Setup - Perform required setup. int __rgelt_native__numElements; System.Runtime.CompilerServices.Unsafe.SkipInit(out __rgelt_native__numElements); + scoped System.Span __rgelt_native__out = default; try { // Unmarshal - Convert native data to managed data. __retVal = 0; // S_OK global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.GetManagedValuesDestination(rgelt).Clear(); + __rgelt_native__numElements = -1; + rgelt = global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.AllocateContainerForManagedElements(__rgelt_native, __rgelt_native__numElements); @this = System.Runtime.InteropServices.ComWrappers.ComInterfaceDispatch.GetInstance(__this_native); @this.Next(celt, rgelt, out pceltFetched); // Marshal - Convert managed data to native data. - __pceltFetched_native = pceltFetched; + __pceltFetched_native__out = pceltFetched; { __rgelt_native__numElements = global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.GetManagedValuesDestination(rgelt).Length; - System.Span __rgelt_native__nativeSpan = System.Runtime.InteropServices.MemoryMarshal.CreateSpan(ref System.Runtime.CompilerServices.Unsafe.AsRef(in global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.GetUnmanagedValuesSource(__rgelt_native, __rgelt_native__numElements).GetPinnableReference()), __rgelt_native__numElements); + __rgelt_native__out = stackalloc System.IntPtr[__rgelt_native__numElements]; + System.Span __rgelt_native__nativeSpan = __rgelt_native__out; System.Span __rgelt_native__managedSpan = global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.GetManagedValuesDestination(rgelt); for (int __i0 = 0; __i0 < __rgelt_native__numElements; ++__i0) { void* __rgelt_native__nativeSpan____i0__original = (void*)__rgelt_native__nativeSpan[__i0]; __rgelt_native__nativeSpan[__i0] = (System.IntPtr)(void*)global::System.Runtime.InteropServices.Marshalling.ComInterfaceMarshaller.ConvertToUnmanaged(__rgelt_native__managedSpan[__i0]); - global::System.Runtime.InteropServices.Marshalling.ComInterfaceMarshaller.Free(__rgelt_native__nativeSpan____i0__original); } } + + // AssignOut - Assign to parameters + *__pceltFetched_native__param = __pceltFetched_native__out; + __rgelt_native__out.CopyTo(System.Runtime.InteropServices.MemoryMarshal.CreateSpan(ref System.Runtime.CompilerServices.Unsafe.AsRef(in global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.GetUnmanagedValuesSource(__rgelt_native, __rgelt_native__numElements).GetPinnableReference()), __rgelt_native__numElements)); } catch (System.Exception __exception) { __retVal = System.Runtime.InteropServices.Marshalling.ExceptionAsHResultMarshaller.ConvertToUnmanaged(__exception); + // CleanupFailure - Perform required cleanup. + { + System.ReadOnlySpan __rgelt_native__nativeSpan = __rgelt_native__out; + for (int __i0 = 0; __i0 < __rgelt_native__nativeSpan.Length; ++__i0) + { + global::System.Runtime.InteropServices.Marshalling.ComInterfaceMarshaller.Free((void*)__rgelt_native__nativeSpan[__i0]); + } + } + + __rgelt_native__numElements = -1; + return __retVal; } return __retVal; @@ -179,6 +197,7 @@ internal static int ABI_Skip(System.Runtime.InteropServices.ComWrappers.ComInter catch (System.Exception __exception) { __retVal = System.Runtime.InteropServices.Marshalling.ExceptionAsHResultMarshaller.ConvertToUnmanaged(__exception); + return __retVal; } return __retVal; @@ -199,6 +218,7 @@ internal static int ABI_Reset(System.Runtime.InteropServices.ComWrappers.ComInte catch (System.Exception __exception) { __retVal = System.Runtime.InteropServices.Marshalling.ExceptionAsHResultMarshaller.ConvertToUnmanaged(__exception); + return __retVal; } return __retVal; @@ -208,7 +228,7 @@ internal static int ABI_Reset(System.Runtime.InteropServices.ComWrappers.ComInte internal static int ABI_Clone(System.Runtime.InteropServices.ComWrappers.ComInterfaceDispatch* __this_native, void** __ppenum_native__param) { global::SharedTypes.ComInterfaces.IEnumUnknown @this = default; - ref void* __ppenum_native = ref *__ppenum_native__param; + void* __ppenum_native__out = default; global::SharedTypes.ComInterfaces.IEnumUnknown ppenum = default; int __retVal = default; try @@ -218,11 +238,16 @@ internal static int ABI_Clone(System.Runtime.InteropServices.ComWrappers.ComInte @this = System.Runtime.InteropServices.ComWrappers.ComInterfaceDispatch.GetInstance(__this_native); @this.Clone(out ppenum); // Marshal - Convert managed data to native data. - __ppenum_native = (void*)global::System.Runtime.InteropServices.Marshalling.ComInterfaceMarshaller.ConvertToUnmanaged(ppenum); + __ppenum_native__out = (void*)global::System.Runtime.InteropServices.Marshalling.ComInterfaceMarshaller.ConvertToUnmanaged(ppenum); + // AssignOut - Assign to parameters + *__ppenum_native__param = __ppenum_native__out; } catch (System.Exception __exception) { __retVal = System.Runtime.InteropServices.Marshalling.ExceptionAsHResultMarshaller.ConvertToUnmanaged(__exception); + // CleanupFailure - Perform required cleanup. + global::System.Runtime.InteropServices.Marshalling.ComInterfaceMarshaller.Free(__ppenum_native__out); + return __retVal; } return __retVal; diff --git a/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.ComInterfaceGenerator/SharedTypes.ComInterfaces.IFloat.cs b/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.ComInterfaceGenerator/SharedTypes.ComInterfaces.IFloat.cs index dca0a9a..37f5d68 100644 --- a/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.ComInterfaceGenerator/SharedTypes.ComInterfaces.IFloat.cs +++ b/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.ComInterfaceGenerator/SharedTypes.ComInterfaces.IFloat.cs @@ -50,7 +50,7 @@ file unsafe partial interface InterfaceImplementation internal static int ABI_Get(System.Runtime.InteropServices.ComWrappers.ComInterfaceDispatch* __this_native, float* __invokeRetValUnmanaged__param) { global::SharedTypes.ComInterfaces.IFloat @this = default; - ref float __invokeRetValUnmanaged = ref *__invokeRetValUnmanaged__param; + float __invokeRetValUnmanaged__out = default; float __invokeRetVal = default; int __retVal = default; try @@ -60,11 +60,14 @@ internal static int ABI_Get(System.Runtime.InteropServices.ComWrappers.ComInterf @this = System.Runtime.InteropServices.ComWrappers.ComInterfaceDispatch.GetInstance(__this_native); __invokeRetVal = @this.Get(); // Marshal - Convert managed data to native data. - __invokeRetValUnmanaged = __invokeRetVal; + __invokeRetValUnmanaged__out = __invokeRetVal; + // AssignOut - Assign to parameters + *__invokeRetValUnmanaged__param = __invokeRetValUnmanaged__out; } catch (System.Exception __exception) { __retVal = System.Runtime.InteropServices.Marshalling.ExceptionAsHResultMarshaller.ConvertToUnmanaged(__exception); + return __retVal; } return __retVal; @@ -85,6 +88,7 @@ internal static int ABI_Set(System.Runtime.InteropServices.ComWrappers.ComInterf catch (System.Exception __exception) { __retVal = System.Runtime.InteropServices.Marshalling.ExceptionAsHResultMarshaller.ConvertToUnmanaged(__exception); + return __retVal; } return __retVal; diff --git a/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.ComInterfaceGenerator/SharedTypes.ComInterfaces.IGetAndSetInt.cs b/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.ComInterfaceGenerator/SharedTypes.ComInterfaces.IGetAndSetInt.cs index 16faf64..3675daa 100644 --- a/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.ComInterfaceGenerator/SharedTypes.ComInterfaces.IGetAndSetInt.cs +++ b/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.ComInterfaceGenerator/SharedTypes.ComInterfaces.IGetAndSetInt.cs @@ -50,7 +50,7 @@ file unsafe partial interface InterfaceImplementation internal static int ABI_GetInt(System.Runtime.InteropServices.ComWrappers.ComInterfaceDispatch* __this_native, int* __invokeRetValUnmanaged__param) { global::SharedTypes.ComInterfaces.IGetAndSetInt @this = default; - ref int __invokeRetValUnmanaged = ref *__invokeRetValUnmanaged__param; + int __invokeRetValUnmanaged__out = default; int __invokeRetVal = default; int __retVal = default; try @@ -60,11 +60,14 @@ internal static int ABI_GetInt(System.Runtime.InteropServices.ComWrappers.ComInt @this = System.Runtime.InteropServices.ComWrappers.ComInterfaceDispatch.GetInstance(__this_native); __invokeRetVal = @this.GetInt(); // Marshal - Convert managed data to native data. - __invokeRetValUnmanaged = __invokeRetVal; + __invokeRetValUnmanaged__out = __invokeRetVal; + // AssignOut - Assign to parameters + *__invokeRetValUnmanaged__param = __invokeRetValUnmanaged__out; } catch (System.Exception __exception) { __retVal = System.Runtime.InteropServices.Marshalling.ExceptionAsHResultMarshaller.ConvertToUnmanaged(__exception); + return __retVal; } return __retVal; @@ -85,6 +88,7 @@ internal static int ABI_SetInt(System.Runtime.InteropServices.ComWrappers.ComInt catch (System.Exception __exception) { __retVal = System.Runtime.InteropServices.Marshalling.ExceptionAsHResultMarshaller.ConvertToUnmanaged(__exception); + return __retVal; } return __retVal; diff --git a/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.ComInterfaceGenerator/SharedTypes.ComInterfaces.IGetIntArray.cs b/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.ComInterfaceGenerator/SharedTypes.ComInterfaces.IGetIntArray.cs index 946449e..e894238 100644 --- a/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.ComInterfaceGenerator/SharedTypes.ComInterfaces.IGetIntArray.cs +++ b/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.ComInterfaceGenerator/SharedTypes.ComInterfaces.IGetIntArray.cs @@ -38,7 +38,6 @@ finally { // Cleanup - Perform required cleanup. - __retVal_native__numElements = 10; global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.Free(__retVal_native); } @@ -52,7 +51,7 @@ file unsafe partial interface InterfaceImplementation internal static int ABI_GetInts(System.Runtime.InteropServices.ComWrappers.ComInterfaceDispatch* __this_native, int** __invokeRetValUnmanaged__param) { global::SharedTypes.ComInterfaces.IGetIntArray @this = default; - ref int* __invokeRetValUnmanaged = ref *__invokeRetValUnmanaged__param; + int* __invokeRetValUnmanaged__out = default; int[] __invokeRetVal = default; int __retVal = default; // Setup - Perform required setup. @@ -65,12 +64,17 @@ internal static int ABI_GetInts(System.Runtime.InteropServices.ComWrappers.ComIn @this = System.Runtime.InteropServices.ComWrappers.ComInterfaceDispatch.GetInstance(__this_native); __invokeRetVal = @this.GetInts(); // Marshal - Convert managed data to native data. - __invokeRetValUnmanaged = global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.AllocateContainerForUnmanagedElements(__invokeRetVal, out __invokeRetValUnmanaged__numElements); - global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.GetManagedValuesSource(__invokeRetVal).CopyTo(global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.GetUnmanagedValuesDestination(__invokeRetValUnmanaged, __invokeRetValUnmanaged__numElements)); + __invokeRetValUnmanaged__out = global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.AllocateContainerForUnmanagedElements(__invokeRetVal, out __invokeRetValUnmanaged__numElements); + global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.GetManagedValuesSource(__invokeRetVal).CopyTo(global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.GetUnmanagedValuesDestination(__invokeRetValUnmanaged__out, __invokeRetValUnmanaged__numElements)); + // AssignOut - Assign to parameters + *__invokeRetValUnmanaged__param = __invokeRetValUnmanaged__out; } catch (System.Exception __exception) { __retVal = System.Runtime.InteropServices.Marshalling.ExceptionAsHResultMarshaller.ConvertToUnmanaged(__exception); + // CleanupFailure - Perform required cleanup. + global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.Free(__invokeRetValUnmanaged__out); + return __retVal; } return __retVal; diff --git a/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.ComInterfaceGenerator/SharedTypes.ComInterfaces.IInt.cs b/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.ComInterfaceGenerator/SharedTypes.ComInterfaces.IInt.cs index 1c1ed57..c794008 100644 --- a/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.ComInterfaceGenerator/SharedTypes.ComInterfaces.IInt.cs +++ b/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.ComInterfaceGenerator/SharedTypes.ComInterfaces.IInt.cs @@ -102,7 +102,7 @@ file unsafe partial interface InterfaceImplementation internal static int ABI_Get(System.Runtime.InteropServices.ComWrappers.ComInterfaceDispatch* __this_native, int* __invokeRetValUnmanaged__param) { global::SharedTypes.ComInterfaces.IInt @this = default; - ref int __invokeRetValUnmanaged = ref *__invokeRetValUnmanaged__param; + int __invokeRetValUnmanaged__out = default; int __invokeRetVal = default; int __retVal = default; try @@ -112,11 +112,14 @@ internal static int ABI_Get(System.Runtime.InteropServices.ComWrappers.ComInterf @this = System.Runtime.InteropServices.ComWrappers.ComInterfaceDispatch.GetInstance(__this_native); __invokeRetVal = @this.Get(); // Marshal - Convert managed data to native data. - __invokeRetValUnmanaged = __invokeRetVal; + __invokeRetValUnmanaged__out = __invokeRetVal; + // AssignOut - Assign to parameters + *__invokeRetValUnmanaged__param = __invokeRetValUnmanaged__out; } catch (System.Exception __exception) { __retVal = System.Runtime.InteropServices.Marshalling.ExceptionAsHResultMarshaller.ConvertToUnmanaged(__exception); + return __retVal; } return __retVal; @@ -137,6 +140,7 @@ internal static int ABI_Set(System.Runtime.InteropServices.ComWrappers.ComInterf catch (System.Exception __exception) { __retVal = System.Runtime.InteropServices.Marshalling.ExceptionAsHResultMarshaller.ConvertToUnmanaged(__exception); + return __retVal; } return __retVal; @@ -146,7 +150,8 @@ internal static int ABI_Set(System.Runtime.InteropServices.ComWrappers.ComInterf internal static int ABI_SwapRef(System.Runtime.InteropServices.ComWrappers.ComInterfaceDispatch* __this_native, int* __value_native__param) { global::SharedTypes.ComInterfaces.IInt @this = default; - ref int __value_native = ref *__value_native__param; + int __value_native__out = default; + int __value_native = *__value_native__param; int value = default; int __retVal = default; try @@ -157,11 +162,14 @@ internal static int ABI_SwapRef(System.Runtime.InteropServices.ComWrappers.ComIn @this = System.Runtime.InteropServices.ComWrappers.ComInterfaceDispatch.GetInstance(__this_native); @this.SwapRef(ref value); // Marshal - Convert managed data to native data. - __value_native = value; + __value_native__out = value; + // AssignOut - Assign to parameters + *__value_native__param = __value_native__out; } catch (System.Exception __exception) { __retVal = System.Runtime.InteropServices.Marshalling.ExceptionAsHResultMarshaller.ConvertToUnmanaged(__exception); + return __retVal; } return __retVal; @@ -171,7 +179,7 @@ internal static int ABI_SwapRef(System.Runtime.InteropServices.ComWrappers.ComIn internal static int ABI_GetOut(System.Runtime.InteropServices.ComWrappers.ComInterfaceDispatch* __this_native, int* __value_native__param) { global::SharedTypes.ComInterfaces.IInt @this = default; - ref int __value_native = ref *__value_native__param; + int __value_native__out = default; int value = default; int __retVal = default; try @@ -181,11 +189,14 @@ internal static int ABI_GetOut(System.Runtime.InteropServices.ComWrappers.ComInt @this = System.Runtime.InteropServices.ComWrappers.ComInterfaceDispatch.GetInstance(__this_native); @this.GetOut(out value); // Marshal - Convert managed data to native data. - __value_native = value; + __value_native__out = value; + // AssignOut - Assign to parameters + *__value_native__param = __value_native__out; } catch (System.Exception __exception) { __retVal = System.Runtime.InteropServices.Marshalling.ExceptionAsHResultMarshaller.ConvertToUnmanaged(__exception); + return __retVal; } return __retVal; @@ -195,7 +206,7 @@ internal static int ABI_GetOut(System.Runtime.InteropServices.ComWrappers.ComInt internal static int ABI_SetIn(System.Runtime.InteropServices.ComWrappers.ComInterfaceDispatch* __this_native, int* __value_native__param) { global::SharedTypes.ComInterfaces.IInt @this = default; - ref int __value_native = ref *__value_native__param; + int __value_native = *__value_native__param; int value = default; int __retVal = default; try @@ -209,6 +220,7 @@ internal static int ABI_SetIn(System.Runtime.InteropServices.ComWrappers.ComInte catch (System.Exception __exception) { __retVal = System.Runtime.InteropServices.Marshalling.ExceptionAsHResultMarshaller.ConvertToUnmanaged(__exception); + return __retVal; } return __retVal; diff --git a/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.ComInterfaceGenerator/SharedTypes.ComInterfaces.IIntArray.cs b/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.ComInterfaceGenerator/SharedTypes.ComInterfaces.IIntArray.cs index 8922540..7986287 100644 --- a/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.ComInterfaceGenerator/SharedTypes.ComInterfaces.IIntArray.cs +++ b/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.ComInterfaceGenerator/SharedTypes.ComInterfaces.IIntArray.cs @@ -41,7 +41,6 @@ finally { // Cleanup - Perform required cleanup. - __retVal_native__numElements = size; global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.Free(__retVal_native); } @@ -76,7 +75,6 @@ finally { // Cleanup - Perform required cleanup. - __array_native__numElements = __retVal; global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.Free(__array_native); } @@ -198,7 +196,6 @@ finally { // Cleanup - Perform required cleanup. - __array_native__numElements = size; global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.Free(__array_native); } } @@ -210,9 +207,9 @@ file unsafe partial interface InterfaceImplementation internal static int ABI_GetReturn(System.Runtime.InteropServices.ComWrappers.ComInterfaceDispatch* __this_native, int* __size_native__param, int** __invokeRetValUnmanaged__param) { global::SharedTypes.ComInterfaces.IIntArray @this = default; - ref int __size_native = ref *__size_native__param; + int __size_native__out = default; int size = default; - ref int* __invokeRetValUnmanaged = ref *__invokeRetValUnmanaged__param; + int* __invokeRetValUnmanaged__out = default; int[] __invokeRetVal = default; int __retVal = default; // Setup - Perform required setup. @@ -225,13 +222,19 @@ internal static int ABI_GetReturn(System.Runtime.InteropServices.ComWrappers.Com @this = System.Runtime.InteropServices.ComWrappers.ComInterfaceDispatch.GetInstance(__this_native); __invokeRetVal = @this.GetReturn(out size); // Marshal - Convert managed data to native data. - __size_native = size; - __invokeRetValUnmanaged = global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.AllocateContainerForUnmanagedElements(__invokeRetVal, out __invokeRetValUnmanaged__numElements); - global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.GetManagedValuesSource(__invokeRetVal).CopyTo(global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.GetUnmanagedValuesDestination(__invokeRetValUnmanaged, __invokeRetValUnmanaged__numElements)); + __size_native__out = size; + __invokeRetValUnmanaged__out = global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.AllocateContainerForUnmanagedElements(__invokeRetVal, out __invokeRetValUnmanaged__numElements); + global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.GetManagedValuesSource(__invokeRetVal).CopyTo(global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.GetUnmanagedValuesDestination(__invokeRetValUnmanaged__out, __invokeRetValUnmanaged__numElements)); + // AssignOut - Assign to parameters + *__size_native__param = __size_native__out; + *__invokeRetValUnmanaged__param = __invokeRetValUnmanaged__out; } catch (System.Exception __exception) { __retVal = System.Runtime.InteropServices.Marshalling.ExceptionAsHResultMarshaller.ConvertToUnmanaged(__exception); + // CleanupFailure - Perform required cleanup. + global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.Free(__invokeRetValUnmanaged__out); + return __retVal; } return __retVal; @@ -241,9 +244,9 @@ internal static int ABI_GetReturn(System.Runtime.InteropServices.ComWrappers.Com internal static int ABI_GetOut(System.Runtime.InteropServices.ComWrappers.ComInterfaceDispatch* __this_native, int** __array_native__param, int* __invokeRetValUnmanaged__param) { global::SharedTypes.ComInterfaces.IIntArray @this = default; - ref int* __array_native = ref *__array_native__param; + int* __array_native__out = default; int[] array = default; - ref int __invokeRetValUnmanaged = ref *__invokeRetValUnmanaged__param; + int __invokeRetValUnmanaged__out = default; int __invokeRetVal = default; int __retVal = default; // Setup - Perform required setup. @@ -256,13 +259,19 @@ internal static int ABI_GetOut(System.Runtime.InteropServices.ComWrappers.ComInt @this = System.Runtime.InteropServices.ComWrappers.ComInterfaceDispatch.GetInstance(__this_native); __invokeRetVal = @this.GetOut(out array); // Marshal - Convert managed data to native data. - __invokeRetValUnmanaged = __invokeRetVal; - __array_native = global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.AllocateContainerForUnmanagedElements(array, out __array_native__numElements); - global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.GetManagedValuesSource(array).CopyTo(global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.GetUnmanagedValuesDestination(__array_native, __array_native__numElements)); + __invokeRetValUnmanaged__out = __invokeRetVal; + __array_native__out = global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.AllocateContainerForUnmanagedElements(array, out __array_native__numElements); + global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.GetManagedValuesSource(array).CopyTo(global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.GetUnmanagedValuesDestination(__array_native__out, __array_native__numElements)); + // AssignOut - Assign to parameters + *__invokeRetValUnmanaged__param = __invokeRetValUnmanaged__out; + *__array_native__param = __array_native__out; } catch (System.Exception __exception) { __retVal = System.Runtime.InteropServices.Marshalling.ExceptionAsHResultMarshaller.ConvertToUnmanaged(__exception); + // CleanupFailure - Perform required cleanup. + global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.Free(__array_native__out); + return __retVal; } return __retVal; @@ -290,6 +299,7 @@ internal static int ABI_SetContents(System.Runtime.InteropServices.ComWrappers.C catch (System.Exception __exception) { __retVal = System.Runtime.InteropServices.Marshalling.ExceptionAsHResultMarshaller.ConvertToUnmanaged(__exception); + return __retVal; } return __retVal; @@ -304,19 +314,34 @@ internal static int ABI_FillAscending(System.Runtime.InteropServices.ComWrappers // Setup - Perform required setup. int __array_native__numElements; System.Runtime.CompilerServices.Unsafe.SkipInit(out __array_native__numElements); + scoped System.Span __array_native__out = default; try { // Unmarshal - Convert native data to managed data. __retVal = 0; // S_OK global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.GetManagedValuesDestination(array).Clear(); + __array_native__numElements = size; + array = global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.AllocateContainerForManagedElements(__array_native, __array_native__numElements); @this = System.Runtime.InteropServices.ComWrappers.ComInterfaceDispatch.GetInstance(__this_native); @this.FillAscending(array, size); // Marshal - Convert managed data to native data. - global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.GetManagedValuesDestination(array).CopyTo(System.Runtime.InteropServices.MemoryMarshal.CreateSpan(ref System.Runtime.InteropServices.MemoryMarshal.GetReference(global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.GetUnmanagedValuesSource(__array_native, __array_native__numElements)), global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.GetUnmanagedValuesSource(__array_native, __array_native__numElements).Length)); + { + __array_native__numElements = global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.GetManagedValuesSource(array).Length; +#pragma warning disable CS9081 + __array_native__out = stackalloc int[__array_native__numElements]; +#pragma warning restore CS9081 + global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.GetManagedValuesDestination(array).CopyTo(__array_native__out); + } + + // AssignOut - Assign to parameters + __array_native__out.CopyTo(System.Runtime.InteropServices.MemoryMarshal.CreateSpan(ref System.Runtime.CompilerServices.Unsafe.AsRef(in global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.GetUnmanagedValuesSource(__array_native, __array_native__numElements).GetPinnableReference()), __array_native__numElements)); } catch (System.Exception __exception) { __retVal = System.Runtime.InteropServices.Marshalling.ExceptionAsHResultMarshaller.ConvertToUnmanaged(__exception); + // CleanupFailure - Perform required cleanup. + __array_native__numElements = size; + return __retVal; } return __retVal; @@ -331,6 +356,7 @@ internal static int ABI_Double(System.Runtime.InteropServices.ComWrappers.ComInt // Setup - Perform required setup. int __array_native__numElements; System.Runtime.CompilerServices.Unsafe.SkipInit(out __array_native__numElements); + scoped System.Span __array_native__out = default; try { // Unmarshal - Convert native data to managed data. @@ -340,10 +366,17 @@ internal static int ABI_Double(System.Runtime.InteropServices.ComWrappers.ComInt global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.GetUnmanagedValuesSource(__array_native, __array_native__numElements).CopyTo(global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.GetManagedValuesDestination(array)); @this = System.Runtime.InteropServices.ComWrappers.ComInterfaceDispatch.GetInstance(__this_native); @this.Double(array, size); + // AssignOut - Assign to parameters + __array_native__out.CopyTo(System.Runtime.InteropServices.MemoryMarshal.CreateSpan(ref System.Runtime.CompilerServices.Unsafe.AsRef(in global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.GetUnmanagedValuesSource(__array_native, __array_native__numElements).GetPinnableReference()), __array_native__numElements)); + // Cleanup - Perform required cleanup. + __array_native__numElements = size; } catch (System.Exception __exception) { __retVal = System.Runtime.InteropServices.Marshalling.ExceptionAsHResultMarshaller.ConvertToUnmanaged(__exception); + // CleanupFailure - Perform required cleanup. + __array_native__numElements = size; + return __retVal; } return __retVal; @@ -353,7 +386,7 @@ internal static int ABI_Double(System.Runtime.InteropServices.ComWrappers.ComInt internal static int ABI_PassIn(System.Runtime.InteropServices.ComWrappers.ComInterfaceDispatch* __this_native, int** __array_native__param, int size) { global::SharedTypes.ComInterfaces.IIntArray @this = default; - ref int* __array_native = ref *__array_native__param; + int* __array_native = *__array_native__param; int[] array = default; int __retVal = default; // Setup - Perform required setup. @@ -372,6 +405,7 @@ internal static int ABI_PassIn(System.Runtime.InteropServices.ComWrappers.ComInt catch (System.Exception __exception) { __retVal = System.Runtime.InteropServices.Marshalling.ExceptionAsHResultMarshaller.ConvertToUnmanaged(__exception); + return __retVal; } return __retVal; @@ -381,14 +415,13 @@ internal static int ABI_PassIn(System.Runtime.InteropServices.ComWrappers.ComInt internal static int ABI_SwapArray(System.Runtime.InteropServices.ComWrappers.ComInterfaceDispatch* __this_native, int** __array_native__param, int size) { global::SharedTypes.ComInterfaces.IIntArray @this = default; - ref int* __array_native = ref *__array_native__param; + int* __array_native__out = default; + int* __array_native = *__array_native__param; int[] array = default; int __retVal = default; // Setup - Perform required setup. int __array_native__numElements; System.Runtime.CompilerServices.Unsafe.SkipInit(out __array_native__numElements); - bool __array_native__ownOriginal = false; - int* __array_native__original = __array_native; try { // Unmarshal - Convert native data to managed data. @@ -399,22 +432,19 @@ internal static int ABI_SwapArray(System.Runtime.InteropServices.ComWrappers.Com @this = System.Runtime.InteropServices.ComWrappers.ComInterfaceDispatch.GetInstance(__this_native); @this.SwapArray(ref array, size); // Marshal - Convert managed data to native data. - __array_native = global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.AllocateContainerForUnmanagedElements(array, out __array_native__numElements); - __array_native__ownOriginal = true; - global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.GetManagedValuesSource(array).CopyTo(global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.GetUnmanagedValuesDestination(__array_native, __array_native__numElements)); + __array_native__out = global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.AllocateContainerForUnmanagedElements(array, out __array_native__numElements); + global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.GetManagedValuesSource(array).CopyTo(global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.GetUnmanagedValuesDestination(__array_native__out, __array_native__numElements)); + // AssignOut - Assign to parameters + *__array_native__param = __array_native__out; + // Cleanup - Perform required cleanup. + global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.Free(__array_native); } catch (System.Exception __exception) { __retVal = System.Runtime.InteropServices.Marshalling.ExceptionAsHResultMarshaller.ConvertToUnmanaged(__exception); - } - finally - { - // Cleanup - Perform required cleanup. - if (__array_native__ownOriginal) - { - __array_native__numElements = size; - global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.Free(__array_native__original); - } + // CleanupFailure - Perform required cleanup. + global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.Free(__array_native__out); + return __retVal; } return __retVal; diff --git a/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.ComInterfaceGenerator/SharedTypes.ComInterfaces.IInterface.cs b/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.ComInterfaceGenerator/SharedTypes.ComInterfaces.IInterface.cs index ff6245f..9ccea46 100644 --- a/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.ComInterfaceGenerator/SharedTypes.ComInterfaces.IInterface.cs +++ b/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.ComInterfaceGenerator/SharedTypes.ComInterfaces.IInterface.cs @@ -151,7 +151,7 @@ file unsafe partial interface InterfaceImplementation internal static int ABI_Get(System.Runtime.InteropServices.ComWrappers.ComInterfaceDispatch* __this_native, void** __invokeRetValUnmanaged__param) { global::SharedTypes.ComInterfaces.IInterface @this = default; - ref void* __invokeRetValUnmanaged = ref *__invokeRetValUnmanaged__param; + void* __invokeRetValUnmanaged__out = default; global::SharedTypes.ComInterfaces.IInt __invokeRetVal = default; int __retVal = default; try @@ -161,11 +161,16 @@ internal static int ABI_Get(System.Runtime.InteropServices.ComWrappers.ComInterf @this = System.Runtime.InteropServices.ComWrappers.ComInterfaceDispatch.GetInstance(__this_native); __invokeRetVal = @this.Get(); // Marshal - Convert managed data to native data. - __invokeRetValUnmanaged = (void*)global::System.Runtime.InteropServices.Marshalling.ComInterfaceMarshaller.ConvertToUnmanaged(__invokeRetVal); + __invokeRetValUnmanaged__out = (void*)global::System.Runtime.InteropServices.Marshalling.ComInterfaceMarshaller.ConvertToUnmanaged(__invokeRetVal); + // AssignOut - Assign to parameters + *__invokeRetValUnmanaged__param = __invokeRetValUnmanaged__out; } catch (System.Exception __exception) { __retVal = System.Runtime.InteropServices.Marshalling.ExceptionAsHResultMarshaller.ConvertToUnmanaged(__exception); + // CleanupFailure - Perform required cleanup. + global::System.Runtime.InteropServices.Marshalling.ComInterfaceMarshaller.Free(__invokeRetValUnmanaged__out); + return __retVal; } return __retVal; @@ -188,6 +193,7 @@ internal static int ABI_SetInt(System.Runtime.InteropServices.ComWrappers.ComInt catch (System.Exception __exception) { __retVal = System.Runtime.InteropServices.Marshalling.ExceptionAsHResultMarshaller.ConvertToUnmanaged(__exception); + return __retVal; } return __retVal; @@ -197,12 +203,10 @@ internal static int ABI_SetInt(System.Runtime.InteropServices.ComWrappers.ComInt internal static int ABI_SwapRef(System.Runtime.InteropServices.ComWrappers.ComInterfaceDispatch* __this_native, void** __value_native__param) { global::SharedTypes.ComInterfaces.IInterface @this = default; - ref void* __value_native = ref *__value_native__param; + void* __value_native__out = default; + void* __value_native = *__value_native__param; global::SharedTypes.ComInterfaces.IInt value = default; int __retVal = default; - // Setup - Perform required setup. - bool __value_native__ownOriginal = false; - void* __value_native__original = __value_native; try { // Unmarshal - Convert native data to managed data. @@ -211,20 +215,18 @@ internal static int ABI_SwapRef(System.Runtime.InteropServices.ComWrappers.ComIn @this = System.Runtime.InteropServices.ComWrappers.ComInterfaceDispatch.GetInstance(__this_native); @this.SwapRef(ref value); // Marshal - Convert managed data to native data. - __value_native = (void*)global::System.Runtime.InteropServices.Marshalling.ComInterfaceMarshaller.ConvertToUnmanaged(value); - __value_native__ownOriginal = true; + __value_native__out = (void*)global::System.Runtime.InteropServices.Marshalling.ComInterfaceMarshaller.ConvertToUnmanaged(value); + // AssignOut - Assign to parameters + *__value_native__param = __value_native__out; + // Cleanup - Perform required cleanup. + global::System.Runtime.InteropServices.Marshalling.ComInterfaceMarshaller.Free(__value_native); } catch (System.Exception __exception) { __retVal = System.Runtime.InteropServices.Marshalling.ExceptionAsHResultMarshaller.ConvertToUnmanaged(__exception); - } - finally - { - // Cleanup - Perform required cleanup. - if (__value_native__ownOriginal) - { - global::System.Runtime.InteropServices.Marshalling.ComInterfaceMarshaller.Free(__value_native__original); - } + // CleanupFailure - Perform required cleanup. + global::System.Runtime.InteropServices.Marshalling.ComInterfaceMarshaller.Free(__value_native__out); + return __retVal; } return __retVal; @@ -234,7 +236,7 @@ internal static int ABI_SwapRef(System.Runtime.InteropServices.ComWrappers.ComIn internal static int ABI_GetOut(System.Runtime.InteropServices.ComWrappers.ComInterfaceDispatch* __this_native, void** __value_native__param) { global::SharedTypes.ComInterfaces.IInterface @this = default; - ref void* __value_native = ref *__value_native__param; + void* __value_native__out = default; global::SharedTypes.ComInterfaces.IInt value = default; int __retVal = default; try @@ -244,11 +246,16 @@ internal static int ABI_GetOut(System.Runtime.InteropServices.ComWrappers.ComInt @this = System.Runtime.InteropServices.ComWrappers.ComInterfaceDispatch.GetInstance(__this_native); @this.GetOut(out value); // Marshal - Convert managed data to native data. - __value_native = (void*)global::System.Runtime.InteropServices.Marshalling.ComInterfaceMarshaller.ConvertToUnmanaged(value); + __value_native__out = (void*)global::System.Runtime.InteropServices.Marshalling.ComInterfaceMarshaller.ConvertToUnmanaged(value); + // AssignOut - Assign to parameters + *__value_native__param = __value_native__out; } catch (System.Exception __exception) { __retVal = System.Runtime.InteropServices.Marshalling.ExceptionAsHResultMarshaller.ConvertToUnmanaged(__exception); + // CleanupFailure - Perform required cleanup. + global::System.Runtime.InteropServices.Marshalling.ComInterfaceMarshaller.Free(__value_native__out); + return __retVal; } return __retVal; @@ -258,7 +265,7 @@ internal static int ABI_GetOut(System.Runtime.InteropServices.ComWrappers.ComInt internal static int ABI_InInt(System.Runtime.InteropServices.ComWrappers.ComInterfaceDispatch* __this_native, void** __value_native__param) { global::SharedTypes.ComInterfaces.IInterface @this = default; - ref void* __value_native = ref *__value_native__param; + void* __value_native = *__value_native__param; global::SharedTypes.ComInterfaces.IInt value = default; int __retVal = default; try @@ -272,6 +279,7 @@ internal static int ABI_InInt(System.Runtime.InteropServices.ComWrappers.ComInte catch (System.Exception __exception) { __retVal = System.Runtime.InteropServices.Marshalling.ExceptionAsHResultMarshaller.ConvertToUnmanaged(__exception); + return __retVal; } return __retVal; diff --git a/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.ComInterfaceGenerator/SharedTypes.ComInterfaces.IJaggedIntArray.cs b/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.ComInterfaceGenerator/SharedTypes.ComInterfaces.IJaggedIntArray.cs index f7d3efb..219fdbd 100644 --- a/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.ComInterfaceGenerator/SharedTypes.ComInterfaces.IJaggedIntArray.cs +++ b/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.ComInterfaceGenerator/SharedTypes.ComInterfaces.IJaggedIntArray.cs @@ -59,7 +59,6 @@ finally { // Cleanup - Perform required cleanup. - __widths_native__numElements = length; global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.Free(__widths_native); { System.ReadOnlySpan __retVal_native__nativeSpan = global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.GetUnmanagedValuesDestination(__retVal_native, __retVal_native__numElements); @@ -67,12 +66,10 @@ { int __retVal_native__nativeSpan____i0__numElements; System.Runtime.CompilerServices.Unsafe.SkipInit(out __retVal_native__nativeSpan____i0__numElements); - __retVal_native__nativeSpan____i0__numElements = widths[__i0]; global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.Free((int*)__retVal_native__nativeSpan[__i0]); } } - __retVal_native__numElements = length; global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.Free(__retVal_native); } @@ -125,7 +122,6 @@ finally { // Cleanup - Perform required cleanup. - __widths_native__numElements = __retVal; global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.Free(__widths_native); { System.ReadOnlySpan __array_native__nativeSpan = global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.GetUnmanagedValuesDestination(__array_native, __array_native__numElements); @@ -133,12 +129,10 @@ { int __array_native__nativeSpan____i0__numElements; System.Runtime.CompilerServices.Unsafe.SkipInit(out __array_native__nativeSpan____i0__numElements); - __array_native__nativeSpan____i0__numElements = widths[__i0]; global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.Free((int*)__array_native__nativeSpan[__i0]); } } - __array_native__numElements = __retVal; global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.Free(__array_native); } @@ -195,7 +189,6 @@ { int __array_native__nativeSpan____i0__numElements; System.Runtime.CompilerServices.Unsafe.SkipInit(out __array_native__nativeSpan____i0__numElements); - ; global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.Free((int*)__array_native__nativeSpan[__i0]); } } @@ -211,11 +204,11 @@ file unsafe partial interface InterfaceImplementation internal static int ABI_Get(System.Runtime.InteropServices.ComWrappers.ComInterfaceDispatch* __this_native, int** __widths_native__param, int* __length_native__param, System.IntPtr** __invokeRetValUnmanaged__param) { global::SharedTypes.ComInterfaces.IJaggedIntArray @this = default; - ref int* __widths_native = ref *__widths_native__param; + int* __widths_native__out = default; int[] widths = default; - ref int __length_native = ref *__length_native__param; + int __length_native__out = default; int length = default; - ref System.IntPtr* __invokeRetValUnmanaged = ref *__invokeRetValUnmanaged__param; + System.IntPtr* __invokeRetValUnmanaged__out = default; int[][] __invokeRetVal = default; int __retVal = default; // Setup - Perform required setup. @@ -230,13 +223,13 @@ internal static int ABI_Get(System.Runtime.InteropServices.ComWrappers.ComInterf @this = System.Runtime.InteropServices.ComWrappers.ComInterfaceDispatch.GetInstance(__this_native); __invokeRetVal = @this.Get(out widths, out length); // Marshal - Convert managed data to native data. - __length_native = length; - __widths_native = global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.AllocateContainerForUnmanagedElements(widths, out __widths_native__numElements); - global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.GetManagedValuesSource(widths).CopyTo(global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.GetUnmanagedValuesDestination(__widths_native, __widths_native__numElements)); - __invokeRetValUnmanaged = global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.AllocateContainerForUnmanagedElements(__invokeRetVal, out __invokeRetValUnmanaged__numElements); + __length_native__out = length; + __widths_native__out = global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.AllocateContainerForUnmanagedElements(widths, out __widths_native__numElements); + global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.GetManagedValuesSource(widths).CopyTo(global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.GetUnmanagedValuesDestination(__widths_native__out, __widths_native__numElements)); + __invokeRetValUnmanaged__out = global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.AllocateContainerForUnmanagedElements(__invokeRetVal, out __invokeRetValUnmanaged__numElements); { System.ReadOnlySpan __invokeRetValUnmanaged__managedSpan = global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.GetManagedValuesSource(__invokeRetVal); - System.Span __invokeRetValUnmanaged__nativeSpan = global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.GetUnmanagedValuesDestination(__invokeRetValUnmanaged, __invokeRetValUnmanaged__numElements); + System.Span __invokeRetValUnmanaged__nativeSpan = global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.GetUnmanagedValuesDestination(__invokeRetValUnmanaged__out, __invokeRetValUnmanaged__numElements); __invokeRetValUnmanaged__nativeSpan.Clear(); for (int __i0 = 0; __i0 < __invokeRetValUnmanaged__managedSpan.Length; ++__i0) { @@ -246,10 +239,29 @@ internal static int ABI_Get(System.Runtime.InteropServices.ComWrappers.ComInterf global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.GetManagedValuesSource(__invokeRetValUnmanaged__managedSpan[__i0]).CopyTo(global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.GetUnmanagedValuesDestination((int*)__invokeRetValUnmanaged__nativeSpan[__i0], __invokeRetValUnmanaged__nativeSpan____i0__numElements)); } } + + // AssignOut - Assign to parameters + *__length_native__param = __length_native__out; + *__widths_native__param = __widths_native__out; + *__invokeRetValUnmanaged__param = __invokeRetValUnmanaged__out; } catch (System.Exception __exception) { __retVal = System.Runtime.InteropServices.Marshalling.ExceptionAsHResultMarshaller.ConvertToUnmanaged(__exception); + // CleanupFailure - Perform required cleanup. + global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.Free(__widths_native__out); + { + System.ReadOnlySpan __invokeRetValUnmanaged__nativeSpan = global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.GetUnmanagedValuesSource(__invokeRetValUnmanaged__out, __invokeRetValUnmanaged__numElements); + for (int __i0 = 0; __i0 < __invokeRetValUnmanaged__nativeSpan.Length; ++__i0) + { + int __invokeRetValUnmanaged__nativeSpan____i0__numElements; + System.Runtime.CompilerServices.Unsafe.SkipInit(out __invokeRetValUnmanaged__nativeSpan____i0__numElements); + global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.Free((int*)__invokeRetValUnmanaged__nativeSpan[__i0]); + } + } + + global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.Free(__invokeRetValUnmanaged__out); + return __retVal; } return __retVal; @@ -259,11 +271,11 @@ internal static int ABI_Get(System.Runtime.InteropServices.ComWrappers.ComInterf internal static int ABI_Get2(System.Runtime.InteropServices.ComWrappers.ComInterfaceDispatch* __this_native, System.IntPtr** __array_native__param, int** __widths_native__param, int* __invokeRetValUnmanaged__param) { global::SharedTypes.ComInterfaces.IJaggedIntArray @this = default; - ref System.IntPtr* __array_native = ref *__array_native__param; + System.IntPtr* __array_native__out = default; int[][] array = default; - ref int* __widths_native = ref *__widths_native__param; + int* __widths_native__out = default; int[] widths = default; - ref int __invokeRetValUnmanaged = ref *__invokeRetValUnmanaged__param; + int __invokeRetValUnmanaged__out = default; int __invokeRetVal = default; int __retVal = default; // Setup - Perform required setup. @@ -278,13 +290,13 @@ internal static int ABI_Get2(System.Runtime.InteropServices.ComWrappers.ComInter @this = System.Runtime.InteropServices.ComWrappers.ComInterfaceDispatch.GetInstance(__this_native); __invokeRetVal = @this.Get2(out array, out widths); // Marshal - Convert managed data to native data. - __invokeRetValUnmanaged = __invokeRetVal; - __widths_native = global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.AllocateContainerForUnmanagedElements(widths, out __widths_native__numElements); - global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.GetManagedValuesSource(widths).CopyTo(global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.GetUnmanagedValuesDestination(__widths_native, __widths_native__numElements)); - __array_native = global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.AllocateContainerForUnmanagedElements(array, out __array_native__numElements); + __invokeRetValUnmanaged__out = __invokeRetVal; + __widths_native__out = global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.AllocateContainerForUnmanagedElements(widths, out __widths_native__numElements); + global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.GetManagedValuesSource(widths).CopyTo(global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.GetUnmanagedValuesDestination(__widths_native__out, __widths_native__numElements)); + __array_native__out = global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.AllocateContainerForUnmanagedElements(array, out __array_native__numElements); { System.ReadOnlySpan __array_native__managedSpan = global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.GetManagedValuesSource(array); - System.Span __array_native__nativeSpan = global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.GetUnmanagedValuesDestination(__array_native, __array_native__numElements); + System.Span __array_native__nativeSpan = global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.GetUnmanagedValuesDestination(__array_native__out, __array_native__numElements); __array_native__nativeSpan.Clear(); for (int __i0 = 0; __i0 < __array_native__managedSpan.Length; ++__i0) { @@ -294,10 +306,29 @@ internal static int ABI_Get2(System.Runtime.InteropServices.ComWrappers.ComInter global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.GetManagedValuesSource(__array_native__managedSpan[__i0]).CopyTo(global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.GetUnmanagedValuesDestination((int*)__array_native__nativeSpan[__i0], __array_native__nativeSpan____i0__numElements)); } } + + // AssignOut - Assign to parameters + *__invokeRetValUnmanaged__param = __invokeRetValUnmanaged__out; + *__widths_native__param = __widths_native__out; + *__array_native__param = __array_native__out; } catch (System.Exception __exception) { __retVal = System.Runtime.InteropServices.Marshalling.ExceptionAsHResultMarshaller.ConvertToUnmanaged(__exception); + // CleanupFailure - Perform required cleanup. + global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.Free(__widths_native__out); + { + System.ReadOnlySpan __array_native__nativeSpan = global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.GetUnmanagedValuesSource(__array_native__out, __array_native__numElements); + for (int __i0 = 0; __i0 < __array_native__nativeSpan.Length; ++__i0) + { + int __array_native__nativeSpan____i0__numElements; + System.Runtime.CompilerServices.Unsafe.SkipInit(out __array_native__nativeSpan____i0__numElements); + global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.Free((int*)__array_native__nativeSpan[__i0]); + } + } + + global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.Free(__array_native__out); + return __retVal; } return __retVal; @@ -343,6 +374,7 @@ internal static int ABI_Set(System.Runtime.InteropServices.ComWrappers.ComInterf catch (System.Exception __exception) { __retVal = System.Runtime.InteropServices.Marshalling.ExceptionAsHResultMarshaller.ConvertToUnmanaged(__exception); + return __retVal; } return __retVal; diff --git a/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.ComInterfaceGenerator/SharedTypes.ComInterfaces.IPointProvider.cs b/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.ComInterfaceGenerator/SharedTypes.ComInterfaces.IPointProvider.cs index bb1ab1c..5c8dd39 100644 --- a/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.ComInterfaceGenerator/SharedTypes.ComInterfaces.IPointProvider.cs +++ b/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.ComInterfaceGenerator/SharedTypes.ComInterfaces.IPointProvider.cs @@ -57,6 +57,7 @@ file unsafe partial interface InterfaceImplementation catch (System.Exception __exception) { __retVal = System.Runtime.InteropServices.Marshalling.ExceptionAsDefaultMarshaller.ConvertToUnmanaged(__exception); + return __retVal; } return __retVal; @@ -77,6 +78,7 @@ internal static int ABI_SetPoint(System.Runtime.InteropServices.ComWrappers.ComI catch (System.Exception __exception) { __retVal = System.Runtime.InteropServices.Marshalling.ExceptionAsHResultMarshaller.ConvertToUnmanaged(__exception); + return __retVal; } return __retVal; diff --git a/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.ComInterfaceGenerator/SharedTypes.ComInterfaces.IRefStrings.cs b/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.ComInterfaceGenerator/SharedTypes.ComInterfaces.IRefStrings.cs index a80f5a5..1a78ec3 100644 --- a/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.ComInterfaceGenerator/SharedTypes.ComInterfaces.IRefStrings.cs +++ b/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.ComInterfaceGenerator/SharedTypes.ComInterfaces.IRefStrings.cs @@ -101,12 +101,10 @@ file unsafe partial interface InterfaceImplementation internal static int ABI_RefString(System.Runtime.InteropServices.ComWrappers.ComInterfaceDispatch* __this_native, byte** __value_native__param) { global::SharedTypes.ComInterfaces.IRefStrings @this = default; - ref byte* __value_native = ref *__value_native__param; + byte* __value_native__out = default; + byte* __value_native = *__value_native__param; string value = default; int __retVal = default; - // Setup - Perform required setup. - bool __value_native__ownOriginal = false; - byte* __value_native__original = __value_native; try { // Unmarshal - Convert native data to managed data. @@ -115,20 +113,18 @@ internal static int ABI_RefString(System.Runtime.InteropServices.ComWrappers.Com @this = System.Runtime.InteropServices.ComWrappers.ComInterfaceDispatch.GetInstance(__this_native); @this.RefString(ref value); // Marshal - Convert managed data to native data. - __value_native = (byte*)global::System.Runtime.InteropServices.Marshalling.Utf8StringMarshaller.ConvertToUnmanaged(value); - __value_native__ownOriginal = true; + __value_native__out = (byte*)global::System.Runtime.InteropServices.Marshalling.Utf8StringMarshaller.ConvertToUnmanaged(value); + // AssignOut - Assign to parameters + *__value_native__param = __value_native__out; + // Cleanup - Perform required cleanup. + global::System.Runtime.InteropServices.Marshalling.Utf8StringMarshaller.Free(__value_native); } catch (System.Exception __exception) { __retVal = System.Runtime.InteropServices.Marshalling.ExceptionAsHResultMarshaller.ConvertToUnmanaged(__exception); - } - finally - { - // Cleanup - Perform required cleanup. - if (__value_native__ownOriginal) - { - global::System.Runtime.InteropServices.Marshalling.Utf8StringMarshaller.Free(__value_native__original); - } + // CleanupFailure - Perform required cleanup. + global::System.Runtime.InteropServices.Marshalling.Utf8StringMarshaller.Free(__value_native__out); + return __retVal; } return __retVal; @@ -138,7 +134,7 @@ internal static int ABI_RefString(System.Runtime.InteropServices.ComWrappers.Com internal static int ABI_InString(System.Runtime.InteropServices.ComWrappers.ComInterfaceDispatch* __this_native, byte** __value_native__param) { global::SharedTypes.ComInterfaces.IRefStrings @this = default; - ref byte* __value_native = ref *__value_native__param; + byte* __value_native = *__value_native__param; string value = default; int __retVal = default; try @@ -152,6 +148,7 @@ internal static int ABI_InString(System.Runtime.InteropServices.ComWrappers.ComI catch (System.Exception __exception) { __retVal = System.Runtime.InteropServices.Marshalling.ExceptionAsHResultMarshaller.ConvertToUnmanaged(__exception); + return __retVal; } return __retVal; @@ -161,7 +158,7 @@ internal static int ABI_InString(System.Runtime.InteropServices.ComWrappers.ComI internal static int ABI_OutString(System.Runtime.InteropServices.ComWrappers.ComInterfaceDispatch* __this_native, byte** __value_native__param) { global::SharedTypes.ComInterfaces.IRefStrings @this = default; - ref byte* __value_native = ref *__value_native__param; + byte* __value_native__out = default; string value = default; int __retVal = default; try @@ -171,11 +168,16 @@ internal static int ABI_OutString(System.Runtime.InteropServices.ComWrappers.Com @this = System.Runtime.InteropServices.ComWrappers.ComInterfaceDispatch.GetInstance(__this_native); @this.OutString(out value); // Marshal - Convert managed data to native data. - __value_native = (byte*)global::System.Runtime.InteropServices.Marshalling.Utf8StringMarshaller.ConvertToUnmanaged(value); + __value_native__out = (byte*)global::System.Runtime.InteropServices.Marshalling.Utf8StringMarshaller.ConvertToUnmanaged(value); + // AssignOut - Assign to parameters + *__value_native__param = __value_native__out; } catch (System.Exception __exception) { __retVal = System.Runtime.InteropServices.Marshalling.ExceptionAsHResultMarshaller.ConvertToUnmanaged(__exception); + // CleanupFailure - Perform required cleanup. + global::System.Runtime.InteropServices.Marshalling.Utf8StringMarshaller.Free(__value_native__out); + return __retVal; } return __retVal; diff --git a/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.ComInterfaceGenerator/SharedTypes.ComInterfaces.IStatefulCallerAllocatedBufferMarshalling.cs b/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.ComInterfaceGenerator/SharedTypes.ComInterfaces.IStatefulCallerAllocatedBufferMarshalling.cs index 6d3e9b1..7117542 100644 --- a/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.ComInterfaceGenerator/SharedTypes.ComInterfaces.IStatefulCallerAllocatedBufferMarshalling.cs +++ b/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.ComInterfaceGenerator/SharedTypes.ComInterfaces.IStatefulCallerAllocatedBufferMarshalling.cs @@ -214,15 +214,13 @@ internal static int ABI_Method(System.Runtime.InteropServices.ComWrappers.ComInt param = __param_native__marshaller.ToManaged(); @this = System.Runtime.InteropServices.ComWrappers.ComInterfaceDispatch.GetInstance(__this_native); @this.Method(param); + // Cleanup - Perform required cleanup. + __param_native__marshaller.Free(); } catch (System.Exception __exception) { __retVal = System.Runtime.InteropServices.Marshalling.ExceptionAsHResultMarshaller.ConvertToUnmanaged(__exception); - } - finally - { - // Cleanup - Perform required cleanup. - __param_native__marshaller.Free(); + return __retVal; } return __retVal; @@ -232,7 +230,7 @@ internal static int ABI_Method(System.Runtime.InteropServices.ComWrappers.ComInt internal static int ABI_MethodIn(System.Runtime.InteropServices.ComWrappers.ComInterfaceDispatch* __this_native, nint* __param_native__param) { global::SharedTypes.ComInterfaces.IStatefulCallerAllocatedBufferMarshalling @this = default; - ref nint __param_native = ref *__param_native__param; + nint __param_native = *__param_native__param; global::SharedTypes.ComInterfaces.StatefulCallerAllocatedBufferType param = default; int __retVal = default; // Setup - Perform required setup. @@ -246,15 +244,13 @@ internal static int ABI_MethodIn(System.Runtime.InteropServices.ComWrappers.ComI param = __param_native__marshaller.ToManaged(); @this = System.Runtime.InteropServices.ComWrappers.ComInterfaceDispatch.GetInstance(__this_native); @this.MethodIn(in param); + // Cleanup - Perform required cleanup. + __param_native__marshaller.Free(); } catch (System.Exception __exception) { __retVal = System.Runtime.InteropServices.Marshalling.ExceptionAsHResultMarshaller.ConvertToUnmanaged(__exception); - } - finally - { - // Cleanup - Perform required cleanup. - __param_native__marshaller.Free(); + return __retVal; } return __retVal; @@ -264,7 +260,7 @@ internal static int ABI_MethodIn(System.Runtime.InteropServices.ComWrappers.ComI internal static int ABI_MethodOut(System.Runtime.InteropServices.ComWrappers.ComInterfaceDispatch* __this_native, nint* __param_native__param) { global::SharedTypes.ComInterfaces.IStatefulCallerAllocatedBufferMarshalling @this = default; - ref nint __param_native = ref *__param_native__param; + nint __param_native__out = default; global::SharedTypes.ComInterfaces.StatefulCallerAllocatedBufferType param = default; int __retVal = default; // Setup - Perform required setup. @@ -276,16 +272,16 @@ internal static int ABI_MethodOut(System.Runtime.InteropServices.ComWrappers.Com @this = System.Runtime.InteropServices.ComWrappers.ComInterfaceDispatch.GetInstance(__this_native); @this.MethodOut(out param); // PinnedMarshal - Convert managed data to native data that requires the managed data to be pinned. - __param_native = __param_native__marshaller.ToUnmanaged(); + __param_native__out = __param_native__marshaller.ToUnmanaged(); + // AssignOut - Assign to parameters + *__param_native__param = __param_native__out; } catch (System.Exception __exception) { __retVal = System.Runtime.InteropServices.Marshalling.ExceptionAsHResultMarshaller.ConvertToUnmanaged(__exception); - } - finally - { - // Cleanup - Perform required cleanup. + // CleanupFailure - Perform required cleanup. __param_native__marshaller.Free(); + return __retVal; } return __retVal; @@ -295,7 +291,8 @@ internal static int ABI_MethodOut(System.Runtime.InteropServices.ComWrappers.Com internal static int ABI_MethodRef(System.Runtime.InteropServices.ComWrappers.ComInterfaceDispatch* __this_native, nint* __param_native__param) { global::SharedTypes.ComInterfaces.IStatefulCallerAllocatedBufferMarshalling @this = default; - ref nint __param_native = ref *__param_native__param; + nint __param_native__out = default; + nint __param_native = *__param_native__param; global::SharedTypes.ComInterfaces.StatefulCallerAllocatedBufferType param = default; int __retVal = default; // Setup - Perform required setup. @@ -310,16 +307,18 @@ internal static int ABI_MethodRef(System.Runtime.InteropServices.ComWrappers.Com @this = System.Runtime.InteropServices.ComWrappers.ComInterfaceDispatch.GetInstance(__this_native); @this.MethodRef(ref param); // PinnedMarshal - Convert managed data to native data that requires the managed data to be pinned. - __param_native = __param_native__marshaller.ToUnmanaged(); + __param_native__out = __param_native__marshaller.ToUnmanaged(); + // AssignOut - Assign to parameters + *__param_native__param = __param_native__out; + // Cleanup - Perform required cleanup. + __param_native__marshaller.Free(); } catch (System.Exception __exception) { __retVal = System.Runtime.InteropServices.Marshalling.ExceptionAsHResultMarshaller.ConvertToUnmanaged(__exception); - } - finally - { - // Cleanup - Perform required cleanup. + // CleanupFailure - Perform required cleanup. __param_native__marshaller.Free(); + return __retVal; } return __retVal; @@ -329,7 +328,7 @@ internal static int ABI_MethodRef(System.Runtime.InteropServices.ComWrappers.Com internal static int ABI_Return(System.Runtime.InteropServices.ComWrappers.ComInterfaceDispatch* __this_native, nint* __invokeRetValUnmanaged__param) { global::SharedTypes.ComInterfaces.IStatefulCallerAllocatedBufferMarshalling @this = default; - ref nint __invokeRetValUnmanaged = ref *__invokeRetValUnmanaged__param; + nint __invokeRetValUnmanaged__out = default; global::SharedTypes.ComInterfaces.StatefulCallerAllocatedBufferType __invokeRetVal = default; int __retVal = default; // Setup - Perform required setup. @@ -341,16 +340,16 @@ internal static int ABI_Return(System.Runtime.InteropServices.ComWrappers.ComInt @this = System.Runtime.InteropServices.ComWrappers.ComInterfaceDispatch.GetInstance(__this_native); __invokeRetVal = @this.Return(); // PinnedMarshal - Convert managed data to native data that requires the managed data to be pinned. - __invokeRetValUnmanaged = __invokeRetValUnmanaged__marshaller.ToUnmanaged(); + __invokeRetValUnmanaged__out = __invokeRetValUnmanaged__marshaller.ToUnmanaged(); + // AssignOut - Assign to parameters + *__invokeRetValUnmanaged__param = __invokeRetValUnmanaged__out; } catch (System.Exception __exception) { __retVal = System.Runtime.InteropServices.Marshalling.ExceptionAsHResultMarshaller.ConvertToUnmanaged(__exception); - } - finally - { - // Cleanup - Perform required cleanup. + // CleanupFailure - Perform required cleanup. __invokeRetValUnmanaged__marshaller.Free(); + return __retVal; } return __retVal; @@ -371,15 +370,13 @@ internal static nint ABI_ReturnPreserveSig(System.Runtime.InteropServices.ComWra __retVal = @this.ReturnPreserveSig(); // PinnedMarshal - Convert managed data to native data that requires the managed data to be pinned. __retVal_native = __retVal_native__marshaller.ToUnmanaged(); + // Cleanup - Perform required cleanup. + __retVal_native__marshaller.Free(); } catch (System.Exception __exception) { __retVal_native = System.Runtime.InteropServices.Marshalling.ExceptionAsDefaultMarshaller.ConvertToUnmanaged(__exception); - } - finally - { - // Cleanup - Perform required cleanup. - __retVal_native__marshaller.Free(); + return __retVal_native; } return __retVal_native; diff --git a/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.ComInterfaceGenerator/SharedTypes.ComInterfaces.IStatefulCollectionBlittableElement.cs b/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.ComInterfaceGenerator/SharedTypes.ComInterfaces.IStatefulCollectionBlittableElement.cs index f59e629..11b8616 100644 --- a/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.ComInterfaceGenerator/SharedTypes.ComInterfaces.IStatefulCollectionBlittableElement.cs +++ b/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.ComInterfaceGenerator/SharedTypes.ComInterfaces.IStatefulCollectionBlittableElement.cs @@ -248,15 +248,13 @@ internal static int ABI_Method(System.Runtime.InteropServices.ComWrappers.ComInt p = __p_native__marshaller.ToManaged(); @this = System.Runtime.InteropServices.ComWrappers.ComInterfaceDispatch.GetInstance(__this_native); @this.Method(p, size); + // Cleanup - Perform required cleanup. + __p_native__marshaller.Free(); } catch (System.Exception __exception) { __retVal = System.Runtime.InteropServices.Marshalling.ExceptionAsHResultMarshaller.ConvertToUnmanaged(__exception); - } - finally - { - // Cleanup - Perform required cleanup. - __p_native__marshaller.Free(); + return __retVal; } return __retVal; @@ -266,9 +264,9 @@ internal static int ABI_Method(System.Runtime.InteropServices.ComWrappers.ComInt internal static int ABI_MethodIn(System.Runtime.InteropServices.ComWrappers.ComInterfaceDispatch* __this_native, byte** __pIn_native__param, int* __size_native__param) { global::SharedTypes.ComInterfaces.IStatefulCollectionBlittableElement @this = default; - ref byte* __pIn_native = ref *__pIn_native__param; + byte* __pIn_native = *__pIn_native__param; global::SharedTypes.ComInterfaces.StatefulCollection pIn = default; - ref int __size_native = ref *__size_native__param; + int __size_native = *__size_native__param; int size = default; int __retVal = default; // Setup - Perform required setup. @@ -287,15 +285,13 @@ internal static int ABI_MethodIn(System.Runtime.InteropServices.ComWrappers.ComI pIn = __pIn_native__marshaller.ToManaged(); @this = System.Runtime.InteropServices.ComWrappers.ComInterfaceDispatch.GetInstance(__this_native); @this.MethodIn(in pIn, in size); + // Cleanup - Perform required cleanup. + __pIn_native__marshaller.Free(); } catch (System.Exception __exception) { __retVal = System.Runtime.InteropServices.Marshalling.ExceptionAsHResultMarshaller.ConvertToUnmanaged(__exception); - } - finally - { - // Cleanup - Perform required cleanup. - __pIn_native__marshaller.Free(); + return __retVal; } return __retVal; @@ -305,13 +301,12 @@ internal static int ABI_MethodIn(System.Runtime.InteropServices.ComWrappers.ComI internal static int ABI_MethodRef(System.Runtime.InteropServices.ComWrappers.ComInterfaceDispatch* __this_native, byte** __pRef_native__param, int size) { global::SharedTypes.ComInterfaces.IStatefulCollectionBlittableElement @this = default; - ref byte* __pRef_native = ref *__pRef_native__param; + byte* __pRef_native__out = default; + byte* __pRef_native = *__pRef_native__param; global::SharedTypes.ComInterfaces.StatefulCollection pRef = default; int __retVal = default; // Setup - Perform required setup. scoped global::SharedTypes.ComInterfaces.StatefulCollectionMarshaller.Default __pRef_native__marshaller = new(); - bool __pRef_native__ownOriginal = false; - byte* __pRef_native__original = __pRef_native; int __pRef_native__numElements; System.Runtime.CompilerServices.Unsafe.SkipInit(out __pRef_native__numElements); try @@ -325,25 +320,22 @@ internal static int ABI_MethodRef(System.Runtime.InteropServices.ComWrappers.Com pRef = __pRef_native__marshaller.ToManaged(); @this = System.Runtime.InteropServices.ComWrappers.ComInterfaceDispatch.GetInstance(__this_native); @this.MethodRef(ref pRef, size); - // PinnedMarshal - Convert managed data to native data that requires the managed data to be pinned. - __pRef_native = __pRef_native__marshaller.ToUnmanaged(); // Marshal - Convert managed data to native data. __pRef_native__marshaller.FromManaged(pRef); - __pRef_native__ownOriginal = true; __pRef_native__marshaller.GetManagedValuesSource().CopyTo(__pRef_native__marshaller.GetUnmanagedValuesDestination()); + // PinnedMarshal - Convert managed data to native data that requires the managed data to be pinned. + __pRef_native__out = __pRef_native__marshaller.ToUnmanaged(); + // AssignOut - Assign to parameters + *__pRef_native__param = __pRef_native__out; + // Cleanup - Perform required cleanup. + __pRef_native__marshaller.Free(); } catch (System.Exception __exception) { __retVal = System.Runtime.InteropServices.Marshalling.ExceptionAsHResultMarshaller.ConvertToUnmanaged(__exception); - } - finally - { - // Cleanup - Perform required cleanup. - if (__pRef_native__ownOriginal) - { - } - + // CleanupFailure - Perform required cleanup. __pRef_native__marshaller.Free(); + return __retVal; } return __retVal; @@ -353,9 +345,9 @@ internal static int ABI_MethodRef(System.Runtime.InteropServices.ComWrappers.Com internal static int ABI_MethodOut(System.Runtime.InteropServices.ComWrappers.ComInterfaceDispatch* __this_native, byte** __pOut_native__param, int* __size_native__param) { global::SharedTypes.ComInterfaces.IStatefulCollectionBlittableElement @this = default; - ref byte* __pOut_native = ref *__pOut_native__param; + byte* __pOut_native__out = default; global::SharedTypes.ComInterfaces.StatefulCollection pOut = default; - ref int __size_native = ref *__size_native__param; + int __size_native__out = default; int size = default; int __retVal = default; // Setup - Perform required setup. @@ -368,21 +360,22 @@ internal static int ABI_MethodOut(System.Runtime.InteropServices.ComWrappers.Com __retVal = 0; // S_OK @this = System.Runtime.InteropServices.ComWrappers.ComInterfaceDispatch.GetInstance(__this_native); @this.MethodOut(out pOut, out size); - // PinnedMarshal - Convert managed data to native data that requires the managed data to be pinned. - __pOut_native = __pOut_native__marshaller.ToUnmanaged(); // Marshal - Convert managed data to native data. - __size_native = size; + __size_native__out = size; __pOut_native__marshaller.FromManaged(pOut); __pOut_native__marshaller.GetManagedValuesSource().CopyTo(__pOut_native__marshaller.GetUnmanagedValuesDestination()); + // PinnedMarshal - Convert managed data to native data that requires the managed data to be pinned. + __pOut_native__out = __pOut_native__marshaller.ToUnmanaged(); + // AssignOut - Assign to parameters + *__size_native__param = __size_native__out; + *__pOut_native__param = __pOut_native__out; } catch (System.Exception __exception) { __retVal = System.Runtime.InteropServices.Marshalling.ExceptionAsHResultMarshaller.ConvertToUnmanaged(__exception); - } - finally - { - // Cleanup - Perform required cleanup. + // CleanupFailure - Perform required cleanup. __pOut_native__marshaller.Free(); + return __retVal; } return __retVal; @@ -392,7 +385,7 @@ internal static int ABI_MethodOut(System.Runtime.InteropServices.ComWrappers.Com internal static int ABI_Return(System.Runtime.InteropServices.ComWrappers.ComInterfaceDispatch* __this_native, int size, byte** __invokeRetValUnmanaged__param) { global::SharedTypes.ComInterfaces.IStatefulCollectionBlittableElement @this = default; - ref byte* __invokeRetValUnmanaged = ref *__invokeRetValUnmanaged__param; + byte* __invokeRetValUnmanaged__out = default; global::SharedTypes.ComInterfaces.StatefulCollection __invokeRetVal = default; int __retVal = default; // Setup - Perform required setup. @@ -405,20 +398,20 @@ internal static int ABI_Return(System.Runtime.InteropServices.ComWrappers.ComInt __retVal = 0; // S_OK @this = System.Runtime.InteropServices.ComWrappers.ComInterfaceDispatch.GetInstance(__this_native); __invokeRetVal = @this.Return(size); - // PinnedMarshal - Convert managed data to native data that requires the managed data to be pinned. - __invokeRetValUnmanaged = __invokeRetValUnmanaged__marshaller.ToUnmanaged(); // Marshal - Convert managed data to native data. __invokeRetValUnmanaged__marshaller.FromManaged(__invokeRetVal); __invokeRetValUnmanaged__marshaller.GetManagedValuesSource().CopyTo(__invokeRetValUnmanaged__marshaller.GetUnmanagedValuesDestination()); + // PinnedMarshal - Convert managed data to native data that requires the managed data to be pinned. + __invokeRetValUnmanaged__out = __invokeRetValUnmanaged__marshaller.ToUnmanaged(); + // AssignOut - Assign to parameters + *__invokeRetValUnmanaged__param = __invokeRetValUnmanaged__out; } catch (System.Exception __exception) { __retVal = System.Runtime.InteropServices.Marshalling.ExceptionAsHResultMarshaller.ConvertToUnmanaged(__exception); - } - finally - { - // Cleanup - Perform required cleanup. + // CleanupFailure - Perform required cleanup. __invokeRetValUnmanaged__marshaller.Free(); + return __retVal; } return __retVal; @@ -439,20 +432,18 @@ internal static int ABI_Return(System.Runtime.InteropServices.ComWrappers.ComInt // Unmarshal - Convert native data to managed data. @this = System.Runtime.InteropServices.ComWrappers.ComInterfaceDispatch.GetInstance(__this_native); __retVal = @this.ReturnPreserveSig(size); - // PinnedMarshal - Convert managed data to native data that requires the managed data to be pinned. - __retVal_native = __retVal_native__marshaller.ToUnmanaged(); // Marshal - Convert managed data to native data. __retVal_native__marshaller.FromManaged(__retVal); __retVal_native__marshaller.GetManagedValuesSource().CopyTo(__retVal_native__marshaller.GetUnmanagedValuesDestination()); + // PinnedMarshal - Convert managed data to native data that requires the managed data to be pinned. + __retVal_native = __retVal_native__marshaller.ToUnmanaged(); + // Cleanup - Perform required cleanup. + __retVal_native__marshaller.Free(); } catch (System.Exception __exception) { __retVal_native = (byte*)System.Runtime.InteropServices.Marshalling.ExceptionAsDefaultMarshaller.ConvertToUnmanaged(__exception); - } - finally - { - // Cleanup - Perform required cleanup. - __retVal_native__marshaller.Free(); + return __retVal_native; } return __retVal_native; diff --git a/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.ComInterfaceGenerator/SharedTypes.ComInterfaces.IStatefulCollectionStatelessElement.cs b/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.ComInterfaceGenerator/SharedTypes.ComInterfaces.IStatefulCollectionStatelessElement.cs index 54a6518..11e8b3c 100644 --- a/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.ComInterfaceGenerator/SharedTypes.ComInterfaces.IStatefulCollectionStatelessElement.cs +++ b/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.ComInterfaceGenerator/SharedTypes.ComInterfaces.IStatefulCollectionStatelessElement.cs @@ -363,15 +363,13 @@ internal static int ABI_Method(System.Runtime.InteropServices.ComWrappers.ComInt p = __p_native__marshaller.ToManaged(); @this = System.Runtime.InteropServices.ComWrappers.ComInterfaceDispatch.GetInstance(__this_native); @this.Method(p, size); + // Cleanup - Perform required cleanup. + __p_native__marshaller.Free(); } catch (System.Exception __exception) { __retVal = System.Runtime.InteropServices.Marshalling.ExceptionAsHResultMarshaller.ConvertToUnmanaged(__exception); - } - finally - { - // Cleanup - Perform required cleanup. - __p_native__marshaller.Free(); + return __retVal; } return __retVal; @@ -381,9 +379,9 @@ internal static int ABI_Method(System.Runtime.InteropServices.ComWrappers.ComInt internal static int ABI_MethodIn(System.Runtime.InteropServices.ComWrappers.ComInterfaceDispatch* __this_native, byte** __pIn_native__param, int* __size_native__param) { global::SharedTypes.ComInterfaces.IStatefulCollectionStatelessElement @this = default; - ref byte* __pIn_native = ref *__pIn_native__param; + byte* __pIn_native = *__pIn_native__param; global::SharedTypes.ComInterfaces.StatefulCollection pIn = default; - ref int __size_native = ref *__size_native__param; + int __size_native = *__size_native__param; int size = default; int __retVal = default; // Setup - Perform required setup. @@ -410,15 +408,13 @@ internal static int ABI_MethodIn(System.Runtime.InteropServices.ComWrappers.ComI pIn = __pIn_native__marshaller.ToManaged(); @this = System.Runtime.InteropServices.ComWrappers.ComInterfaceDispatch.GetInstance(__this_native); @this.MethodIn(in pIn, in size); + // Cleanup - Perform required cleanup. + __pIn_native__marshaller.Free(); } catch (System.Exception __exception) { __retVal = System.Runtime.InteropServices.Marshalling.ExceptionAsHResultMarshaller.ConvertToUnmanaged(__exception); - } - finally - { - // Cleanup - Perform required cleanup. - __pIn_native__marshaller.Free(); + return __retVal; } return __retVal; @@ -428,13 +424,12 @@ internal static int ABI_MethodIn(System.Runtime.InteropServices.ComWrappers.ComI internal static int ABI_MethodRef(System.Runtime.InteropServices.ComWrappers.ComInterfaceDispatch* __this_native, byte** __pRef_native__param, int size) { global::SharedTypes.ComInterfaces.IStatefulCollectionStatelessElement @this = default; - ref byte* __pRef_native = ref *__pRef_native__param; + byte* __pRef_native__out = default; + byte* __pRef_native = *__pRef_native__param; global::SharedTypes.ComInterfaces.StatefulCollection pRef = default; int __retVal = default; // Setup - Perform required setup. scoped global::SharedTypes.ComInterfaces.StatefulCollectionMarshaller.Default __pRef_native__marshaller = new(); - bool __pRef_native__ownOriginal = false; - byte* __pRef_native__original = __pRef_native; int __pRef_native__numElements; int __pRef_native__lastIndexMarshalled = 0; System.Runtime.CompilerServices.Unsafe.SkipInit(out __pRef_native__numElements); @@ -457,11 +452,8 @@ internal static int ABI_MethodRef(System.Runtime.InteropServices.ComWrappers.Com pRef = __pRef_native__marshaller.ToManaged(); @this = System.Runtime.InteropServices.ComWrappers.ComInterfaceDispatch.GetInstance(__this_native); @this.MethodRef(ref pRef, size); - // PinnedMarshal - Convert managed data to native data that requires the managed data to be pinned. - __pRef_native = __pRef_native__marshaller.ToUnmanaged(); // Marshal - Convert managed data to native data. __pRef_native__marshaller.FromManaged(pRef); - __pRef_native__ownOriginal = true; { System.ReadOnlySpan __pRef_native__managedSpan = __pRef_native__marshaller.GetManagedValuesSource(); System.Span __pRef_native__nativeSpan = __pRef_native__marshaller.GetUnmanagedValuesDestination(); @@ -470,26 +462,36 @@ internal static int ABI_MethodRef(System.Runtime.InteropServices.ComWrappers.Com __pRef_native__nativeSpan[__i0] = global::SharedTypes.ComInterfaces.StatelessTypeMarshaller.ConvertToUnmanaged(__pRef_native__managedSpan[__i0]); } } + + // PinnedMarshal - Convert managed data to native data that requires the managed data to be pinned. + __pRef_native__out = __pRef_native__marshaller.ToUnmanaged(); + // AssignOut - Assign to parameters + *__pRef_native__param = __pRef_native__out; + // Cleanup - Perform required cleanup. + { + System.ReadOnlySpan __pRef_native__nativeSpan = __pRef_native__marshaller.GetUnmanagedValuesSource(__pRef_native__numElements); + for (int __i0 = 0; __i0 < __pRef_native__lastIndexMarshalled; ++__i0) + { + global::SharedTypes.ComInterfaces.StatelessTypeMarshaller.Free(__pRef_native__nativeSpan[__i0]); + } + } + + __pRef_native__marshaller.Free(); } catch (System.Exception __exception) { __retVal = System.Runtime.InteropServices.Marshalling.ExceptionAsHResultMarshaller.ConvertToUnmanaged(__exception); - } - finally - { - // Cleanup - Perform required cleanup. - if (__pRef_native__ownOriginal) + // CleanupFailure - Perform required cleanup. { + System.ReadOnlySpan __pRef_native__nativeSpan = __pRef_native__marshaller.GetUnmanagedValuesSource(__pRef_native__numElements); + for (int __i0 = 0; __i0 < __pRef_native__lastIndexMarshalled; ++__i0) { - System.ReadOnlySpan __pRef_native__nativeSpan = __pRef_native__marshaller.GetUnmanagedValuesSource(__pRef_native__numElements); - for (int __i0 = 0; __i0 < __pRef_native__lastIndexMarshalled; ++__i0) - { - global::SharedTypes.ComInterfaces.StatelessTypeMarshaller.Free(__pRef_native__nativeSpan[__i0]); - } + global::SharedTypes.ComInterfaces.StatelessTypeMarshaller.Free(__pRef_native__nativeSpan[__i0]); } } __pRef_native__marshaller.Free(); + return __retVal; } return __retVal; @@ -499,9 +501,9 @@ internal static int ABI_MethodRef(System.Runtime.InteropServices.ComWrappers.Com internal static int ABI_MethodOut(System.Runtime.InteropServices.ComWrappers.ComInterfaceDispatch* __this_native, byte** __pOut_native__param, int* __size_native__param) { global::SharedTypes.ComInterfaces.IStatefulCollectionStatelessElement @this = default; - ref byte* __pOut_native = ref *__pOut_native__param; + byte* __pOut_native__out = default; global::SharedTypes.ComInterfaces.StatefulCollection pOut = default; - ref int __size_native = ref *__size_native__param; + int __size_native__out = default; int size = default; int __retVal = default; // Setup - Perform required setup. @@ -514,10 +516,8 @@ internal static int ABI_MethodOut(System.Runtime.InteropServices.ComWrappers.Com __retVal = 0; // S_OK @this = System.Runtime.InteropServices.ComWrappers.ComInterfaceDispatch.GetInstance(__this_native); @this.MethodOut(out pOut, out size); - // PinnedMarshal - Convert managed data to native data that requires the managed data to be pinned. - __pOut_native = __pOut_native__marshaller.ToUnmanaged(); // Marshal - Convert managed data to native data. - __size_native = size; + __size_native__out = size; __pOut_native__marshaller.FromManaged(pOut); { System.ReadOnlySpan __pOut_native__managedSpan = __pOut_native__marshaller.GetManagedValuesSource(); @@ -528,15 +528,27 @@ internal static int ABI_MethodOut(System.Runtime.InteropServices.ComWrappers.Com __pOut_native__nativeSpan[__i0] = global::SharedTypes.ComInterfaces.StatelessTypeMarshaller.ConvertToUnmanaged(__pOut_native__managedSpan[__i0]); } } + + // PinnedMarshal - Convert managed data to native data that requires the managed data to be pinned. + __pOut_native__out = __pOut_native__marshaller.ToUnmanaged(); + // AssignOut - Assign to parameters + *__size_native__param = __size_native__out; + *__pOut_native__param = __pOut_native__out; } catch (System.Exception __exception) { __retVal = System.Runtime.InteropServices.Marshalling.ExceptionAsHResultMarshaller.ConvertToUnmanaged(__exception); - } - finally - { - // Cleanup - Perform required cleanup. + // CleanupFailure - Perform required cleanup. + { + System.ReadOnlySpan __pOut_native__nativeSpan = __pOut_native__marshaller.GetUnmanagedValuesSource(__pOut_native__numElements); + for (int __i0 = 0; __i0 < __pOut_native__nativeSpan.Length; ++__i0) + { + global::SharedTypes.ComInterfaces.StatelessTypeMarshaller.Free(__pOut_native__nativeSpan[__i0]); + } + } + __pOut_native__marshaller.Free(); + return __retVal; } return __retVal; @@ -546,7 +558,7 @@ internal static int ABI_MethodOut(System.Runtime.InteropServices.ComWrappers.Com internal static int ABI_Return(System.Runtime.InteropServices.ComWrappers.ComInterfaceDispatch* __this_native, int size, byte** __invokeRetValUnmanaged__param) { global::SharedTypes.ComInterfaces.IStatefulCollectionStatelessElement @this = default; - ref byte* __invokeRetValUnmanaged = ref *__invokeRetValUnmanaged__param; + byte* __invokeRetValUnmanaged__out = default; global::SharedTypes.ComInterfaces.StatefulCollection __invokeRetVal = default; int __retVal = default; // Setup - Perform required setup. @@ -559,8 +571,6 @@ internal static int ABI_Return(System.Runtime.InteropServices.ComWrappers.ComInt __retVal = 0; // S_OK @this = System.Runtime.InteropServices.ComWrappers.ComInterfaceDispatch.GetInstance(__this_native); __invokeRetVal = @this.Return(size); - // PinnedMarshal - Convert managed data to native data that requires the managed data to be pinned. - __invokeRetValUnmanaged = __invokeRetValUnmanaged__marshaller.ToUnmanaged(); // Marshal - Convert managed data to native data. __invokeRetValUnmanaged__marshaller.FromManaged(__invokeRetVal); { @@ -572,15 +582,26 @@ internal static int ABI_Return(System.Runtime.InteropServices.ComWrappers.ComInt __invokeRetValUnmanaged__nativeSpan[__i0] = global::SharedTypes.ComInterfaces.StatelessTypeMarshaller.ConvertToUnmanaged(__invokeRetValUnmanaged__managedSpan[__i0]); } } + + // PinnedMarshal - Convert managed data to native data that requires the managed data to be pinned. + __invokeRetValUnmanaged__out = __invokeRetValUnmanaged__marshaller.ToUnmanaged(); + // AssignOut - Assign to parameters + *__invokeRetValUnmanaged__param = __invokeRetValUnmanaged__out; } catch (System.Exception __exception) { __retVal = System.Runtime.InteropServices.Marshalling.ExceptionAsHResultMarshaller.ConvertToUnmanaged(__exception); - } - finally - { - // Cleanup - Perform required cleanup. + // CleanupFailure - Perform required cleanup. + { + System.ReadOnlySpan __invokeRetValUnmanaged__nativeSpan = __invokeRetValUnmanaged__marshaller.GetUnmanagedValuesSource(__invokeRetValUnmanaged__numElements); + for (int __i0 = 0; __i0 < __invokeRetValUnmanaged__nativeSpan.Length; ++__i0) + { + global::SharedTypes.ComInterfaces.StatelessTypeMarshaller.Free(__invokeRetValUnmanaged__nativeSpan[__i0]); + } + } + __invokeRetValUnmanaged__marshaller.Free(); + return __retVal; } return __retVal; @@ -602,8 +623,6 @@ internal static int ABI_Return(System.Runtime.InteropServices.ComWrappers.ComInt // Unmarshal - Convert native data to managed data. @this = System.Runtime.InteropServices.ComWrappers.ComInterfaceDispatch.GetInstance(__this_native); __retVal = @this.ReturnPreserveSig(size); - // PinnedMarshal - Convert managed data to native data that requires the managed data to be pinned. - __retVal_native = __retVal_native__marshaller.ToUnmanaged(); // Marshal - Convert managed data to native data. __retVal_native__marshaller.FromManaged(__retVal); { @@ -614,15 +633,16 @@ internal static int ABI_Return(System.Runtime.InteropServices.ComWrappers.ComInt __retVal_native__nativeSpan[__i0] = global::SharedTypes.ComInterfaces.StatelessTypeMarshaller.ConvertToUnmanaged(__retVal_native__managedSpan[__i0]); } } + + // PinnedMarshal - Convert managed data to native data that requires the managed data to be pinned. + __retVal_native = __retVal_native__marshaller.ToUnmanaged(); + // Cleanup - Perform required cleanup. + __retVal_native__marshaller.Free(); } catch (System.Exception __exception) { __retVal_native = (byte*)System.Runtime.InteropServices.Marshalling.ExceptionAsDefaultMarshaller.ConvertToUnmanaged(__exception); - } - finally - { - // Cleanup - Perform required cleanup. - __retVal_native__marshaller.Free(); + return __retVal_native; } return __retVal_native; diff --git a/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.ComInterfaceGenerator/SharedTypes.ComInterfaces.IStatefulFinallyMarshalling.cs b/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.ComInterfaceGenerator/SharedTypes.ComInterfaces.IStatefulFinallyMarshalling.cs index f68a10e..80633b3 100644 --- a/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.ComInterfaceGenerator/SharedTypes.ComInterfaces.IStatefulFinallyMarshalling.cs +++ b/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.ComInterfaceGenerator/SharedTypes.ComInterfaces.IStatefulFinallyMarshalling.cs @@ -229,7 +229,6 @@ file unsafe partial interface InterfaceImplementation [System.Runtime.InteropServices.UnmanagedCallersOnlyAttribute(CallConvs = new[] { typeof(System.Runtime.CompilerServices.CallConvMemberFunction) })] internal static int ABI_Method(System.Runtime.InteropServices.ComWrappers.ComInterfaceDispatch* __this_native, nint __param_native) { - bool __invokeSucceeded = default; global::SharedTypes.ComInterfaces.IStatefulFinallyMarshalling @this = default; global::SharedTypes.ComInterfaces.StatefulFinallyType param = default; int __retVal = default; @@ -242,23 +241,16 @@ internal static int ABI_Method(System.Runtime.InteropServices.ComWrappers.ComInt // Unmarshal - Convert native data to managed data. __retVal = 0; // S_OK @this = System.Runtime.InteropServices.ComWrappers.ComInterfaceDispatch.GetInstance(__this_native); + // GuaranteedUnmarshal - Convert native data to managed data even in the case of an exception during the non-cleanup phases. + param = __param_native__marshaller.ToManagedFinally(); @this.Method(param); - __invokeSucceeded = true; + // Cleanup - Perform required cleanup. + __param_native__marshaller.Free(); } catch (System.Exception __exception) { __retVal = System.Runtime.InteropServices.Marshalling.ExceptionAsHResultMarshaller.ConvertToUnmanaged(__exception); - } - finally - { - if (__invokeSucceeded) - { - // GuaranteedUnmarshal - Convert native data to managed data even in the case of an exception during the non-cleanup phases. - param = __param_native__marshaller.ToManagedFinally(); - } - - // Cleanup - Perform required cleanup. - __param_native__marshaller.Free(); + return __retVal; } return __retVal; @@ -267,9 +259,8 @@ internal static int ABI_Method(System.Runtime.InteropServices.ComWrappers.ComInt [System.Runtime.InteropServices.UnmanagedCallersOnlyAttribute(CallConvs = new[] { typeof(System.Runtime.CompilerServices.CallConvMemberFunction) })] internal static int ABI_MethodIn(System.Runtime.InteropServices.ComWrappers.ComInterfaceDispatch* __this_native, nint* __param_native__param) { - bool __invokeSucceeded = default; global::SharedTypes.ComInterfaces.IStatefulFinallyMarshalling @this = default; - ref nint __param_native = ref *__param_native__param; + nint __param_native = *__param_native__param; global::SharedTypes.ComInterfaces.StatefulFinallyType param = default; int __retVal = default; // Setup - Perform required setup. @@ -281,23 +272,16 @@ internal static int ABI_MethodIn(System.Runtime.InteropServices.ComWrappers.ComI // Unmarshal - Convert native data to managed data. __retVal = 0; // S_OK @this = System.Runtime.InteropServices.ComWrappers.ComInterfaceDispatch.GetInstance(__this_native); + // GuaranteedUnmarshal - Convert native data to managed data even in the case of an exception during the non-cleanup phases. + param = __param_native__marshaller.ToManagedFinally(); @this.MethodIn(in param); - __invokeSucceeded = true; + // Cleanup - Perform required cleanup. + __param_native__marshaller.Free(); } catch (System.Exception __exception) { __retVal = System.Runtime.InteropServices.Marshalling.ExceptionAsHResultMarshaller.ConvertToUnmanaged(__exception); - } - finally - { - if (__invokeSucceeded) - { - // GuaranteedUnmarshal - Convert native data to managed data even in the case of an exception during the non-cleanup phases. - param = __param_native__marshaller.ToManagedFinally(); - } - - // Cleanup - Perform required cleanup. - __param_native__marshaller.Free(); + return __retVal; } return __retVal; @@ -307,7 +291,7 @@ internal static int ABI_MethodIn(System.Runtime.InteropServices.ComWrappers.ComI internal static int ABI_MethodOut(System.Runtime.InteropServices.ComWrappers.ComInterfaceDispatch* __this_native, nint* __param_native__param) { global::SharedTypes.ComInterfaces.IStatefulFinallyMarshalling @this = default; - ref nint __param_native = ref *__param_native__param; + nint __param_native__out = default; global::SharedTypes.ComInterfaces.StatefulFinallyType param = default; int __retVal = default; // Setup - Perform required setup. @@ -318,19 +302,19 @@ internal static int ABI_MethodOut(System.Runtime.InteropServices.ComWrappers.Com __retVal = 0; // S_OK @this = System.Runtime.InteropServices.ComWrappers.ComInterfaceDispatch.GetInstance(__this_native); @this.MethodOut(out param); - // PinnedMarshal - Convert managed data to native data that requires the managed data to be pinned. - __param_native = __param_native__marshaller.ToUnmanaged(); // Marshal - Convert managed data to native data. __param_native__marshaller.FromManaged(param); + // PinnedMarshal - Convert managed data to native data that requires the managed data to be pinned. + __param_native__out = __param_native__marshaller.ToUnmanaged(); + // AssignOut - Assign to parameters + *__param_native__param = __param_native__out; } catch (System.Exception __exception) { __retVal = System.Runtime.InteropServices.Marshalling.ExceptionAsHResultMarshaller.ConvertToUnmanaged(__exception); - } - finally - { - // Cleanup - Perform required cleanup. + // CleanupFailure - Perform required cleanup. __param_native__marshaller.Free(); + return __retVal; } return __retVal; @@ -339,9 +323,9 @@ internal static int ABI_MethodOut(System.Runtime.InteropServices.ComWrappers.Com [System.Runtime.InteropServices.UnmanagedCallersOnlyAttribute(CallConvs = new[] { typeof(System.Runtime.CompilerServices.CallConvMemberFunction) })] internal static int ABI_MethodRef(System.Runtime.InteropServices.ComWrappers.ComInterfaceDispatch* __this_native, nint* __param_native__param) { - bool __invokeSucceeded = default; global::SharedTypes.ComInterfaces.IStatefulFinallyMarshalling @this = default; - ref nint __param_native = ref *__param_native__param; + nint __param_native__out = default; + nint __param_native = *__param_native__param; global::SharedTypes.ComInterfaces.StatefulFinallyType param = default; int __retVal = default; // Setup - Perform required setup. @@ -353,27 +337,24 @@ internal static int ABI_MethodRef(System.Runtime.InteropServices.ComWrappers.Com // Unmarshal - Convert native data to managed data. __retVal = 0; // S_OK @this = System.Runtime.InteropServices.ComWrappers.ComInterfaceDispatch.GetInstance(__this_native); + // GuaranteedUnmarshal - Convert native data to managed data even in the case of an exception during the non-cleanup phases. + param = __param_native__marshaller.ToManagedFinally(); @this.MethodRef(ref param); - __invokeSucceeded = true; - // PinnedMarshal - Convert managed data to native data that requires the managed data to be pinned. - __param_native = __param_native__marshaller.ToUnmanaged(); // Marshal - Convert managed data to native data. __param_native__marshaller.FromManaged(param); + // PinnedMarshal - Convert managed data to native data that requires the managed data to be pinned. + __param_native__out = __param_native__marshaller.ToUnmanaged(); + // AssignOut - Assign to parameters + *__param_native__param = __param_native__out; + // Cleanup - Perform required cleanup. + __param_native__marshaller.Free(); } catch (System.Exception __exception) { __retVal = System.Runtime.InteropServices.Marshalling.ExceptionAsHResultMarshaller.ConvertToUnmanaged(__exception); - } - finally - { - if (__invokeSucceeded) - { - // GuaranteedUnmarshal - Convert native data to managed data even in the case of an exception during the non-cleanup phases. - param = __param_native__marshaller.ToManagedFinally(); - } - - // Cleanup - Perform required cleanup. + // CleanupFailure - Perform required cleanup. __param_native__marshaller.Free(); + return __retVal; } return __retVal; @@ -383,7 +364,7 @@ internal static int ABI_MethodRef(System.Runtime.InteropServices.ComWrappers.Com internal static int ABI_Return(System.Runtime.InteropServices.ComWrappers.ComInterfaceDispatch* __this_native, nint* __invokeRetValUnmanaged__param) { global::SharedTypes.ComInterfaces.IStatefulFinallyMarshalling @this = default; - ref nint __invokeRetValUnmanaged = ref *__invokeRetValUnmanaged__param; + nint __invokeRetValUnmanaged__out = default; global::SharedTypes.ComInterfaces.StatefulFinallyType __invokeRetVal = default; int __retVal = default; // Setup - Perform required setup. @@ -394,19 +375,19 @@ internal static int ABI_Return(System.Runtime.InteropServices.ComWrappers.ComInt __retVal = 0; // S_OK @this = System.Runtime.InteropServices.ComWrappers.ComInterfaceDispatch.GetInstance(__this_native); __invokeRetVal = @this.Return(); - // PinnedMarshal - Convert managed data to native data that requires the managed data to be pinned. - __invokeRetValUnmanaged = __invokeRetValUnmanaged__marshaller.ToUnmanaged(); // Marshal - Convert managed data to native data. __invokeRetValUnmanaged__marshaller.FromManaged(__invokeRetVal); + // PinnedMarshal - Convert managed data to native data that requires the managed data to be pinned. + __invokeRetValUnmanaged__out = __invokeRetValUnmanaged__marshaller.ToUnmanaged(); + // AssignOut - Assign to parameters + *__invokeRetValUnmanaged__param = __invokeRetValUnmanaged__out; } catch (System.Exception __exception) { __retVal = System.Runtime.InteropServices.Marshalling.ExceptionAsHResultMarshaller.ConvertToUnmanaged(__exception); - } - finally - { - // Cleanup - Perform required cleanup. + // CleanupFailure - Perform required cleanup. __invokeRetValUnmanaged__marshaller.Free(); + return __retVal; } return __retVal; @@ -425,19 +406,17 @@ internal static nint ABI_ReturnPreserveSig(System.Runtime.InteropServices.ComWra // Unmarshal - Convert native data to managed data. @this = System.Runtime.InteropServices.ComWrappers.ComInterfaceDispatch.GetInstance(__this_native); __retVal = @this.ReturnPreserveSig(); - // PinnedMarshal - Convert managed data to native data that requires the managed data to be pinned. - __retVal_native = __retVal_native__marshaller.ToUnmanaged(); // Marshal - Convert managed data to native data. __retVal_native__marshaller.FromManaged(__retVal); + // PinnedMarshal - Convert managed data to native data that requires the managed data to be pinned. + __retVal_native = __retVal_native__marshaller.ToUnmanaged(); + // Cleanup - Perform required cleanup. + __retVal_native__marshaller.Free(); } catch (System.Exception __exception) { __retVal_native = System.Runtime.InteropServices.Marshalling.ExceptionAsDefaultMarshaller.ConvertToUnmanaged(__exception); - } - finally - { - // Cleanup - Perform required cleanup. - __retVal_native__marshaller.Free(); + return __retVal_native; } return __retVal_native; diff --git a/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.ComInterfaceGenerator/SharedTypes.ComInterfaces.IStatefulMarshalling.cs b/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.ComInterfaceGenerator/SharedTypes.ComInterfaces.IStatefulMarshalling.cs index 049230d..59d7d8a 100644 --- a/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.ComInterfaceGenerator/SharedTypes.ComInterfaces.IStatefulMarshalling.cs +++ b/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.ComInterfaceGenerator/SharedTypes.ComInterfaces.IStatefulMarshalling.cs @@ -222,15 +222,13 @@ internal static int ABI_Method(System.Runtime.InteropServices.ComWrappers.ComInt param = __param_native__marshaller.ToManaged(); @this = System.Runtime.InteropServices.ComWrappers.ComInterfaceDispatch.GetInstance(__this_native); @this.Method(param); + // Cleanup - Perform required cleanup. + __param_native__marshaller.Free(); } catch (System.Exception __exception) { __retVal = System.Runtime.InteropServices.Marshalling.ExceptionAsHResultMarshaller.ConvertToUnmanaged(__exception); - } - finally - { - // Cleanup - Perform required cleanup. - __param_native__marshaller.Free(); + return __retVal; } return __retVal; @@ -240,7 +238,7 @@ internal static int ABI_Method(System.Runtime.InteropServices.ComWrappers.ComInt internal static int ABI_MethodIn(System.Runtime.InteropServices.ComWrappers.ComInterfaceDispatch* __this_native, nint* __param_native__param) { global::SharedTypes.ComInterfaces.IStatefulMarshalling @this = default; - ref nint __param_native = ref *__param_native__param; + nint __param_native = *__param_native__param; global::SharedTypes.ComInterfaces.StatefulType param = default; int __retVal = default; // Setup - Perform required setup. @@ -254,15 +252,13 @@ internal static int ABI_MethodIn(System.Runtime.InteropServices.ComWrappers.ComI param = __param_native__marshaller.ToManaged(); @this = System.Runtime.InteropServices.ComWrappers.ComInterfaceDispatch.GetInstance(__this_native); @this.MethodIn(in param); + // Cleanup - Perform required cleanup. + __param_native__marshaller.Free(); } catch (System.Exception __exception) { __retVal = System.Runtime.InteropServices.Marshalling.ExceptionAsHResultMarshaller.ConvertToUnmanaged(__exception); - } - finally - { - // Cleanup - Perform required cleanup. - __param_native__marshaller.Free(); + return __retVal; } return __retVal; @@ -272,7 +268,7 @@ internal static int ABI_MethodIn(System.Runtime.InteropServices.ComWrappers.ComI internal static int ABI_MethodOut(System.Runtime.InteropServices.ComWrappers.ComInterfaceDispatch* __this_native, nint* __param_native__param) { global::SharedTypes.ComInterfaces.IStatefulMarshalling @this = default; - ref nint __param_native = ref *__param_native__param; + nint __param_native__out = default; global::SharedTypes.ComInterfaces.StatefulType param = default; int __retVal = default; // Setup - Perform required setup. @@ -285,19 +281,19 @@ internal static int ABI_MethodOut(System.Runtime.InteropServices.ComWrappers.Com @this.MethodOut(out param); // NotifyForSuccessfulInvoke - Keep alive any managed objects that need to stay alive across the call. __param_native__marshaller.OnInvoked(); - // PinnedMarshal - Convert managed data to native data that requires the managed data to be pinned. - __param_native = __param_native__marshaller.ToUnmanaged(); // Marshal - Convert managed data to native data. __param_native__marshaller.FromManaged(param); + // PinnedMarshal - Convert managed data to native data that requires the managed data to be pinned. + __param_native__out = __param_native__marshaller.ToUnmanaged(); + // AssignOut - Assign to parameters + *__param_native__param = __param_native__out; } catch (System.Exception __exception) { __retVal = System.Runtime.InteropServices.Marshalling.ExceptionAsHResultMarshaller.ConvertToUnmanaged(__exception); - } - finally - { - // Cleanup - Perform required cleanup. + // CleanupFailure - Perform required cleanup. __param_native__marshaller.Free(); + return __retVal; } return __retVal; @@ -307,7 +303,8 @@ internal static int ABI_MethodOut(System.Runtime.InteropServices.ComWrappers.Com internal static int ABI_MethodRef(System.Runtime.InteropServices.ComWrappers.ComInterfaceDispatch* __this_native, nint* __param_native__param) { global::SharedTypes.ComInterfaces.IStatefulMarshalling @this = default; - ref nint __param_native = ref *__param_native__param; + nint __param_native__out = default; + nint __param_native = *__param_native__param; global::SharedTypes.ComInterfaces.StatefulType param = default; int __retVal = default; // Setup - Perform required setup. @@ -323,19 +320,21 @@ internal static int ABI_MethodRef(System.Runtime.InteropServices.ComWrappers.Com @this.MethodRef(ref param); // NotifyForSuccessfulInvoke - Keep alive any managed objects that need to stay alive across the call. __param_native__marshaller.OnInvoked(); - // PinnedMarshal - Convert managed data to native data that requires the managed data to be pinned. - __param_native = __param_native__marshaller.ToUnmanaged(); // Marshal - Convert managed data to native data. __param_native__marshaller.FromManaged(param); + // PinnedMarshal - Convert managed data to native data that requires the managed data to be pinned. + __param_native__out = __param_native__marshaller.ToUnmanaged(); + // AssignOut - Assign to parameters + *__param_native__param = __param_native__out; + // Cleanup - Perform required cleanup. + __param_native__marshaller.Free(); } catch (System.Exception __exception) { __retVal = System.Runtime.InteropServices.Marshalling.ExceptionAsHResultMarshaller.ConvertToUnmanaged(__exception); - } - finally - { - // Cleanup - Perform required cleanup. + // CleanupFailure - Perform required cleanup. __param_native__marshaller.Free(); + return __retVal; } return __retVal; @@ -345,7 +344,7 @@ internal static int ABI_MethodRef(System.Runtime.InteropServices.ComWrappers.Com internal static int ABI_Return(System.Runtime.InteropServices.ComWrappers.ComInterfaceDispatch* __this_native, nint* __invokeRetValUnmanaged__param) { global::SharedTypes.ComInterfaces.IStatefulMarshalling @this = default; - ref nint __invokeRetValUnmanaged = ref *__invokeRetValUnmanaged__param; + nint __invokeRetValUnmanaged__out = default; global::SharedTypes.ComInterfaces.StatefulType __invokeRetVal = default; int __retVal = default; // Setup - Perform required setup. @@ -358,19 +357,19 @@ internal static int ABI_Return(System.Runtime.InteropServices.ComWrappers.ComInt __invokeRetVal = @this.Return(); // NotifyForSuccessfulInvoke - Keep alive any managed objects that need to stay alive across the call. __invokeRetValUnmanaged__marshaller.OnInvoked(); - // PinnedMarshal - Convert managed data to native data that requires the managed data to be pinned. - __invokeRetValUnmanaged = __invokeRetValUnmanaged__marshaller.ToUnmanaged(); // Marshal - Convert managed data to native data. __invokeRetValUnmanaged__marshaller.FromManaged(__invokeRetVal); + // PinnedMarshal - Convert managed data to native data that requires the managed data to be pinned. + __invokeRetValUnmanaged__out = __invokeRetValUnmanaged__marshaller.ToUnmanaged(); + // AssignOut - Assign to parameters + *__invokeRetValUnmanaged__param = __invokeRetValUnmanaged__out; } catch (System.Exception __exception) { __retVal = System.Runtime.InteropServices.Marshalling.ExceptionAsHResultMarshaller.ConvertToUnmanaged(__exception); - } - finally - { - // Cleanup - Perform required cleanup. + // CleanupFailure - Perform required cleanup. __invokeRetValUnmanaged__marshaller.Free(); + return __retVal; } return __retVal; @@ -391,19 +390,17 @@ internal static nint ABI_ReturnPreserveSig(System.Runtime.InteropServices.ComWra __retVal = @this.ReturnPreserveSig(); // NotifyForSuccessfulInvoke - Keep alive any managed objects that need to stay alive across the call. __retVal_native__marshaller.OnInvoked(); - // PinnedMarshal - Convert managed data to native data that requires the managed data to be pinned. - __retVal_native = __retVal_native__marshaller.ToUnmanaged(); // Marshal - Convert managed data to native data. __retVal_native__marshaller.FromManaged(__retVal); + // PinnedMarshal - Convert managed data to native data that requires the managed data to be pinned. + __retVal_native = __retVal_native__marshaller.ToUnmanaged(); + // Cleanup - Perform required cleanup. + __retVal_native__marshaller.Free(); } catch (System.Exception __exception) { __retVal_native = System.Runtime.InteropServices.Marshalling.ExceptionAsDefaultMarshaller.ConvertToUnmanaged(__exception); - } - finally - { - // Cleanup - Perform required cleanup. - __retVal_native__marshaller.Free(); + return __retVal_native; } return __retVal_native; diff --git a/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.ComInterfaceGenerator/SharedTypes.ComInterfaces.IStatefulPinnedMarshalling.cs b/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.ComInterfaceGenerator/SharedTypes.ComInterfaces.IStatefulPinnedMarshalling.cs index 7291d53..04ea0db 100644 --- a/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.ComInterfaceGenerator/SharedTypes.ComInterfaces.IStatefulPinnedMarshalling.cs +++ b/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.ComInterfaceGenerator/SharedTypes.ComInterfaces.IStatefulPinnedMarshalling.cs @@ -224,15 +224,13 @@ internal static int ABI_Method(System.Runtime.InteropServices.ComWrappers.ComInt param = __param_native__marshaller.ToManaged(); @this = System.Runtime.InteropServices.ComWrappers.ComInterfaceDispatch.GetInstance(__this_native); @this.Method(param); + // Cleanup - Perform required cleanup. + __param_native__marshaller.Free(); } catch (System.Exception __exception) { __retVal = System.Runtime.InteropServices.Marshalling.ExceptionAsHResultMarshaller.ConvertToUnmanaged(__exception); - } - finally - { - // Cleanup - Perform required cleanup. - __param_native__marshaller.Free(); + return __retVal; } return __retVal; @@ -242,7 +240,7 @@ internal static int ABI_Method(System.Runtime.InteropServices.ComWrappers.ComInt internal static int ABI_MethodIn(System.Runtime.InteropServices.ComWrappers.ComInterfaceDispatch* __this_native, nint* __param_native__param) { global::SharedTypes.ComInterfaces.IStatefulPinnedMarshalling @this = default; - ref nint __param_native = ref *__param_native__param; + nint __param_native = *__param_native__param; global::SharedTypes.ComInterfaces.StatefulPinnedType param = default; int __retVal = default; // Setup - Perform required setup. @@ -256,15 +254,13 @@ internal static int ABI_MethodIn(System.Runtime.InteropServices.ComWrappers.ComI param = __param_native__marshaller.ToManaged(); @this = System.Runtime.InteropServices.ComWrappers.ComInterfaceDispatch.GetInstance(__this_native); @this.MethodIn(in param); + // Cleanup - Perform required cleanup. + __param_native__marshaller.Free(); } catch (System.Exception __exception) { __retVal = System.Runtime.InteropServices.Marshalling.ExceptionAsHResultMarshaller.ConvertToUnmanaged(__exception); - } - finally - { - // Cleanup - Perform required cleanup. - __param_native__marshaller.Free(); + return __retVal; } return __retVal; @@ -274,7 +270,7 @@ internal static int ABI_MethodIn(System.Runtime.InteropServices.ComWrappers.ComI internal static int ABI_MethodOut(System.Runtime.InteropServices.ComWrappers.ComInterfaceDispatch* __this_native, nint* __param_native__param) { global::SharedTypes.ComInterfaces.IStatefulPinnedMarshalling @this = default; - ref nint __param_native = ref *__param_native__param; + nint __param_native__out = default; global::SharedTypes.ComInterfaces.StatefulPinnedType param = default; int __retVal = default; // Setup - Perform required setup. @@ -288,16 +284,16 @@ internal static int ABI_MethodOut(System.Runtime.InteropServices.ComWrappers.Com // NotifyForSuccessfulInvoke - Keep alive any managed objects that need to stay alive across the call. __param_native__marshaller.OnInvoked(); // PinnedMarshal - Convert managed data to native data that requires the managed data to be pinned. - __param_native = __param_native__marshaller.ToUnmanaged(); + __param_native__out = __param_native__marshaller.ToUnmanaged(); + // AssignOut - Assign to parameters + *__param_native__param = __param_native__out; } catch (System.Exception __exception) { __retVal = System.Runtime.InteropServices.Marshalling.ExceptionAsHResultMarshaller.ConvertToUnmanaged(__exception); - } - finally - { - // Cleanup - Perform required cleanup. + // CleanupFailure - Perform required cleanup. __param_native__marshaller.Free(); + return __retVal; } return __retVal; @@ -307,7 +303,8 @@ internal static int ABI_MethodOut(System.Runtime.InteropServices.ComWrappers.Com internal static int ABI_MethodRef(System.Runtime.InteropServices.ComWrappers.ComInterfaceDispatch* __this_native, nint* __param_native__param) { global::SharedTypes.ComInterfaces.IStatefulPinnedMarshalling @this = default; - ref nint __param_native = ref *__param_native__param; + nint __param_native__out = default; + nint __param_native = *__param_native__param; global::SharedTypes.ComInterfaces.StatefulPinnedType param = default; int __retVal = default; // Setup - Perform required setup. @@ -324,16 +321,18 @@ internal static int ABI_MethodRef(System.Runtime.InteropServices.ComWrappers.Com // NotifyForSuccessfulInvoke - Keep alive any managed objects that need to stay alive across the call. __param_native__marshaller.OnInvoked(); // PinnedMarshal - Convert managed data to native data that requires the managed data to be pinned. - __param_native = __param_native__marshaller.ToUnmanaged(); + __param_native__out = __param_native__marshaller.ToUnmanaged(); + // AssignOut - Assign to parameters + *__param_native__param = __param_native__out; + // Cleanup - Perform required cleanup. + __param_native__marshaller.Free(); } catch (System.Exception __exception) { __retVal = System.Runtime.InteropServices.Marshalling.ExceptionAsHResultMarshaller.ConvertToUnmanaged(__exception); - } - finally - { - // Cleanup - Perform required cleanup. + // CleanupFailure - Perform required cleanup. __param_native__marshaller.Free(); + return __retVal; } return __retVal; @@ -343,7 +342,7 @@ internal static int ABI_MethodRef(System.Runtime.InteropServices.ComWrappers.Com internal static int ABI_Return(System.Runtime.InteropServices.ComWrappers.ComInterfaceDispatch* __this_native, nint* __invokeRetValUnmanaged__param) { global::SharedTypes.ComInterfaces.IStatefulPinnedMarshalling @this = default; - ref nint __invokeRetValUnmanaged = ref *__invokeRetValUnmanaged__param; + nint __invokeRetValUnmanaged__out = default; global::SharedTypes.ComInterfaces.StatefulPinnedType __invokeRetVal = default; int __retVal = default; // Setup - Perform required setup. @@ -357,16 +356,16 @@ internal static int ABI_Return(System.Runtime.InteropServices.ComWrappers.ComInt // NotifyForSuccessfulInvoke - Keep alive any managed objects that need to stay alive across the call. __invokeRetValUnmanaged__marshaller.OnInvoked(); // PinnedMarshal - Convert managed data to native data that requires the managed data to be pinned. - __invokeRetValUnmanaged = __invokeRetValUnmanaged__marshaller.ToUnmanaged(); + __invokeRetValUnmanaged__out = __invokeRetValUnmanaged__marshaller.ToUnmanaged(); + // AssignOut - Assign to parameters + *__invokeRetValUnmanaged__param = __invokeRetValUnmanaged__out; } catch (System.Exception __exception) { __retVal = System.Runtime.InteropServices.Marshalling.ExceptionAsHResultMarshaller.ConvertToUnmanaged(__exception); - } - finally - { - // Cleanup - Perform required cleanup. + // CleanupFailure - Perform required cleanup. __invokeRetValUnmanaged__marshaller.Free(); + return __retVal; } return __retVal; @@ -389,15 +388,13 @@ internal static nint ABI_ReturnPreserveSig(System.Runtime.InteropServices.ComWra __retVal_native__marshaller.OnInvoked(); // PinnedMarshal - Convert managed data to native data that requires the managed data to be pinned. __retVal_native = __retVal_native__marshaller.ToUnmanaged(); + // Cleanup - Perform required cleanup. + __retVal_native__marshaller.Free(); } catch (System.Exception __exception) { __retVal_native = System.Runtime.InteropServices.Marshalling.ExceptionAsDefaultMarshaller.ConvertToUnmanaged(__exception); - } - finally - { - // Cleanup - Perform required cleanup. - __retVal_native__marshaller.Free(); + return __retVal_native; } return __retVal_native; diff --git a/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.ComInterfaceGenerator/SharedTypes.ComInterfaces.IStatelessCallerAllocateBufferMarshalling.cs b/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.ComInterfaceGenerator/SharedTypes.ComInterfaces.IStatelessCallerAllocateBufferMarshalling.cs index 88caed4..cfa2922 100644 --- a/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.ComInterfaceGenerator/SharedTypes.ComInterfaces.IStatelessCallerAllocateBufferMarshalling.cs +++ b/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.ComInterfaceGenerator/SharedTypes.ComInterfaces.IStatelessCallerAllocateBufferMarshalling.cs @@ -192,6 +192,7 @@ internal static int ABI_Method(System.Runtime.InteropServices.ComWrappers.ComInt catch (System.Exception __exception) { __retVal = System.Runtime.InteropServices.Marshalling.ExceptionAsHResultMarshaller.ConvertToUnmanaged(__exception); + return __retVal; } return __retVal; @@ -201,7 +202,7 @@ internal static int ABI_Method(System.Runtime.InteropServices.ComWrappers.ComInt internal static int ABI_MethodIn(System.Runtime.InteropServices.ComWrappers.ComInterfaceDispatch* __this_native, nint* __param_native__param, int size) { global::SharedTypes.ComInterfaces.IStatelessCallerAllocateBufferMarshalling @this = default; - ref nint __param_native = ref *__param_native__param; + nint __param_native = *__param_native__param; global::SharedTypes.ComInterfaces.StatelessCallerAllocatedBufferType param = default; int __retVal = default; try @@ -215,6 +216,7 @@ internal static int ABI_MethodIn(System.Runtime.InteropServices.ComWrappers.ComI catch (System.Exception __exception) { __retVal = System.Runtime.InteropServices.Marshalling.ExceptionAsHResultMarshaller.ConvertToUnmanaged(__exception); + return __retVal; } return __retVal; @@ -224,7 +226,7 @@ internal static int ABI_MethodIn(System.Runtime.InteropServices.ComWrappers.ComI internal static int ABI_MethodOut(System.Runtime.InteropServices.ComWrappers.ComInterfaceDispatch* __this_native, nint* __param_native__param, int size) { global::SharedTypes.ComInterfaces.IStatelessCallerAllocateBufferMarshalling @this = default; - ref nint __param_native = ref *__param_native__param; + nint __param_native__out = default; global::SharedTypes.ComInterfaces.StatelessCallerAllocatedBufferType param = default; int __retVal = default; try @@ -234,11 +236,16 @@ internal static int ABI_MethodOut(System.Runtime.InteropServices.ComWrappers.Com @this = System.Runtime.InteropServices.ComWrappers.ComInterfaceDispatch.GetInstance(__this_native); @this.MethodOut(out param, size); // Marshal - Convert managed data to native data. - __param_native = global::SharedTypes.ComInterfaces.StatelessCallerAllocatedBufferTypeMarshaller.ConvertToUnmanaged(param); + __param_native__out = global::SharedTypes.ComInterfaces.StatelessCallerAllocatedBufferTypeMarshaller.ConvertToUnmanaged(param); + // AssignOut - Assign to parameters + *__param_native__param = __param_native__out; } catch (System.Exception __exception) { __retVal = System.Runtime.InteropServices.Marshalling.ExceptionAsHResultMarshaller.ConvertToUnmanaged(__exception); + // CleanupFailure - Perform required cleanup. + global::SharedTypes.ComInterfaces.StatelessCallerAllocatedBufferTypeMarshaller.Free(__param_native__out); + return __retVal; } return __retVal; @@ -248,12 +255,10 @@ internal static int ABI_MethodOut(System.Runtime.InteropServices.ComWrappers.Com internal static int ABI_MethodRef(System.Runtime.InteropServices.ComWrappers.ComInterfaceDispatch* __this_native, nint* __param_native__param, int size) { global::SharedTypes.ComInterfaces.IStatelessCallerAllocateBufferMarshalling @this = default; - ref nint __param_native = ref *__param_native__param; + nint __param_native__out = default; + nint __param_native = *__param_native__param; global::SharedTypes.ComInterfaces.StatelessCallerAllocatedBufferType param = default; int __retVal = default; - // Setup - Perform required setup. - bool __param_native__ownOriginal = false; - nint __param_native__original = __param_native; try { // Unmarshal - Convert native data to managed data. @@ -262,20 +267,18 @@ internal static int ABI_MethodRef(System.Runtime.InteropServices.ComWrappers.Com @this = System.Runtime.InteropServices.ComWrappers.ComInterfaceDispatch.GetInstance(__this_native); @this.MethodRef(ref param, size); // Marshal - Convert managed data to native data. - __param_native = global::SharedTypes.ComInterfaces.StatelessCallerAllocatedBufferTypeMarshaller.ConvertToUnmanaged(param); - __param_native__ownOriginal = true; + __param_native__out = global::SharedTypes.ComInterfaces.StatelessCallerAllocatedBufferTypeMarshaller.ConvertToUnmanaged(param); + // AssignOut - Assign to parameters + *__param_native__param = __param_native__out; + // Cleanup - Perform required cleanup. + global::SharedTypes.ComInterfaces.StatelessCallerAllocatedBufferTypeMarshaller.Free(__param_native); } catch (System.Exception __exception) { __retVal = System.Runtime.InteropServices.Marshalling.ExceptionAsHResultMarshaller.ConvertToUnmanaged(__exception); - } - finally - { - // Cleanup - Perform required cleanup. - if (__param_native__ownOriginal) - { - global::SharedTypes.ComInterfaces.StatelessCallerAllocatedBufferTypeMarshaller.Free(__param_native__original); - } + // CleanupFailure - Perform required cleanup. + global::SharedTypes.ComInterfaces.StatelessCallerAllocatedBufferTypeMarshaller.Free(__param_native__out); + return __retVal; } return __retVal; @@ -285,7 +288,7 @@ internal static int ABI_MethodRef(System.Runtime.InteropServices.ComWrappers.Com internal static int ABI_Return(System.Runtime.InteropServices.ComWrappers.ComInterfaceDispatch* __this_native, nint* __invokeRetValUnmanaged__param) { global::SharedTypes.ComInterfaces.IStatelessCallerAllocateBufferMarshalling @this = default; - ref nint __invokeRetValUnmanaged = ref *__invokeRetValUnmanaged__param; + nint __invokeRetValUnmanaged__out = default; global::SharedTypes.ComInterfaces.StatelessCallerAllocatedBufferType __invokeRetVal = default; int __retVal = default; try @@ -295,11 +298,16 @@ internal static int ABI_Return(System.Runtime.InteropServices.ComWrappers.ComInt @this = System.Runtime.InteropServices.ComWrappers.ComInterfaceDispatch.GetInstance(__this_native); __invokeRetVal = @this.Return(); // Marshal - Convert managed data to native data. - __invokeRetValUnmanaged = global::SharedTypes.ComInterfaces.StatelessCallerAllocatedBufferTypeMarshaller.ConvertToUnmanaged(__invokeRetVal); + __invokeRetValUnmanaged__out = global::SharedTypes.ComInterfaces.StatelessCallerAllocatedBufferTypeMarshaller.ConvertToUnmanaged(__invokeRetVal); + // AssignOut - Assign to parameters + *__invokeRetValUnmanaged__param = __invokeRetValUnmanaged__out; } catch (System.Exception __exception) { __retVal = System.Runtime.InteropServices.Marshalling.ExceptionAsHResultMarshaller.ConvertToUnmanaged(__exception); + // CleanupFailure - Perform required cleanup. + global::SharedTypes.ComInterfaces.StatelessCallerAllocatedBufferTypeMarshaller.Free(__invokeRetValUnmanaged__out); + return __retVal; } return __retVal; @@ -322,6 +330,7 @@ internal static nint ABI_ReturnPreserveSig(System.Runtime.InteropServices.ComWra catch (System.Exception __exception) { __retVal_native = System.Runtime.InteropServices.Marshalling.ExceptionAsDefaultMarshaller.ConvertToUnmanaged(__exception); + return __retVal_native; } return __retVal_native; diff --git a/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.ComInterfaceGenerator/SharedTypes.ComInterfaces.IStatelessCollectionBlittableElement.cs b/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.ComInterfaceGenerator/SharedTypes.ComInterfaces.IStatelessCollectionBlittableElement.cs index 0ad6700..0a4a445 100644 --- a/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.ComInterfaceGenerator/SharedTypes.ComInterfaces.IStatelessCollectionBlittableElement.cs +++ b/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.ComInterfaceGenerator/SharedTypes.ComInterfaces.IStatelessCollectionBlittableElement.cs @@ -37,7 +37,6 @@ finally { // Cleanup - Perform required cleanup. - ; global::SharedTypes.ComInterfaces.StatelessCollectionMarshaller.Default.Free(__p_native); } } @@ -70,7 +69,6 @@ finally { // Cleanup - Perform required cleanup. - ; global::SharedTypes.ComInterfaces.StatelessCollectionMarshaller.Default.Free(__pIn_native); } } @@ -104,7 +102,6 @@ finally { // Cleanup - Perform required cleanup. - __pRef_native__numElements = size; global::SharedTypes.ComInterfaces.StatelessCollectionMarshaller.Default.Free(__pRef_native); } } @@ -139,7 +136,6 @@ finally { // Cleanup - Perform required cleanup. - __pOut_native__numElements = size; global::SharedTypes.ComInterfaces.StatelessCollectionMarshaller.Default.Free(__pOut_native); } } @@ -171,7 +167,6 @@ finally { // Cleanup - Perform required cleanup. - __retVal_native__numElements = size; global::SharedTypes.ComInterfaces.StatelessCollectionMarshaller.Default.Free(__retVal_native); } @@ -203,7 +198,6 @@ finally { // Cleanup - Perform required cleanup. - __retVal_native__numElements = size; global::SharedTypes.ComInterfaces.StatelessCollectionMarshaller.Default.Free(__retVal_native); } @@ -235,6 +229,7 @@ internal static int ABI_Method(System.Runtime.InteropServices.ComWrappers.ComInt catch (System.Exception __exception) { __retVal = System.Runtime.InteropServices.Marshalling.ExceptionAsHResultMarshaller.ConvertToUnmanaged(__exception); + return __retVal; } return __retVal; @@ -244,9 +239,9 @@ internal static int ABI_Method(System.Runtime.InteropServices.ComWrappers.ComInt internal static int ABI_MethodIn(System.Runtime.InteropServices.ComWrappers.ComInterfaceDispatch* __this_native, nint* __pIn_native__param, int* __size_native__param) { global::SharedTypes.ComInterfaces.IStatelessCollectionBlittableElement @this = default; - ref nint __pIn_native = ref *__pIn_native__param; + nint __pIn_native = *__pIn_native__param; global::SharedTypes.ComInterfaces.StatelessCollection pIn = default; - ref int __size_native = ref *__size_native__param; + int __size_native = *__size_native__param; int size = default; int __retVal = default; // Setup - Perform required setup. @@ -266,6 +261,7 @@ internal static int ABI_MethodIn(System.Runtime.InteropServices.ComWrappers.ComI catch (System.Exception __exception) { __retVal = System.Runtime.InteropServices.Marshalling.ExceptionAsHResultMarshaller.ConvertToUnmanaged(__exception); + return __retVal; } return __retVal; @@ -275,14 +271,13 @@ internal static int ABI_MethodIn(System.Runtime.InteropServices.ComWrappers.ComI internal static int ABI_MethodRef(System.Runtime.InteropServices.ComWrappers.ComInterfaceDispatch* __this_native, nint* __pRef_native__param, int size) { global::SharedTypes.ComInterfaces.IStatelessCollectionBlittableElement @this = default; - ref nint __pRef_native = ref *__pRef_native__param; + nint __pRef_native__out = default; + nint __pRef_native = *__pRef_native__param; global::SharedTypes.ComInterfaces.StatelessCollection pRef = default; int __retVal = default; // Setup - Perform required setup. int __pRef_native__numElements; System.Runtime.CompilerServices.Unsafe.SkipInit(out __pRef_native__numElements); - bool __pRef_native__ownOriginal = false; - nint __pRef_native__original = __pRef_native; try { // Unmarshal - Convert native data to managed data. @@ -293,22 +288,19 @@ internal static int ABI_MethodRef(System.Runtime.InteropServices.ComWrappers.Com @this = System.Runtime.InteropServices.ComWrappers.ComInterfaceDispatch.GetInstance(__this_native); @this.MethodRef(ref pRef, size); // Marshal - Convert managed data to native data. - __pRef_native = global::SharedTypes.ComInterfaces.StatelessCollectionMarshaller.Default.AllocateContainerForUnmanagedElements(pRef, out __pRef_native__numElements); - __pRef_native__ownOriginal = true; - global::SharedTypes.ComInterfaces.StatelessCollectionMarshaller.Default.GetManagedValuesSource(pRef).CopyTo(global::SharedTypes.ComInterfaces.StatelessCollectionMarshaller.Default.GetUnmanagedValuesDestination(__pRef_native, __pRef_native__numElements)); + __pRef_native__out = global::SharedTypes.ComInterfaces.StatelessCollectionMarshaller.Default.AllocateContainerForUnmanagedElements(pRef, out __pRef_native__numElements); + global::SharedTypes.ComInterfaces.StatelessCollectionMarshaller.Default.GetManagedValuesSource(pRef).CopyTo(global::SharedTypes.ComInterfaces.StatelessCollectionMarshaller.Default.GetUnmanagedValuesDestination(__pRef_native__out, __pRef_native__numElements)); + // AssignOut - Assign to parameters + *__pRef_native__param = __pRef_native__out; + // Cleanup - Perform required cleanup. + global::SharedTypes.ComInterfaces.StatelessCollectionMarshaller.Default.Free(__pRef_native); } catch (System.Exception __exception) { __retVal = System.Runtime.InteropServices.Marshalling.ExceptionAsHResultMarshaller.ConvertToUnmanaged(__exception); - } - finally - { - // Cleanup - Perform required cleanup. - if (__pRef_native__ownOriginal) - { - __pRef_native__numElements = size; - global::SharedTypes.ComInterfaces.StatelessCollectionMarshaller.Default.Free(__pRef_native__original); - } + // CleanupFailure - Perform required cleanup. + global::SharedTypes.ComInterfaces.StatelessCollectionMarshaller.Default.Free(__pRef_native__out); + return __retVal; } return __retVal; @@ -318,9 +310,9 @@ internal static int ABI_MethodRef(System.Runtime.InteropServices.ComWrappers.Com internal static int ABI_MethodOut(System.Runtime.InteropServices.ComWrappers.ComInterfaceDispatch* __this_native, nint* __pOut_native__param, int* __size_native__param) { global::SharedTypes.ComInterfaces.IStatelessCollectionBlittableElement @this = default; - ref nint __pOut_native = ref *__pOut_native__param; + nint __pOut_native__out = default; global::SharedTypes.ComInterfaces.StatelessCollection pOut = default; - ref int __size_native = ref *__size_native__param; + int __size_native__out = default; int size = default; int __retVal = default; // Setup - Perform required setup. @@ -333,13 +325,19 @@ internal static int ABI_MethodOut(System.Runtime.InteropServices.ComWrappers.Com @this = System.Runtime.InteropServices.ComWrappers.ComInterfaceDispatch.GetInstance(__this_native); @this.MethodOut(out pOut, out size); // Marshal - Convert managed data to native data. - __size_native = size; - __pOut_native = global::SharedTypes.ComInterfaces.StatelessCollectionMarshaller.Default.AllocateContainerForUnmanagedElements(pOut, out __pOut_native__numElements); - global::SharedTypes.ComInterfaces.StatelessCollectionMarshaller.Default.GetManagedValuesSource(pOut).CopyTo(global::SharedTypes.ComInterfaces.StatelessCollectionMarshaller.Default.GetUnmanagedValuesDestination(__pOut_native, __pOut_native__numElements)); + __size_native__out = size; + __pOut_native__out = global::SharedTypes.ComInterfaces.StatelessCollectionMarshaller.Default.AllocateContainerForUnmanagedElements(pOut, out __pOut_native__numElements); + global::SharedTypes.ComInterfaces.StatelessCollectionMarshaller.Default.GetManagedValuesSource(pOut).CopyTo(global::SharedTypes.ComInterfaces.StatelessCollectionMarshaller.Default.GetUnmanagedValuesDestination(__pOut_native__out, __pOut_native__numElements)); + // AssignOut - Assign to parameters + *__size_native__param = __size_native__out; + *__pOut_native__param = __pOut_native__out; } catch (System.Exception __exception) { __retVal = System.Runtime.InteropServices.Marshalling.ExceptionAsHResultMarshaller.ConvertToUnmanaged(__exception); + // CleanupFailure - Perform required cleanup. + global::SharedTypes.ComInterfaces.StatelessCollectionMarshaller.Default.Free(__pOut_native__out); + return __retVal; } return __retVal; @@ -349,7 +347,7 @@ internal static int ABI_MethodOut(System.Runtime.InteropServices.ComWrappers.Com internal static int ABI_Return(System.Runtime.InteropServices.ComWrappers.ComInterfaceDispatch* __this_native, int size, nint* __invokeRetValUnmanaged__param) { global::SharedTypes.ComInterfaces.IStatelessCollectionBlittableElement @this = default; - ref nint __invokeRetValUnmanaged = ref *__invokeRetValUnmanaged__param; + nint __invokeRetValUnmanaged__out = default; global::SharedTypes.ComInterfaces.StatelessCollection __invokeRetVal = default; int __retVal = default; // Setup - Perform required setup. @@ -362,12 +360,17 @@ internal static int ABI_Return(System.Runtime.InteropServices.ComWrappers.ComInt @this = System.Runtime.InteropServices.ComWrappers.ComInterfaceDispatch.GetInstance(__this_native); __invokeRetVal = @this.Return(size); // Marshal - Convert managed data to native data. - __invokeRetValUnmanaged = global::SharedTypes.ComInterfaces.StatelessCollectionMarshaller.Default.AllocateContainerForUnmanagedElements(__invokeRetVal, out __invokeRetValUnmanaged__numElements); - global::SharedTypes.ComInterfaces.StatelessCollectionMarshaller.Default.GetManagedValuesSource(__invokeRetVal).CopyTo(global::SharedTypes.ComInterfaces.StatelessCollectionMarshaller.Default.GetUnmanagedValuesDestination(__invokeRetValUnmanaged, __invokeRetValUnmanaged__numElements)); + __invokeRetValUnmanaged__out = global::SharedTypes.ComInterfaces.StatelessCollectionMarshaller.Default.AllocateContainerForUnmanagedElements(__invokeRetVal, out __invokeRetValUnmanaged__numElements); + global::SharedTypes.ComInterfaces.StatelessCollectionMarshaller.Default.GetManagedValuesSource(__invokeRetVal).CopyTo(global::SharedTypes.ComInterfaces.StatelessCollectionMarshaller.Default.GetUnmanagedValuesDestination(__invokeRetValUnmanaged__out, __invokeRetValUnmanaged__numElements)); + // AssignOut - Assign to parameters + *__invokeRetValUnmanaged__param = __invokeRetValUnmanaged__out; } catch (System.Exception __exception) { __retVal = System.Runtime.InteropServices.Marshalling.ExceptionAsHResultMarshaller.ConvertToUnmanaged(__exception); + // CleanupFailure - Perform required cleanup. + global::SharedTypes.ComInterfaces.StatelessCollectionMarshaller.Default.Free(__invokeRetValUnmanaged__out); + return __retVal; } return __retVal; @@ -394,6 +397,7 @@ internal static nint ABI_ReturnPreserveSig(System.Runtime.InteropServices.ComWra catch (System.Exception __exception) { __retVal_native = System.Runtime.InteropServices.Marshalling.ExceptionAsDefaultMarshaller.ConvertToUnmanaged(__exception); + return __retVal_native; } return __retVal_native; diff --git a/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.ComInterfaceGenerator/SharedTypes.ComInterfaces.IStatelessCollectionStatelessElement.cs b/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.ComInterfaceGenerator/SharedTypes.ComInterfaces.IStatelessCollectionStatelessElement.cs index 907d0da..a61eae7 100644 --- a/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.ComInterfaceGenerator/SharedTypes.ComInterfaces.IStatelessCollectionStatelessElement.cs +++ b/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.ComInterfaceGenerator/SharedTypes.ComInterfaces.IStatelessCollectionStatelessElement.cs @@ -52,7 +52,8 @@ { global::SharedTypes.ComInterfaces.StatelessTypeMarshaller.Free(__p_native__nativeSpan[__i0]); } - }; + } + global::SharedTypes.ComInterfaces.StatelessCollectionMarshaller.Default.Free(__p_native); } } @@ -100,7 +101,8 @@ { global::SharedTypes.ComInterfaces.StatelessTypeMarshaller.Free(__pIn_native__nativeSpan[__i0]); } - }; + } + global::SharedTypes.ComInterfaces.StatelessCollectionMarshaller.Default.Free(__pIn_native); } } @@ -158,7 +160,6 @@ } } - __pRef_native__numElements = size; global::SharedTypes.ComInterfaces.StatelessCollectionMarshaller.Default.Free(__pRef_native); } } @@ -208,7 +209,6 @@ } } - __pOut_native__numElements = size; global::SharedTypes.ComInterfaces.StatelessCollectionMarshaller.Default.Free(__pOut_native); } } @@ -255,7 +255,6 @@ } } - __retVal_native__numElements = size; global::SharedTypes.ComInterfaces.StatelessCollectionMarshaller.Default.Free(__retVal_native); } @@ -302,7 +301,6 @@ } } - __retVal_native__numElements = size; global::SharedTypes.ComInterfaces.StatelessCollectionMarshaller.Default.Free(__retVal_native); } @@ -342,6 +340,7 @@ internal static int ABI_Method(System.Runtime.InteropServices.ComWrappers.ComInt catch (System.Exception __exception) { __retVal = System.Runtime.InteropServices.Marshalling.ExceptionAsHResultMarshaller.ConvertToUnmanaged(__exception); + return __retVal; } return __retVal; @@ -351,9 +350,9 @@ internal static int ABI_Method(System.Runtime.InteropServices.ComWrappers.ComInt internal static int ABI_MethodIn(System.Runtime.InteropServices.ComWrappers.ComInterfaceDispatch* __this_native, nint* __pIn_native__param, int* __size_native__param) { global::SharedTypes.ComInterfaces.IStatelessCollectionStatelessElement @this = default; - ref nint __pIn_native = ref *__pIn_native__param; + nint __pIn_native = *__pIn_native__param; global::SharedTypes.ComInterfaces.StatelessCollection pIn = default; - ref int __size_native = ref *__size_native__param; + int __size_native = *__size_native__param; int size = default; int __retVal = default; // Setup - Perform required setup. @@ -381,6 +380,7 @@ internal static int ABI_MethodIn(System.Runtime.InteropServices.ComWrappers.ComI catch (System.Exception __exception) { __retVal = System.Runtime.InteropServices.Marshalling.ExceptionAsHResultMarshaller.ConvertToUnmanaged(__exception); + return __retVal; } return __retVal; @@ -390,14 +390,13 @@ internal static int ABI_MethodIn(System.Runtime.InteropServices.ComWrappers.ComI internal static int ABI_MethodRef(System.Runtime.InteropServices.ComWrappers.ComInterfaceDispatch* __this_native, nint* __pRef_native__param, int size) { global::SharedTypes.ComInterfaces.IStatelessCollectionStatelessElement @this = default; - ref nint __pRef_native = ref *__pRef_native__param; + nint __pRef_native__out = default; + nint __pRef_native = *__pRef_native__param; global::SharedTypes.ComInterfaces.StatelessCollection pRef = default; int __retVal = default; // Setup - Perform required setup. int __pRef_native__numElements; System.Runtime.CompilerServices.Unsafe.SkipInit(out __pRef_native__numElements); - bool __pRef_native__ownOriginal = false; - nint __pRef_native__original = __pRef_native; int __pRef_native__lastIndexMarshalled = 0; try { @@ -417,37 +416,43 @@ internal static int ABI_MethodRef(System.Runtime.InteropServices.ComWrappers.Com @this = System.Runtime.InteropServices.ComWrappers.ComInterfaceDispatch.GetInstance(__this_native); @this.MethodRef(ref pRef, size); // Marshal - Convert managed data to native data. - __pRef_native = global::SharedTypes.ComInterfaces.StatelessCollectionMarshaller.Default.AllocateContainerForUnmanagedElements(pRef, out __pRef_native__numElements); - __pRef_native__ownOriginal = true; + __pRef_native__out = global::SharedTypes.ComInterfaces.StatelessCollectionMarshaller.Default.AllocateContainerForUnmanagedElements(pRef, out __pRef_native__numElements); { System.ReadOnlySpan __pRef_native__managedSpan = global::SharedTypes.ComInterfaces.StatelessCollectionMarshaller.Default.GetManagedValuesSource(pRef); - System.Span __pRef_native__nativeSpan = global::SharedTypes.ComInterfaces.StatelessCollectionMarshaller.Default.GetUnmanagedValuesDestination(__pRef_native, __pRef_native__numElements); + System.Span __pRef_native__nativeSpan = global::SharedTypes.ComInterfaces.StatelessCollectionMarshaller.Default.GetUnmanagedValuesDestination(__pRef_native__out, __pRef_native__numElements); for (int __i0 = 0; __i0 < __pRef_native__managedSpan.Length; ++__i0, ++__pRef_native__lastIndexMarshalled) { __pRef_native__nativeSpan[__i0] = global::SharedTypes.ComInterfaces.StatelessTypeMarshaller.ConvertToUnmanaged(__pRef_native__managedSpan[__i0]); } } + + // AssignOut - Assign to parameters + *__pRef_native__param = __pRef_native__out; + // Cleanup - Perform required cleanup. + { + System.ReadOnlySpan __pRef_native__nativeSpan = global::SharedTypes.ComInterfaces.StatelessCollectionMarshaller.Default.GetUnmanagedValuesSource(__pRef_native, __pRef_native__numElements); + for (int __i0 = 0; __i0 < __pRef_native__lastIndexMarshalled; ++__i0) + { + global::SharedTypes.ComInterfaces.StatelessTypeMarshaller.Free(__pRef_native__nativeSpan[__i0]); + } + } + + global::SharedTypes.ComInterfaces.StatelessCollectionMarshaller.Default.Free(__pRef_native); } catch (System.Exception __exception) { __retVal = System.Runtime.InteropServices.Marshalling.ExceptionAsHResultMarshaller.ConvertToUnmanaged(__exception); - } - finally - { - // Cleanup - Perform required cleanup. - if (__pRef_native__ownOriginal) + // CleanupFailure - Perform required cleanup. { + System.ReadOnlySpan __pRef_native__nativeSpan = global::SharedTypes.ComInterfaces.StatelessCollectionMarshaller.Default.GetUnmanagedValuesSource(__pRef_native__out, __pRef_native__numElements); + for (int __i0 = 0; __i0 < __pRef_native__lastIndexMarshalled; ++__i0) { - System.ReadOnlySpan __pRef_native__nativeSpan = global::SharedTypes.ComInterfaces.StatelessCollectionMarshaller.Default.GetUnmanagedValuesSource(__pRef_native__original, __pRef_native__numElements); - for (int __i0 = 0; __i0 < __pRef_native__lastIndexMarshalled; ++__i0) - { - global::SharedTypes.ComInterfaces.StatelessTypeMarshaller.Free(__pRef_native__nativeSpan[__i0]); - } + global::SharedTypes.ComInterfaces.StatelessTypeMarshaller.Free(__pRef_native__nativeSpan[__i0]); } - - __pRef_native__numElements = size; - global::SharedTypes.ComInterfaces.StatelessCollectionMarshaller.Default.Free(__pRef_native__original); } + + global::SharedTypes.ComInterfaces.StatelessCollectionMarshaller.Default.Free(__pRef_native__out); + return __retVal; } return __retVal; @@ -457,9 +462,9 @@ internal static int ABI_MethodRef(System.Runtime.InteropServices.ComWrappers.Com internal static int ABI_MethodOut(System.Runtime.InteropServices.ComWrappers.ComInterfaceDispatch* __this_native, nint* __pOut_native__param, int* __size_native__param) { global::SharedTypes.ComInterfaces.IStatelessCollectionStatelessElement @this = default; - ref nint __pOut_native = ref *__pOut_native__param; + nint __pOut_native__out = default; global::SharedTypes.ComInterfaces.StatelessCollection pOut = default; - ref int __size_native = ref *__size_native__param; + int __size_native__out = default; int size = default; int __retVal = default; // Setup - Perform required setup. @@ -472,21 +477,36 @@ internal static int ABI_MethodOut(System.Runtime.InteropServices.ComWrappers.Com @this = System.Runtime.InteropServices.ComWrappers.ComInterfaceDispatch.GetInstance(__this_native); @this.MethodOut(out pOut, out size); // Marshal - Convert managed data to native data. - __size_native = size; - __pOut_native = global::SharedTypes.ComInterfaces.StatelessCollectionMarshaller.Default.AllocateContainerForUnmanagedElements(pOut, out __pOut_native__numElements); + __size_native__out = size; + __pOut_native__out = global::SharedTypes.ComInterfaces.StatelessCollectionMarshaller.Default.AllocateContainerForUnmanagedElements(pOut, out __pOut_native__numElements); { System.ReadOnlySpan __pOut_native__managedSpan = global::SharedTypes.ComInterfaces.StatelessCollectionMarshaller.Default.GetManagedValuesSource(pOut); - System.Span __pOut_native__nativeSpan = global::SharedTypes.ComInterfaces.StatelessCollectionMarshaller.Default.GetUnmanagedValuesDestination(__pOut_native, __pOut_native__numElements); + System.Span __pOut_native__nativeSpan = global::SharedTypes.ComInterfaces.StatelessCollectionMarshaller.Default.GetUnmanagedValuesDestination(__pOut_native__out, __pOut_native__numElements); __pOut_native__nativeSpan.Clear(); for (int __i0 = 0; __i0 < __pOut_native__managedSpan.Length; ++__i0) { __pOut_native__nativeSpan[__i0] = global::SharedTypes.ComInterfaces.StatelessTypeMarshaller.ConvertToUnmanaged(__pOut_native__managedSpan[__i0]); } } + + // AssignOut - Assign to parameters + *__size_native__param = __size_native__out; + *__pOut_native__param = __pOut_native__out; } catch (System.Exception __exception) { __retVal = System.Runtime.InteropServices.Marshalling.ExceptionAsHResultMarshaller.ConvertToUnmanaged(__exception); + // CleanupFailure - Perform required cleanup. + { + System.ReadOnlySpan __pOut_native__nativeSpan = global::SharedTypes.ComInterfaces.StatelessCollectionMarshaller.Default.GetUnmanagedValuesSource(__pOut_native__out, __pOut_native__numElements); + for (int __i0 = 0; __i0 < __pOut_native__nativeSpan.Length; ++__i0) + { + global::SharedTypes.ComInterfaces.StatelessTypeMarshaller.Free(__pOut_native__nativeSpan[__i0]); + } + } + + global::SharedTypes.ComInterfaces.StatelessCollectionMarshaller.Default.Free(__pOut_native__out); + return __retVal; } return __retVal; @@ -496,7 +516,7 @@ internal static int ABI_MethodOut(System.Runtime.InteropServices.ComWrappers.Com internal static int ABI_Return(System.Runtime.InteropServices.ComWrappers.ComInterfaceDispatch* __this_native, int size, nint* __invokeRetValUnmanaged__param) { global::SharedTypes.ComInterfaces.IStatelessCollectionStatelessElement @this = default; - ref nint __invokeRetValUnmanaged = ref *__invokeRetValUnmanaged__param; + nint __invokeRetValUnmanaged__out = default; global::SharedTypes.ComInterfaces.StatelessCollection __invokeRetVal = default; int __retVal = default; // Setup - Perform required setup. @@ -509,20 +529,34 @@ internal static int ABI_Return(System.Runtime.InteropServices.ComWrappers.ComInt @this = System.Runtime.InteropServices.ComWrappers.ComInterfaceDispatch.GetInstance(__this_native); __invokeRetVal = @this.Return(size); // Marshal - Convert managed data to native data. - __invokeRetValUnmanaged = global::SharedTypes.ComInterfaces.StatelessCollectionMarshaller.Default.AllocateContainerForUnmanagedElements(__invokeRetVal, out __invokeRetValUnmanaged__numElements); + __invokeRetValUnmanaged__out = global::SharedTypes.ComInterfaces.StatelessCollectionMarshaller.Default.AllocateContainerForUnmanagedElements(__invokeRetVal, out __invokeRetValUnmanaged__numElements); { System.ReadOnlySpan __invokeRetValUnmanaged__managedSpan = global::SharedTypes.ComInterfaces.StatelessCollectionMarshaller.Default.GetManagedValuesSource(__invokeRetVal); - System.Span __invokeRetValUnmanaged__nativeSpan = global::SharedTypes.ComInterfaces.StatelessCollectionMarshaller.Default.GetUnmanagedValuesDestination(__invokeRetValUnmanaged, __invokeRetValUnmanaged__numElements); + System.Span __invokeRetValUnmanaged__nativeSpan = global::SharedTypes.ComInterfaces.StatelessCollectionMarshaller.Default.GetUnmanagedValuesDestination(__invokeRetValUnmanaged__out, __invokeRetValUnmanaged__numElements); __invokeRetValUnmanaged__nativeSpan.Clear(); for (int __i0 = 0; __i0 < __invokeRetValUnmanaged__managedSpan.Length; ++__i0) { __invokeRetValUnmanaged__nativeSpan[__i0] = global::SharedTypes.ComInterfaces.StatelessTypeMarshaller.ConvertToUnmanaged(__invokeRetValUnmanaged__managedSpan[__i0]); } } + + // AssignOut - Assign to parameters + *__invokeRetValUnmanaged__param = __invokeRetValUnmanaged__out; } catch (System.Exception __exception) { __retVal = System.Runtime.InteropServices.Marshalling.ExceptionAsHResultMarshaller.ConvertToUnmanaged(__exception); + // CleanupFailure - Perform required cleanup. + { + System.ReadOnlySpan __invokeRetValUnmanaged__nativeSpan = global::SharedTypes.ComInterfaces.StatelessCollectionMarshaller.Default.GetUnmanagedValuesSource(__invokeRetValUnmanaged__out, __invokeRetValUnmanaged__numElements); + for (int __i0 = 0; __i0 < __invokeRetValUnmanaged__nativeSpan.Length; ++__i0) + { + global::SharedTypes.ComInterfaces.StatelessTypeMarshaller.Free(__invokeRetValUnmanaged__nativeSpan[__i0]); + } + } + + global::SharedTypes.ComInterfaces.StatelessCollectionMarshaller.Default.Free(__invokeRetValUnmanaged__out); + return __retVal; } return __retVal; @@ -557,6 +591,7 @@ internal static nint ABI_ReturnPreserveSig(System.Runtime.InteropServices.ComWra catch (System.Exception __exception) { __retVal_native = System.Runtime.InteropServices.Marshalling.ExceptionAsDefaultMarshaller.ConvertToUnmanaged(__exception); + return __retVal_native; } return __retVal_native; diff --git a/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.ComInterfaceGenerator/SharedTypes.ComInterfaces.IStatelessFinallyMarshalling.cs b/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.ComInterfaceGenerator/SharedTypes.ComInterfaces.IStatelessFinallyMarshalling.cs index 912a933..124aa6c 100644 --- a/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.ComInterfaceGenerator/SharedTypes.ComInterfaces.IStatelessFinallyMarshalling.cs +++ b/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.ComInterfaceGenerator/SharedTypes.ComInterfaces.IStatelessFinallyMarshalling.cs @@ -203,7 +203,6 @@ file unsafe partial interface InterfaceImplementation [System.Runtime.InteropServices.UnmanagedCallersOnlyAttribute(CallConvs = new[] { typeof(System.Runtime.CompilerServices.CallConvMemberFunction) })] internal static int ABI_Method(System.Runtime.InteropServices.ComWrappers.ComInterfaceDispatch* __this_native, nint __param_native, int size) { - bool __invokeSucceeded = default; global::SharedTypes.ComInterfaces.IStatelessFinallyMarshalling @this = default; global::SharedTypes.ComInterfaces.StatelessFinallyType param = default; int __retVal = default; @@ -212,20 +211,14 @@ internal static int ABI_Method(System.Runtime.InteropServices.ComWrappers.ComInt // Unmarshal - Convert native data to managed data. __retVal = 0; // S_OK @this = System.Runtime.InteropServices.ComWrappers.ComInterfaceDispatch.GetInstance(__this_native); + // GuaranteedUnmarshal - Convert native data to managed data even in the case of an exception during the non-cleanup phases. + param = global::SharedTypes.ComInterfaces.StatelessFinallyTypeMarshaller.ConvertToManagedFinally(__param_native); @this.Method(param, size); - __invokeSucceeded = true; } catch (System.Exception __exception) { __retVal = System.Runtime.InteropServices.Marshalling.ExceptionAsHResultMarshaller.ConvertToUnmanaged(__exception); - } - finally - { - if (__invokeSucceeded) - { - // GuaranteedUnmarshal - Convert native data to managed data even in the case of an exception during the non-cleanup phases. - param = global::SharedTypes.ComInterfaces.StatelessFinallyTypeMarshaller.ConvertToManagedFinally(__param_native); - } + return __retVal; } return __retVal; @@ -234,9 +227,8 @@ internal static int ABI_Method(System.Runtime.InteropServices.ComWrappers.ComInt [System.Runtime.InteropServices.UnmanagedCallersOnlyAttribute(CallConvs = new[] { typeof(System.Runtime.CompilerServices.CallConvMemberFunction) })] internal static int ABI_MethodIn(System.Runtime.InteropServices.ComWrappers.ComInterfaceDispatch* __this_native, nint* __param_native__param, int size) { - bool __invokeSucceeded = default; global::SharedTypes.ComInterfaces.IStatelessFinallyMarshalling @this = default; - ref nint __param_native = ref *__param_native__param; + nint __param_native = *__param_native__param; global::SharedTypes.ComInterfaces.StatelessFinallyType param = default; int __retVal = default; try @@ -244,20 +236,14 @@ internal static int ABI_MethodIn(System.Runtime.InteropServices.ComWrappers.ComI // Unmarshal - Convert native data to managed data. __retVal = 0; // S_OK @this = System.Runtime.InteropServices.ComWrappers.ComInterfaceDispatch.GetInstance(__this_native); + // GuaranteedUnmarshal - Convert native data to managed data even in the case of an exception during the non-cleanup phases. + param = global::SharedTypes.ComInterfaces.StatelessFinallyTypeMarshaller.ConvertToManagedFinally(__param_native); @this.MethodIn(in param, size); - __invokeSucceeded = true; } catch (System.Exception __exception) { __retVal = System.Runtime.InteropServices.Marshalling.ExceptionAsHResultMarshaller.ConvertToUnmanaged(__exception); - } - finally - { - if (__invokeSucceeded) - { - // GuaranteedUnmarshal - Convert native data to managed data even in the case of an exception during the non-cleanup phases. - param = global::SharedTypes.ComInterfaces.StatelessFinallyTypeMarshaller.ConvertToManagedFinally(__param_native); - } + return __retVal; } return __retVal; @@ -267,7 +253,7 @@ internal static int ABI_MethodIn(System.Runtime.InteropServices.ComWrappers.ComI internal static int ABI_MethodOut(System.Runtime.InteropServices.ComWrappers.ComInterfaceDispatch* __this_native, nint* __param_native__param, int size) { global::SharedTypes.ComInterfaces.IStatelessFinallyMarshalling @this = default; - ref nint __param_native = ref *__param_native__param; + nint __param_native__out = default; global::SharedTypes.ComInterfaces.StatelessFinallyType param = default; int __retVal = default; try @@ -277,11 +263,16 @@ internal static int ABI_MethodOut(System.Runtime.InteropServices.ComWrappers.Com @this = System.Runtime.InteropServices.ComWrappers.ComInterfaceDispatch.GetInstance(__this_native); @this.MethodOut(out param, size); // Marshal - Convert managed data to native data. - __param_native = global::SharedTypes.ComInterfaces.StatelessFinallyTypeMarshaller.ConvertToUnmanaged(param); + __param_native__out = global::SharedTypes.ComInterfaces.StatelessFinallyTypeMarshaller.ConvertToUnmanaged(param); + // AssignOut - Assign to parameters + *__param_native__param = __param_native__out; } catch (System.Exception __exception) { __retVal = System.Runtime.InteropServices.Marshalling.ExceptionAsHResultMarshaller.ConvertToUnmanaged(__exception); + // CleanupFailure - Perform required cleanup. + global::SharedTypes.ComInterfaces.StatelessFinallyTypeMarshaller.Free(__param_native__out); + return __retVal; } return __retVal; @@ -290,42 +281,32 @@ internal static int ABI_MethodOut(System.Runtime.InteropServices.ComWrappers.Com [System.Runtime.InteropServices.UnmanagedCallersOnlyAttribute(CallConvs = new[] { typeof(System.Runtime.CompilerServices.CallConvMemberFunction) })] internal static int ABI_MethodRef(System.Runtime.InteropServices.ComWrappers.ComInterfaceDispatch* __this_native, nint* __param_native__param, int size) { - bool __invokeSucceeded = default; global::SharedTypes.ComInterfaces.IStatelessFinallyMarshalling @this = default; - ref nint __param_native = ref *__param_native__param; + nint __param_native__out = default; + nint __param_native = *__param_native__param; global::SharedTypes.ComInterfaces.StatelessFinallyType param = default; int __retVal = default; - // Setup - Perform required setup. - bool __param_native__ownOriginal = false; - nint __param_native__original = __param_native; try { // Unmarshal - Convert native data to managed data. __retVal = 0; // S_OK @this = System.Runtime.InteropServices.ComWrappers.ComInterfaceDispatch.GetInstance(__this_native); + // GuaranteedUnmarshal - Convert native data to managed data even in the case of an exception during the non-cleanup phases. + param = global::SharedTypes.ComInterfaces.StatelessFinallyTypeMarshaller.ConvertToManagedFinally(__param_native); @this.MethodRef(ref param, size); - __invokeSucceeded = true; // Marshal - Convert managed data to native data. - __param_native = global::SharedTypes.ComInterfaces.StatelessFinallyTypeMarshaller.ConvertToUnmanaged(param); - __param_native__ownOriginal = true; + __param_native__out = global::SharedTypes.ComInterfaces.StatelessFinallyTypeMarshaller.ConvertToUnmanaged(param); + // AssignOut - Assign to parameters + *__param_native__param = __param_native__out; + // Cleanup - Perform required cleanup. + global::SharedTypes.ComInterfaces.StatelessFinallyTypeMarshaller.Free(__param_native); } catch (System.Exception __exception) { __retVal = System.Runtime.InteropServices.Marshalling.ExceptionAsHResultMarshaller.ConvertToUnmanaged(__exception); - } - finally - { - if (__invokeSucceeded) - { - // GuaranteedUnmarshal - Convert native data to managed data even in the case of an exception during the non-cleanup phases. - param = global::SharedTypes.ComInterfaces.StatelessFinallyTypeMarshaller.ConvertToManagedFinally(__param_native); - } - - // Cleanup - Perform required cleanup. - if (__param_native__ownOriginal) - { - global::SharedTypes.ComInterfaces.StatelessFinallyTypeMarshaller.Free(__param_native__original); - } + // CleanupFailure - Perform required cleanup. + global::SharedTypes.ComInterfaces.StatelessFinallyTypeMarshaller.Free(__param_native__out); + return __retVal; } return __retVal; @@ -335,7 +316,7 @@ internal static int ABI_MethodRef(System.Runtime.InteropServices.ComWrappers.Com internal static int ABI_Return(System.Runtime.InteropServices.ComWrappers.ComInterfaceDispatch* __this_native, nint* __invokeRetValUnmanaged__param) { global::SharedTypes.ComInterfaces.IStatelessFinallyMarshalling @this = default; - ref nint __invokeRetValUnmanaged = ref *__invokeRetValUnmanaged__param; + nint __invokeRetValUnmanaged__out = default; global::SharedTypes.ComInterfaces.StatelessFinallyType __invokeRetVal = default; int __retVal = default; try @@ -345,11 +326,16 @@ internal static int ABI_Return(System.Runtime.InteropServices.ComWrappers.ComInt @this = System.Runtime.InteropServices.ComWrappers.ComInterfaceDispatch.GetInstance(__this_native); __invokeRetVal = @this.Return(); // Marshal - Convert managed data to native data. - __invokeRetValUnmanaged = global::SharedTypes.ComInterfaces.StatelessFinallyTypeMarshaller.ConvertToUnmanaged(__invokeRetVal); + __invokeRetValUnmanaged__out = global::SharedTypes.ComInterfaces.StatelessFinallyTypeMarshaller.ConvertToUnmanaged(__invokeRetVal); + // AssignOut - Assign to parameters + *__invokeRetValUnmanaged__param = __invokeRetValUnmanaged__out; } catch (System.Exception __exception) { __retVal = System.Runtime.InteropServices.Marshalling.ExceptionAsHResultMarshaller.ConvertToUnmanaged(__exception); + // CleanupFailure - Perform required cleanup. + global::SharedTypes.ComInterfaces.StatelessFinallyTypeMarshaller.Free(__invokeRetValUnmanaged__out); + return __retVal; } return __retVal; @@ -372,6 +358,7 @@ internal static nint ABI_ReturnPreserveSig(System.Runtime.InteropServices.ComWra catch (System.Exception __exception) { __retVal_native = System.Runtime.InteropServices.Marshalling.ExceptionAsDefaultMarshaller.ConvertToUnmanaged(__exception); + return __retVal_native; } return __retVal_native; diff --git a/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.ComInterfaceGenerator/SharedTypes.ComInterfaces.IStatelessMarshalling.cs b/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.ComInterfaceGenerator/SharedTypes.ComInterfaces.IStatelessMarshalling.cs index ebc51bd..5a3045f 100644 --- a/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.ComInterfaceGenerator/SharedTypes.ComInterfaces.IStatelessMarshalling.cs +++ b/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.ComInterfaceGenerator/SharedTypes.ComInterfaces.IStatelessMarshalling.cs @@ -65,27 +65,27 @@ [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.Interop.ComInterfaceGenerator", "42.42.42.42")] [global::System.Runtime.CompilerServices.SkipLocalsInitAttribute] - void global::SharedTypes.ComInterfaces.IStatelessMarshalling.MethodOut(out global::SharedTypes.ComInterfaces.StatelessType param, int size) + void global::SharedTypes.ComInterfaces.IStatelessMarshalling.MethodOut(out global::SharedTypes.ComInterfaces.StatelessType paramyBoi, int size) { var(__this, __vtable_native) = ((System.Runtime.InteropServices.Marshalling.IUnmanagedVirtualMethodTableProvider)this).GetVirtualMethodTableInfoForKey(typeof(global::SharedTypes.ComInterfaces.IStatelessMarshalling)); - System.Runtime.CompilerServices.Unsafe.SkipInit(out param); - nint __param_native = default; + System.Runtime.CompilerServices.Unsafe.SkipInit(out paramyBoi); + nint __paramyBoi_native = default; int __invokeRetVal = default; try { { - __invokeRetVal = ((delegate* unmanaged[MemberFunction] )__vtable_native[5])(__this, &__param_native, size); + __invokeRetVal = ((delegate* unmanaged[MemberFunction] )__vtable_native[5])(__this, &__paramyBoi_native, size); } System.GC.KeepAlive(this); // Unmarshal - Convert native data to managed data. System.Runtime.InteropServices.Marshal.ThrowExceptionForHR(__invokeRetVal); - param = global::SharedTypes.ComInterfaces.StatelessTypeMarshaller.ConvertToManaged(__param_native); + paramyBoi = global::SharedTypes.ComInterfaces.StatelessTypeMarshaller.ConvertToManaged(__paramyBoi_native); } finally { // Cleanup - Perform required cleanup. - global::SharedTypes.ComInterfaces.StatelessTypeMarshaller.Free(__param_native); + global::SharedTypes.ComInterfaces.StatelessTypeMarshaller.Free(__paramyBoi_native); } } @@ -190,6 +190,7 @@ internal static int ABI_Method(System.Runtime.InteropServices.ComWrappers.ComInt catch (System.Exception __exception) { __retVal = System.Runtime.InteropServices.Marshalling.ExceptionAsHResultMarshaller.ConvertToUnmanaged(__exception); + return __retVal; } return __retVal; @@ -199,7 +200,7 @@ internal static int ABI_Method(System.Runtime.InteropServices.ComWrappers.ComInt internal static int ABI_MethodIn(System.Runtime.InteropServices.ComWrappers.ComInterfaceDispatch* __this_native, nint* __param_native__param, int size) { global::SharedTypes.ComInterfaces.IStatelessMarshalling @this = default; - ref nint __param_native = ref *__param_native__param; + nint __param_native = *__param_native__param; global::SharedTypes.ComInterfaces.StatelessType param = default; int __retVal = default; try @@ -213,30 +214,36 @@ internal static int ABI_MethodIn(System.Runtime.InteropServices.ComWrappers.ComI catch (System.Exception __exception) { __retVal = System.Runtime.InteropServices.Marshalling.ExceptionAsHResultMarshaller.ConvertToUnmanaged(__exception); + return __retVal; } return __retVal; } [System.Runtime.InteropServices.UnmanagedCallersOnlyAttribute(CallConvs = new[] { typeof(System.Runtime.CompilerServices.CallConvMemberFunction) })] - internal static int ABI_MethodOut(System.Runtime.InteropServices.ComWrappers.ComInterfaceDispatch* __this_native, nint* __param_native__param, int size) + internal static int ABI_MethodOut(System.Runtime.InteropServices.ComWrappers.ComInterfaceDispatch* __this_native, nint* __paramyBoi_native__param, int size) { global::SharedTypes.ComInterfaces.IStatelessMarshalling @this = default; - ref nint __param_native = ref *__param_native__param; - global::SharedTypes.ComInterfaces.StatelessType param = default; + nint __paramyBoi_native__out = default; + global::SharedTypes.ComInterfaces.StatelessType paramyBoi = default; int __retVal = default; try { // Unmarshal - Convert native data to managed data. __retVal = 0; // S_OK @this = System.Runtime.InteropServices.ComWrappers.ComInterfaceDispatch.GetInstance(__this_native); - @this.MethodOut(out param, size); + @this.MethodOut(out paramyBoi, size); // Marshal - Convert managed data to native data. - __param_native = global::SharedTypes.ComInterfaces.StatelessTypeMarshaller.ConvertToUnmanaged(param); + __paramyBoi_native__out = global::SharedTypes.ComInterfaces.StatelessTypeMarshaller.ConvertToUnmanaged(paramyBoi); + // AssignOut - Assign to parameters + *__paramyBoi_native__param = __paramyBoi_native__out; } catch (System.Exception __exception) { __retVal = System.Runtime.InteropServices.Marshalling.ExceptionAsHResultMarshaller.ConvertToUnmanaged(__exception); + // CleanupFailure - Perform required cleanup. + global::SharedTypes.ComInterfaces.StatelessTypeMarshaller.Free(__paramyBoi_native__out); + return __retVal; } return __retVal; @@ -246,12 +253,10 @@ internal static int ABI_MethodOut(System.Runtime.InteropServices.ComWrappers.Com internal static int ABI_MethodRef(System.Runtime.InteropServices.ComWrappers.ComInterfaceDispatch* __this_native, nint* __param_native__param, int size) { global::SharedTypes.ComInterfaces.IStatelessMarshalling @this = default; - ref nint __param_native = ref *__param_native__param; + nint __param_native__out = default; + nint __param_native = *__param_native__param; global::SharedTypes.ComInterfaces.StatelessType param = default; int __retVal = default; - // Setup - Perform required setup. - bool __param_native__ownOriginal = false; - nint __param_native__original = __param_native; try { // Unmarshal - Convert native data to managed data. @@ -260,20 +265,18 @@ internal static int ABI_MethodRef(System.Runtime.InteropServices.ComWrappers.Com @this = System.Runtime.InteropServices.ComWrappers.ComInterfaceDispatch.GetInstance(__this_native); @this.MethodRef(ref param, size); // Marshal - Convert managed data to native data. - __param_native = global::SharedTypes.ComInterfaces.StatelessTypeMarshaller.ConvertToUnmanaged(param); - __param_native__ownOriginal = true; + __param_native__out = global::SharedTypes.ComInterfaces.StatelessTypeMarshaller.ConvertToUnmanaged(param); + // AssignOut - Assign to parameters + *__param_native__param = __param_native__out; + // Cleanup - Perform required cleanup. + global::SharedTypes.ComInterfaces.StatelessTypeMarshaller.Free(__param_native); } catch (System.Exception __exception) { __retVal = System.Runtime.InteropServices.Marshalling.ExceptionAsHResultMarshaller.ConvertToUnmanaged(__exception); - } - finally - { - // Cleanup - Perform required cleanup. - if (__param_native__ownOriginal) - { - global::SharedTypes.ComInterfaces.StatelessTypeMarshaller.Free(__param_native__original); - } + // CleanupFailure - Perform required cleanup. + global::SharedTypes.ComInterfaces.StatelessTypeMarshaller.Free(__param_native__out); + return __retVal; } return __retVal; @@ -283,7 +286,7 @@ internal static int ABI_MethodRef(System.Runtime.InteropServices.ComWrappers.Com internal static int ABI_Return(System.Runtime.InteropServices.ComWrappers.ComInterfaceDispatch* __this_native, nint* __invokeRetValUnmanaged__param) { global::SharedTypes.ComInterfaces.IStatelessMarshalling @this = default; - ref nint __invokeRetValUnmanaged = ref *__invokeRetValUnmanaged__param; + nint __invokeRetValUnmanaged__out = default; global::SharedTypes.ComInterfaces.StatelessType __invokeRetVal = default; int __retVal = default; try @@ -293,11 +296,16 @@ internal static int ABI_Return(System.Runtime.InteropServices.ComWrappers.ComInt @this = System.Runtime.InteropServices.ComWrappers.ComInterfaceDispatch.GetInstance(__this_native); __invokeRetVal = @this.Return(); // Marshal - Convert managed data to native data. - __invokeRetValUnmanaged = global::SharedTypes.ComInterfaces.StatelessTypeMarshaller.ConvertToUnmanaged(__invokeRetVal); + __invokeRetValUnmanaged__out = global::SharedTypes.ComInterfaces.StatelessTypeMarshaller.ConvertToUnmanaged(__invokeRetVal); + // AssignOut - Assign to parameters + *__invokeRetValUnmanaged__param = __invokeRetValUnmanaged__out; } catch (System.Exception __exception) { __retVal = System.Runtime.InteropServices.Marshalling.ExceptionAsHResultMarshaller.ConvertToUnmanaged(__exception); + // CleanupFailure - Perform required cleanup. + global::SharedTypes.ComInterfaces.StatelessTypeMarshaller.Free(__invokeRetValUnmanaged__out); + return __retVal; } return __retVal; @@ -320,6 +328,7 @@ internal static nint ABI_ReturnPreserveSig(System.Runtime.InteropServices.ComWra catch (System.Exception __exception) { __retVal_native = System.Runtime.InteropServices.Marshalling.ExceptionAsDefaultMarshaller.ConvertToUnmanaged(__exception); + return __retVal_native; } return __retVal_native; diff --git a/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.ComInterfaceGenerator/SharedTypes.ComInterfaces.IStatelessPinnedMarshalling.cs b/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.ComInterfaceGenerator/SharedTypes.ComInterfaces.IStatelessPinnedMarshalling.cs index a47d487..a640544 100644 --- a/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.ComInterfaceGenerator/SharedTypes.ComInterfaces.IStatelessPinnedMarshalling.cs +++ b/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.ComInterfaceGenerator/SharedTypes.ComInterfaces.IStatelessPinnedMarshalling.cs @@ -181,6 +181,7 @@ internal static int ABI_Method(System.Runtime.InteropServices.ComWrappers.ComInt catch (System.Exception __exception) { __retVal = System.Runtime.InteropServices.Marshalling.ExceptionAsHResultMarshaller.ConvertToUnmanaged(__exception); + return __retVal; } return __retVal; @@ -190,7 +191,7 @@ internal static int ABI_Method(System.Runtime.InteropServices.ComWrappers.ComInt internal static int ABI_MethodIn(System.Runtime.InteropServices.ComWrappers.ComInterfaceDispatch* __this_native, nint* __param_native__param, int size) { global::SharedTypes.ComInterfaces.IStatelessPinnedMarshalling @this = default; - ref nint __param_native = ref *__param_native__param; + nint __param_native = *__param_native__param; global::SharedTypes.ComInterfaces.StatelessPinnedType param = default; int __retVal = default; try @@ -204,6 +205,7 @@ internal static int ABI_MethodIn(System.Runtime.InteropServices.ComWrappers.ComI catch (System.Exception __exception) { __retVal = System.Runtime.InteropServices.Marshalling.ExceptionAsHResultMarshaller.ConvertToUnmanaged(__exception); + return __retVal; } return __retVal; @@ -213,7 +215,7 @@ internal static int ABI_MethodIn(System.Runtime.InteropServices.ComWrappers.ComI internal static int ABI_MethodOut(System.Runtime.InteropServices.ComWrappers.ComInterfaceDispatch* __this_native, nint* __param_native__param, int size) { global::SharedTypes.ComInterfaces.IStatelessPinnedMarshalling @this = default; - ref nint __param_native = ref *__param_native__param; + nint __param_native__out = default; global::SharedTypes.ComInterfaces.StatelessPinnedType param = default; int __retVal = default; try @@ -223,11 +225,16 @@ internal static int ABI_MethodOut(System.Runtime.InteropServices.ComWrappers.Com @this = System.Runtime.InteropServices.ComWrappers.ComInterfaceDispatch.GetInstance(__this_native); @this.MethodOut(out param, size); // Marshal - Convert managed data to native data. - __param_native = global::SharedTypes.ComInterfaces.StatelessPinnedTypeMarshaller.ConvertToUnmanaged(param); + __param_native__out = global::SharedTypes.ComInterfaces.StatelessPinnedTypeMarshaller.ConvertToUnmanaged(param); + // AssignOut - Assign to parameters + *__param_native__param = __param_native__out; } catch (System.Exception __exception) { __retVal = System.Runtime.InteropServices.Marshalling.ExceptionAsHResultMarshaller.ConvertToUnmanaged(__exception); + // CleanupFailure - Perform required cleanup. + global::SharedTypes.ComInterfaces.StatelessPinnedTypeMarshaller.Free(__param_native__out); + return __retVal; } return __retVal; @@ -237,12 +244,10 @@ internal static int ABI_MethodOut(System.Runtime.InteropServices.ComWrappers.Com internal static int ABI_MethodRef(System.Runtime.InteropServices.ComWrappers.ComInterfaceDispatch* __this_native, nint* __param_native__param, int size) { global::SharedTypes.ComInterfaces.IStatelessPinnedMarshalling @this = default; - ref nint __param_native = ref *__param_native__param; + nint __param_native__out = default; + nint __param_native = *__param_native__param; global::SharedTypes.ComInterfaces.StatelessPinnedType param = default; int __retVal = default; - // Setup - Perform required setup. - bool __param_native__ownOriginal = false; - nint __param_native__original = __param_native; try { // Unmarshal - Convert native data to managed data. @@ -251,20 +256,18 @@ internal static int ABI_MethodRef(System.Runtime.InteropServices.ComWrappers.Com @this = System.Runtime.InteropServices.ComWrappers.ComInterfaceDispatch.GetInstance(__this_native); @this.MethodRef(ref param, size); // Marshal - Convert managed data to native data. - __param_native = global::SharedTypes.ComInterfaces.StatelessPinnedTypeMarshaller.ConvertToUnmanaged(param); - __param_native__ownOriginal = true; + __param_native__out = global::SharedTypes.ComInterfaces.StatelessPinnedTypeMarshaller.ConvertToUnmanaged(param); + // AssignOut - Assign to parameters + *__param_native__param = __param_native__out; + // Cleanup - Perform required cleanup. + global::SharedTypes.ComInterfaces.StatelessPinnedTypeMarshaller.Free(__param_native); } catch (System.Exception __exception) { __retVal = System.Runtime.InteropServices.Marshalling.ExceptionAsHResultMarshaller.ConvertToUnmanaged(__exception); - } - finally - { - // Cleanup - Perform required cleanup. - if (__param_native__ownOriginal) - { - global::SharedTypes.ComInterfaces.StatelessPinnedTypeMarshaller.Free(__param_native__original); - } + // CleanupFailure - Perform required cleanup. + global::SharedTypes.ComInterfaces.StatelessPinnedTypeMarshaller.Free(__param_native__out); + return __retVal; } return __retVal; @@ -274,7 +277,7 @@ internal static int ABI_MethodRef(System.Runtime.InteropServices.ComWrappers.Com internal static int ABI_Return(System.Runtime.InteropServices.ComWrappers.ComInterfaceDispatch* __this_native, nint* __invokeRetValUnmanaged__param) { global::SharedTypes.ComInterfaces.IStatelessPinnedMarshalling @this = default; - ref nint __invokeRetValUnmanaged = ref *__invokeRetValUnmanaged__param; + nint __invokeRetValUnmanaged__out = default; global::SharedTypes.ComInterfaces.StatelessPinnedType __invokeRetVal = default; int __retVal = default; try @@ -284,11 +287,16 @@ internal static int ABI_Return(System.Runtime.InteropServices.ComWrappers.ComInt @this = System.Runtime.InteropServices.ComWrappers.ComInterfaceDispatch.GetInstance(__this_native); __invokeRetVal = @this.Return(); // Marshal - Convert managed data to native data. - __invokeRetValUnmanaged = global::SharedTypes.ComInterfaces.StatelessPinnedTypeMarshaller.ConvertToUnmanaged(__invokeRetVal); + __invokeRetValUnmanaged__out = global::SharedTypes.ComInterfaces.StatelessPinnedTypeMarshaller.ConvertToUnmanaged(__invokeRetVal); + // AssignOut - Assign to parameters + *__invokeRetValUnmanaged__param = __invokeRetValUnmanaged__out; } catch (System.Exception __exception) { __retVal = System.Runtime.InteropServices.Marshalling.ExceptionAsHResultMarshaller.ConvertToUnmanaged(__exception); + // CleanupFailure - Perform required cleanup. + global::SharedTypes.ComInterfaces.StatelessPinnedTypeMarshaller.Free(__invokeRetValUnmanaged__out); + return __retVal; } return __retVal; @@ -311,6 +319,7 @@ internal static nint ABI_ReturnPreserveSig(System.Runtime.InteropServices.ComWra catch (System.Exception __exception) { __retVal_native = System.Runtime.InteropServices.Marshalling.ExceptionAsDefaultMarshaller.ConvertToUnmanaged(__exception); + return __retVal_native; } return __retVal_native; diff --git a/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.ComInterfaceGenerator/SharedTypes.ComInterfaces.IStringMarshallingOverride.cs b/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.ComInterfaceGenerator/SharedTypes.ComInterfaces.IStringMarshallingOverride.cs index 2dc1608..606de1e 100644 --- a/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.ComInterfaceGenerator/SharedTypes.ComInterfaces.IStringMarshallingOverride.cs +++ b/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.ComInterfaceGenerator/SharedTypes.ComInterfaces.IStringMarshallingOverride.cs @@ -115,7 +115,7 @@ internal static int ABI_StringMarshallingUtf8(System.Runtime.InteropServices.Com { global::SharedTypes.ComInterfaces.IStringMarshallingOverride @this = default; string input = default; - ref byte* __invokeRetValUnmanaged = ref *__invokeRetValUnmanaged__param; + byte* __invokeRetValUnmanaged__out = default; string __invokeRetVal = default; int __retVal = default; try @@ -126,11 +126,16 @@ internal static int ABI_StringMarshallingUtf8(System.Runtime.InteropServices.Com @this = System.Runtime.InteropServices.ComWrappers.ComInterfaceDispatch.GetInstance(__this_native); __invokeRetVal = @this.StringMarshallingUtf8(input); // Marshal - Convert managed data to native data. - __invokeRetValUnmanaged = (byte*)global::System.Runtime.InteropServices.Marshalling.Utf8StringMarshaller.ConvertToUnmanaged(__invokeRetVal); + __invokeRetValUnmanaged__out = (byte*)global::System.Runtime.InteropServices.Marshalling.Utf8StringMarshaller.ConvertToUnmanaged(__invokeRetVal); + // AssignOut - Assign to parameters + *__invokeRetValUnmanaged__param = __invokeRetValUnmanaged__out; } catch (System.Exception __exception) { __retVal = System.Runtime.InteropServices.Marshalling.ExceptionAsHResultMarshaller.ConvertToUnmanaged(__exception); + // CleanupFailure - Perform required cleanup. + global::System.Runtime.InteropServices.Marshalling.Utf8StringMarshaller.Free(__invokeRetValUnmanaged__out); + return __retVal; } return __retVal; @@ -141,7 +146,7 @@ internal static int ABI_MarshalAsLPWString(System.Runtime.InteropServices.ComWra { global::SharedTypes.ComInterfaces.IStringMarshallingOverride @this = default; string input = default; - ref ushort* __invokeRetValUnmanaged = ref *__invokeRetValUnmanaged__param; + ushort* __invokeRetValUnmanaged__out = default; string __invokeRetVal = default; int __retVal = default; try @@ -152,11 +157,16 @@ internal static int ABI_MarshalAsLPWString(System.Runtime.InteropServices.ComWra @this = System.Runtime.InteropServices.ComWrappers.ComInterfaceDispatch.GetInstance(__this_native); __invokeRetVal = @this.MarshalAsLPWString(input); // Marshal - Convert managed data to native data. - __invokeRetValUnmanaged = (ushort*)global::System.Runtime.InteropServices.Marshalling.Utf16StringMarshaller.ConvertToUnmanaged(__invokeRetVal); + __invokeRetValUnmanaged__out = (ushort*)global::System.Runtime.InteropServices.Marshalling.Utf16StringMarshaller.ConvertToUnmanaged(__invokeRetVal); + // AssignOut - Assign to parameters + *__invokeRetValUnmanaged__param = __invokeRetValUnmanaged__out; } catch (System.Exception __exception) { __retVal = System.Runtime.InteropServices.Marshalling.ExceptionAsHResultMarshaller.ConvertToUnmanaged(__exception); + // CleanupFailure - Perform required cleanup. + global::System.Runtime.InteropServices.Marshalling.Utf16StringMarshaller.Free(__invokeRetValUnmanaged__out); + return __retVal; } return __retVal; @@ -167,7 +177,7 @@ internal static int ABI_MarshalUsingUtf16(System.Runtime.InteropServices.ComWrap { global::SharedTypes.ComInterfaces.IStringMarshallingOverride @this = default; string input = default; - ref ushort* __invokeRetValUnmanaged = ref *__invokeRetValUnmanaged__param; + ushort* __invokeRetValUnmanaged__out = default; string __invokeRetVal = default; int __retVal = default; try @@ -178,11 +188,16 @@ internal static int ABI_MarshalUsingUtf16(System.Runtime.InteropServices.ComWrap @this = System.Runtime.InteropServices.ComWrappers.ComInterfaceDispatch.GetInstance(__this_native); __invokeRetVal = @this.MarshalUsingUtf16(input); // Marshal - Convert managed data to native data. - __invokeRetValUnmanaged = (ushort*)global::System.Runtime.InteropServices.Marshalling.Utf16StringMarshaller.ConvertToUnmanaged(__invokeRetVal); + __invokeRetValUnmanaged__out = (ushort*)global::System.Runtime.InteropServices.Marshalling.Utf16StringMarshaller.ConvertToUnmanaged(__invokeRetVal); + // AssignOut - Assign to parameters + *__invokeRetValUnmanaged__param = __invokeRetValUnmanaged__out; } catch (System.Exception __exception) { __retVal = System.Runtime.InteropServices.Marshalling.ExceptionAsHResultMarshaller.ConvertToUnmanaged(__exception); + // CleanupFailure - Perform required cleanup. + global::System.Runtime.InteropServices.Marshalling.Utf16StringMarshaller.Free(__invokeRetValUnmanaged__out); + return __retVal; } return __retVal; diff --git a/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.ComInterfaceGenerator/SharedTypes.ComInterfaces.IStringMarshallingOverrideDerived.cs b/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.ComInterfaceGenerator/SharedTypes.ComInterfaces.IStringMarshallingOverrideDerived.cs index ce333fc..6de4963 100644 --- a/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.ComInterfaceGenerator/SharedTypes.ComInterfaces.IStringMarshallingOverrideDerived.cs +++ b/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.ComInterfaceGenerator/SharedTypes.ComInterfaces.IStringMarshallingOverrideDerived.cs @@ -215,7 +215,7 @@ internal static int ABI_StringMarshallingUtf8_2(System.Runtime.InteropServices.C { global::SharedTypes.ComInterfaces.IStringMarshallingOverrideDerived @this = default; string input = default; - ref byte* __invokeRetValUnmanaged = ref *__invokeRetValUnmanaged__param; + byte* __invokeRetValUnmanaged__out = default; string __invokeRetVal = default; int __retVal = default; try @@ -226,11 +226,16 @@ internal static int ABI_StringMarshallingUtf8_2(System.Runtime.InteropServices.C @this = System.Runtime.InteropServices.ComWrappers.ComInterfaceDispatch.GetInstance(__this_native); __invokeRetVal = @this.StringMarshallingUtf8_2(input); // Marshal - Convert managed data to native data. - __invokeRetValUnmanaged = (byte*)global::System.Runtime.InteropServices.Marshalling.Utf8StringMarshaller.ConvertToUnmanaged(__invokeRetVal); + __invokeRetValUnmanaged__out = (byte*)global::System.Runtime.InteropServices.Marshalling.Utf8StringMarshaller.ConvertToUnmanaged(__invokeRetVal); + // AssignOut - Assign to parameters + *__invokeRetValUnmanaged__param = __invokeRetValUnmanaged__out; } catch (System.Exception __exception) { __retVal = System.Runtime.InteropServices.Marshalling.ExceptionAsHResultMarshaller.ConvertToUnmanaged(__exception); + // CleanupFailure - Perform required cleanup. + global::System.Runtime.InteropServices.Marshalling.Utf8StringMarshaller.Free(__invokeRetValUnmanaged__out); + return __retVal; } return __retVal; @@ -241,7 +246,7 @@ internal static int ABI_MarshalAsLPWString_2(System.Runtime.InteropServices.ComW { global::SharedTypes.ComInterfaces.IStringMarshallingOverrideDerived @this = default; string input = default; - ref ushort* __invokeRetValUnmanaged = ref *__invokeRetValUnmanaged__param; + ushort* __invokeRetValUnmanaged__out = default; string __invokeRetVal = default; int __retVal = default; try @@ -252,11 +257,16 @@ internal static int ABI_MarshalAsLPWString_2(System.Runtime.InteropServices.ComW @this = System.Runtime.InteropServices.ComWrappers.ComInterfaceDispatch.GetInstance(__this_native); __invokeRetVal = @this.MarshalAsLPWString_2(input); // Marshal - Convert managed data to native data. - __invokeRetValUnmanaged = (ushort*)global::System.Runtime.InteropServices.Marshalling.Utf16StringMarshaller.ConvertToUnmanaged(__invokeRetVal); + __invokeRetValUnmanaged__out = (ushort*)global::System.Runtime.InteropServices.Marshalling.Utf16StringMarshaller.ConvertToUnmanaged(__invokeRetVal); + // AssignOut - Assign to parameters + *__invokeRetValUnmanaged__param = __invokeRetValUnmanaged__out; } catch (System.Exception __exception) { __retVal = System.Runtime.InteropServices.Marshalling.ExceptionAsHResultMarshaller.ConvertToUnmanaged(__exception); + // CleanupFailure - Perform required cleanup. + global::System.Runtime.InteropServices.Marshalling.Utf16StringMarshaller.Free(__invokeRetValUnmanaged__out); + return __retVal; } return __retVal; @@ -267,7 +277,7 @@ internal static int ABI_MarshalUsingUtf16_2(System.Runtime.InteropServices.ComWr { global::SharedTypes.ComInterfaces.IStringMarshallingOverrideDerived @this = default; string input = default; - ref ushort* __invokeRetValUnmanaged = ref *__invokeRetValUnmanaged__param; + ushort* __invokeRetValUnmanaged__out = default; string __invokeRetVal = default; int __retVal = default; try @@ -278,11 +288,16 @@ internal static int ABI_MarshalUsingUtf16_2(System.Runtime.InteropServices.ComWr @this = System.Runtime.InteropServices.ComWrappers.ComInterfaceDispatch.GetInstance(__this_native); __invokeRetVal = @this.MarshalUsingUtf16_2(input); // Marshal - Convert managed data to native data. - __invokeRetValUnmanaged = (ushort*)global::System.Runtime.InteropServices.Marshalling.Utf16StringMarshaller.ConvertToUnmanaged(__invokeRetVal); + __invokeRetValUnmanaged__out = (ushort*)global::System.Runtime.InteropServices.Marshalling.Utf16StringMarshaller.ConvertToUnmanaged(__invokeRetVal); + // AssignOut - Assign to parameters + *__invokeRetValUnmanaged__param = __invokeRetValUnmanaged__out; } catch (System.Exception __exception) { __retVal = System.Runtime.InteropServices.Marshalling.ExceptionAsHResultMarshaller.ConvertToUnmanaged(__exception); + // CleanupFailure - Perform required cleanup. + global::System.Runtime.InteropServices.Marshalling.Utf16StringMarshaller.Free(__invokeRetValUnmanaged__out); + return __retVal; } return __retVal; diff --git a/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.ComInterfaceGenerator/SharedTypes.ComInterfaces.IUTF16Marshalling.cs b/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.ComInterfaceGenerator/SharedTypes.ComInterfaces.IUTF16Marshalling.cs index 6386ee3..d82d3eb 100644 --- a/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.ComInterfaceGenerator/SharedTypes.ComInterfaces.IUTF16Marshalling.cs +++ b/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.ComInterfaceGenerator/SharedTypes.ComInterfaces.IUTF16Marshalling.cs @@ -63,7 +63,7 @@ file unsafe partial interface InterfaceImplementation internal static int ABI_GetString(System.Runtime.InteropServices.ComWrappers.ComInterfaceDispatch* __this_native, ushort** __invokeRetValUnmanaged__param) { global::SharedTypes.ComInterfaces.IUTF16Marshalling @this = default; - ref ushort* __invokeRetValUnmanaged = ref *__invokeRetValUnmanaged__param; + ushort* __invokeRetValUnmanaged__out = default; string __invokeRetVal = default; int __retVal = default; try @@ -73,11 +73,16 @@ internal static int ABI_GetString(System.Runtime.InteropServices.ComWrappers.Com @this = System.Runtime.InteropServices.ComWrappers.ComInterfaceDispatch.GetInstance(__this_native); __invokeRetVal = @this.GetString(); // Marshal - Convert managed data to native data. - __invokeRetValUnmanaged = (ushort*)global::System.Runtime.InteropServices.Marshalling.Utf16StringMarshaller.ConvertToUnmanaged(__invokeRetVal); + __invokeRetValUnmanaged__out = (ushort*)global::System.Runtime.InteropServices.Marshalling.Utf16StringMarshaller.ConvertToUnmanaged(__invokeRetVal); + // AssignOut - Assign to parameters + *__invokeRetValUnmanaged__param = __invokeRetValUnmanaged__out; } catch (System.Exception __exception) { __retVal = System.Runtime.InteropServices.Marshalling.ExceptionAsHResultMarshaller.ConvertToUnmanaged(__exception); + // CleanupFailure - Perform required cleanup. + global::System.Runtime.InteropServices.Marshalling.Utf16StringMarshaller.Free(__invokeRetValUnmanaged__out); + return __retVal; } return __retVal; @@ -100,6 +105,7 @@ internal static int ABI_SetString(System.Runtime.InteropServices.ComWrappers.Com catch (System.Exception __exception) { __retVal = System.Runtime.InteropServices.Marshalling.ExceptionAsHResultMarshaller.ConvertToUnmanaged(__exception); + return __retVal; } return __retVal; diff --git a/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.ComInterfaceGenerator/SharedTypes.ComInterfaces.IUTF8Marshalling.cs b/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.ComInterfaceGenerator/SharedTypes.ComInterfaces.IUTF8Marshalling.cs index a7ad88a..14023af 100644 --- a/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.ComInterfaceGenerator/SharedTypes.ComInterfaces.IUTF8Marshalling.cs +++ b/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.ComInterfaceGenerator/SharedTypes.ComInterfaces.IUTF8Marshalling.cs @@ -76,7 +76,7 @@ file unsafe partial interface InterfaceImplementation internal static int ABI_GetString(System.Runtime.InteropServices.ComWrappers.ComInterfaceDispatch* __this_native, byte** __invokeRetValUnmanaged__param) { global::SharedTypes.ComInterfaces.IUTF8Marshalling @this = default; - ref byte* __invokeRetValUnmanaged = ref *__invokeRetValUnmanaged__param; + byte* __invokeRetValUnmanaged__out = default; string __invokeRetVal = default; int __retVal = default; try @@ -86,11 +86,16 @@ internal static int ABI_GetString(System.Runtime.InteropServices.ComWrappers.Com @this = System.Runtime.InteropServices.ComWrappers.ComInterfaceDispatch.GetInstance(__this_native); __invokeRetVal = @this.GetString(); // Marshal - Convert managed data to native data. - __invokeRetValUnmanaged = (byte*)global::System.Runtime.InteropServices.Marshalling.Utf8StringMarshaller.ConvertToUnmanaged(__invokeRetVal); + __invokeRetValUnmanaged__out = (byte*)global::System.Runtime.InteropServices.Marshalling.Utf8StringMarshaller.ConvertToUnmanaged(__invokeRetVal); + // AssignOut - Assign to parameters + *__invokeRetValUnmanaged__param = __invokeRetValUnmanaged__out; } catch (System.Exception __exception) { __retVal = System.Runtime.InteropServices.Marshalling.ExceptionAsHResultMarshaller.ConvertToUnmanaged(__exception); + // CleanupFailure - Perform required cleanup. + global::System.Runtime.InteropServices.Marshalling.Utf8StringMarshaller.Free(__invokeRetValUnmanaged__out); + return __retVal; } return __retVal; @@ -113,6 +118,7 @@ internal static int ABI_SetString(System.Runtime.InteropServices.ComWrappers.Com catch (System.Exception __exception) { __retVal = System.Runtime.InteropServices.Marshalling.ExceptionAsHResultMarshaller.ConvertToUnmanaged(__exception); + return __retVal; } return __retVal; diff --git a/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.ComInterfaceGenerator/SharedTypes.ComInterfaces.MarshallingFails.ICollectionMarshallingFails.cs b/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.ComInterfaceGenerator/SharedTypes.ComInterfaces.MarshallingFails.ICollectionMarshallingFails.cs index 7712c35..2f8e763 100644 --- a/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.ComInterfaceGenerator/SharedTypes.ComInterfaces.MarshallingFails.ICollectionMarshallingFails.cs +++ b/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.ComInterfaceGenerator/SharedTypes.ComInterfaces.MarshallingFails.ICollectionMarshallingFails.cs @@ -56,7 +56,6 @@ } } - __retVal_native__numElements = size; global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.Free(__retVal_native); } @@ -122,9 +121,9 @@ file unsafe partial interface InterfaceImplementation internal static int ABI_Get(System.Runtime.InteropServices.ComWrappers.ComInterfaceDispatch* __this_native, int* __size_native__param, nint** __invokeRetValUnmanaged__param) { global::SharedTypes.ComInterfaces.MarshallingFails.ICollectionMarshallingFails @this = default; - ref int __size_native = ref *__size_native__param; + int __size_native__out = default; int size = default; - ref nint* __invokeRetValUnmanaged = ref *__invokeRetValUnmanaged__param; + nint* __invokeRetValUnmanaged__out = default; int[] __invokeRetVal = default; int __retVal = default; // Setup - Perform required setup. @@ -137,21 +136,36 @@ internal static int ABI_Get(System.Runtime.InteropServices.ComWrappers.ComInterf @this = System.Runtime.InteropServices.ComWrappers.ComInterfaceDispatch.GetInstance(__this_native); __invokeRetVal = @this.Get(out size); // Marshal - Convert managed data to native data. - __size_native = size; - __invokeRetValUnmanaged = global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.AllocateContainerForUnmanagedElements(__invokeRetVal, out __invokeRetValUnmanaged__numElements); + __size_native__out = size; + __invokeRetValUnmanaged__out = global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.AllocateContainerForUnmanagedElements(__invokeRetVal, out __invokeRetValUnmanaged__numElements); { System.ReadOnlySpan __invokeRetValUnmanaged__managedSpan = global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.GetManagedValuesSource(__invokeRetVal); - System.Span __invokeRetValUnmanaged__nativeSpan = global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.GetUnmanagedValuesDestination(__invokeRetValUnmanaged, __invokeRetValUnmanaged__numElements); + System.Span __invokeRetValUnmanaged__nativeSpan = global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.GetUnmanagedValuesDestination(__invokeRetValUnmanaged__out, __invokeRetValUnmanaged__numElements); __invokeRetValUnmanaged__nativeSpan.Clear(); for (int __i0 = 0; __i0 < __invokeRetValUnmanaged__managedSpan.Length; ++__i0) { __invokeRetValUnmanaged__nativeSpan[__i0] = global::SharedTypes.ComInterfaces.MarshallingFails.ThrowOn4thElementMarshalled.ConvertToUnmanaged(__invokeRetValUnmanaged__managedSpan[__i0]); } } + + // AssignOut - Assign to parameters + *__size_native__param = __size_native__out; + *__invokeRetValUnmanaged__param = __invokeRetValUnmanaged__out; } catch (System.Exception __exception) { __retVal = System.Runtime.InteropServices.Marshalling.ExceptionAsHResultMarshaller.ConvertToUnmanaged(__exception); + // CleanupFailure - Perform required cleanup. + { + System.ReadOnlySpan __invokeRetValUnmanaged__nativeSpan = global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.GetUnmanagedValuesSource(__invokeRetValUnmanaged__out, __invokeRetValUnmanaged__numElements); + for (int __i0 = 0; __i0 < __invokeRetValUnmanaged__nativeSpan.Length; ++__i0) + { + global::SharedTypes.ComInterfaces.MarshallingFails.ThrowOn4thElementMarshalled.Free(__invokeRetValUnmanaged__nativeSpan[__i0]); + } + } + + global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.Free(__invokeRetValUnmanaged__out); + return __retVal; } return __retVal; @@ -187,6 +201,7 @@ internal static int ABI_Set(System.Runtime.InteropServices.ComWrappers.ComInterf catch (System.Exception __exception) { __retVal = System.Runtime.InteropServices.Marshalling.ExceptionAsHResultMarshaller.ConvertToUnmanaged(__exception); + return __retVal; } return __retVal; diff --git a/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.ComInterfaceGenerator/SharedTypes.ComInterfaces.MarshallingFails.IJaggedIntArrayMarshallingFails.cs b/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.ComInterfaceGenerator/SharedTypes.ComInterfaces.MarshallingFails.IJaggedIntArrayMarshallingFails.cs index dad9839..e3a8da4 100644 --- a/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.ComInterfaceGenerator/SharedTypes.ComInterfaces.MarshallingFails.IJaggedIntArrayMarshallingFails.cs +++ b/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.ComInterfaceGenerator/SharedTypes.ComInterfaces.MarshallingFails.IJaggedIntArrayMarshallingFails.cs @@ -66,7 +66,6 @@ finally { // Cleanup - Perform required cleanup. - __widths_native__numElements = length; global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.Free(__widths_native); { System.ReadOnlySpan __retVal_native__nativeSpan = global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.GetUnmanagedValuesDestination(__retVal_native, __retVal_native__numElements); @@ -83,12 +82,10 @@ } } - __retVal_native__nativeSpan____i0__numElements = widths[__i0]; global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.Free((nint*)__retVal_native__nativeSpan[__i0]); } } - __retVal_native__numElements = length; global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.Free(__retVal_native); } @@ -148,7 +145,6 @@ finally { // Cleanup - Perform required cleanup. - __widths_native__numElements = __retVal; global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.Free(__widths_native); { System.ReadOnlySpan __array_native__nativeSpan = global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.GetUnmanagedValuesDestination(__array_native, __array_native__numElements); @@ -165,12 +161,10 @@ } } - __array_native__nativeSpan____i0__numElements = widths[__i0]; global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.Free((nint*)__array_native__nativeSpan[__i0]); } } - __array_native__numElements = __retVal; global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.Free(__array_native); } @@ -242,7 +236,8 @@ { global::SharedTypes.ComInterfaces.MarshallingFails.ThrowOn4thElementMarshalled.Free(__array_native__nativeSpan____i0__nativeSpan[__i1]); } - }; + } + global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.Free((nint*)__array_native__nativeSpan[__i0]); } } @@ -258,11 +253,11 @@ file unsafe partial interface InterfaceImplementation internal static int ABI_Get(System.Runtime.InteropServices.ComWrappers.ComInterfaceDispatch* __this_native, int** __widths_native__param, int* __length_native__param, System.IntPtr** __invokeRetValUnmanaged__param) { global::SharedTypes.ComInterfaces.MarshallingFails.IJaggedIntArrayMarshallingFails @this = default; - ref int* __widths_native = ref *__widths_native__param; + int* __widths_native__out = default; int[] widths = default; - ref int __length_native = ref *__length_native__param; + int __length_native__out = default; int length = default; - ref System.IntPtr* __invokeRetValUnmanaged = ref *__invokeRetValUnmanaged__param; + System.IntPtr* __invokeRetValUnmanaged__out = default; int[][] __invokeRetVal = default; int __retVal = default; // Setup - Perform required setup. @@ -277,13 +272,13 @@ internal static int ABI_Get(System.Runtime.InteropServices.ComWrappers.ComInterf @this = System.Runtime.InteropServices.ComWrappers.ComInterfaceDispatch.GetInstance(__this_native); __invokeRetVal = @this.Get(out widths, out length); // Marshal - Convert managed data to native data. - __length_native = length; - __widths_native = global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.AllocateContainerForUnmanagedElements(widths, out __widths_native__numElements); - global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.GetManagedValuesSource(widths).CopyTo(global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.GetUnmanagedValuesDestination(__widths_native, __widths_native__numElements)); - __invokeRetValUnmanaged = global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.AllocateContainerForUnmanagedElements(__invokeRetVal, out __invokeRetValUnmanaged__numElements); + __length_native__out = length; + __widths_native__out = global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.AllocateContainerForUnmanagedElements(widths, out __widths_native__numElements); + global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.GetManagedValuesSource(widths).CopyTo(global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.GetUnmanagedValuesDestination(__widths_native__out, __widths_native__numElements)); + __invokeRetValUnmanaged__out = global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.AllocateContainerForUnmanagedElements(__invokeRetVal, out __invokeRetValUnmanaged__numElements); { System.ReadOnlySpan __invokeRetValUnmanaged__managedSpan = global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.GetManagedValuesSource(__invokeRetVal); - System.Span __invokeRetValUnmanaged__nativeSpan = global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.GetUnmanagedValuesDestination(__invokeRetValUnmanaged, __invokeRetValUnmanaged__numElements); + System.Span __invokeRetValUnmanaged__nativeSpan = global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.GetUnmanagedValuesDestination(__invokeRetValUnmanaged__out, __invokeRetValUnmanaged__numElements); __invokeRetValUnmanaged__nativeSpan.Clear(); for (int __i0 = 0; __i0 < __invokeRetValUnmanaged__managedSpan.Length; ++__i0) { @@ -301,10 +296,38 @@ internal static int ABI_Get(System.Runtime.InteropServices.ComWrappers.ComInterf } } } + + // AssignOut - Assign to parameters + *__length_native__param = __length_native__out; + *__widths_native__param = __widths_native__out; + *__invokeRetValUnmanaged__param = __invokeRetValUnmanaged__out; } catch (System.Exception __exception) { __retVal = System.Runtime.InteropServices.Marshalling.ExceptionAsHResultMarshaller.ConvertToUnmanaged(__exception); + // CleanupFailure - Perform required cleanup. + global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.Free(__widths_native__out); + { + System.ReadOnlySpan __invokeRetValUnmanaged__nativeSpan = global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.GetUnmanagedValuesSource(__invokeRetValUnmanaged__out, __invokeRetValUnmanaged__numElements); + for (int __i0 = 0; __i0 < __invokeRetValUnmanaged__nativeSpan.Length; ++__i0) + { + int __invokeRetValUnmanaged__nativeSpan____i0__numElements; + System.Runtime.CompilerServices.Unsafe.SkipInit(out __invokeRetValUnmanaged__nativeSpan____i0__numElements); + __invokeRetValUnmanaged__nativeSpan____i0__numElements = 0; + { + System.ReadOnlySpan __invokeRetValUnmanaged__nativeSpan____i0__nativeSpan = global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.GetUnmanagedValuesSource((nint*)__invokeRetValUnmanaged__nativeSpan[__i0], __invokeRetValUnmanaged__nativeSpan____i0__numElements); + for (int __i1 = 0; __i1 < __invokeRetValUnmanaged__nativeSpan____i0__nativeSpan.Length; ++__i1) + { + global::SharedTypes.ComInterfaces.MarshallingFails.ThrowOn4thElementMarshalled.Free(__invokeRetValUnmanaged__nativeSpan____i0__nativeSpan[__i1]); + } + } + + global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.Free((nint*)__invokeRetValUnmanaged__nativeSpan[__i0]); + } + } + + global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.Free(__invokeRetValUnmanaged__out); + return __retVal; } return __retVal; @@ -314,11 +337,11 @@ internal static int ABI_Get(System.Runtime.InteropServices.ComWrappers.ComInterf internal static int ABI_Get2(System.Runtime.InteropServices.ComWrappers.ComInterfaceDispatch* __this_native, System.IntPtr** __array_native__param, int** __widths_native__param, int* __invokeRetValUnmanaged__param) { global::SharedTypes.ComInterfaces.MarshallingFails.IJaggedIntArrayMarshallingFails @this = default; - ref System.IntPtr* __array_native = ref *__array_native__param; + System.IntPtr* __array_native__out = default; int[][] array = default; - ref int* __widths_native = ref *__widths_native__param; + int* __widths_native__out = default; int[] widths = default; - ref int __invokeRetValUnmanaged = ref *__invokeRetValUnmanaged__param; + int __invokeRetValUnmanaged__out = default; int __invokeRetVal = default; int __retVal = default; // Setup - Perform required setup. @@ -333,13 +356,13 @@ internal static int ABI_Get2(System.Runtime.InteropServices.ComWrappers.ComInter @this = System.Runtime.InteropServices.ComWrappers.ComInterfaceDispatch.GetInstance(__this_native); __invokeRetVal = @this.Get2(out array, out widths); // Marshal - Convert managed data to native data. - __invokeRetValUnmanaged = __invokeRetVal; - __widths_native = global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.AllocateContainerForUnmanagedElements(widths, out __widths_native__numElements); - global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.GetManagedValuesSource(widths).CopyTo(global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.GetUnmanagedValuesDestination(__widths_native, __widths_native__numElements)); - __array_native = global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.AllocateContainerForUnmanagedElements(array, out __array_native__numElements); + __invokeRetValUnmanaged__out = __invokeRetVal; + __widths_native__out = global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.AllocateContainerForUnmanagedElements(widths, out __widths_native__numElements); + global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.GetManagedValuesSource(widths).CopyTo(global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.GetUnmanagedValuesDestination(__widths_native__out, __widths_native__numElements)); + __array_native__out = global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.AllocateContainerForUnmanagedElements(array, out __array_native__numElements); { System.ReadOnlySpan __array_native__managedSpan = global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.GetManagedValuesSource(array); - System.Span __array_native__nativeSpan = global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.GetUnmanagedValuesDestination(__array_native, __array_native__numElements); + System.Span __array_native__nativeSpan = global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.GetUnmanagedValuesDestination(__array_native__out, __array_native__numElements); __array_native__nativeSpan.Clear(); for (int __i0 = 0; __i0 < __array_native__managedSpan.Length; ++__i0) { @@ -357,10 +380,38 @@ internal static int ABI_Get2(System.Runtime.InteropServices.ComWrappers.ComInter } } } + + // AssignOut - Assign to parameters + *__invokeRetValUnmanaged__param = __invokeRetValUnmanaged__out; + *__widths_native__param = __widths_native__out; + *__array_native__param = __array_native__out; } catch (System.Exception __exception) { __retVal = System.Runtime.InteropServices.Marshalling.ExceptionAsHResultMarshaller.ConvertToUnmanaged(__exception); + // CleanupFailure - Perform required cleanup. + global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.Free(__widths_native__out); + { + System.ReadOnlySpan __array_native__nativeSpan = global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.GetUnmanagedValuesSource(__array_native__out, __array_native__numElements); + for (int __i0 = 0; __i0 < __array_native__nativeSpan.Length; ++__i0) + { + int __array_native__nativeSpan____i0__numElements; + System.Runtime.CompilerServices.Unsafe.SkipInit(out __array_native__nativeSpan____i0__numElements); + __array_native__nativeSpan____i0__numElements = 0; + { + System.ReadOnlySpan __array_native__nativeSpan____i0__nativeSpan = global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.GetUnmanagedValuesSource((nint*)__array_native__nativeSpan[__i0], __array_native__nativeSpan____i0__numElements); + for (int __i1 = 0; __i1 < __array_native__nativeSpan____i0__nativeSpan.Length; ++__i1) + { + global::SharedTypes.ComInterfaces.MarshallingFails.ThrowOn4thElementMarshalled.Free(__array_native__nativeSpan____i0__nativeSpan[__i1]); + } + } + + global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.Free((nint*)__array_native__nativeSpan[__i0]); + } + } + + global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.Free(__array_native__out); + return __retVal; } return __retVal; @@ -413,6 +464,7 @@ internal static int ABI_Set(System.Runtime.InteropServices.ComWrappers.ComInterf catch (System.Exception __exception) { __retVal = System.Runtime.InteropServices.Marshalling.ExceptionAsHResultMarshaller.ConvertToUnmanaged(__exception); + return __retVal; } return __retVal; diff --git a/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.ComInterfaceGenerator/SharedTypes.ComInterfaces.MarshallingFails.IStringArrayMarshallingFails.cs b/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.ComInterfaceGenerator/SharedTypes.ComInterfaces.MarshallingFails.IStringArrayMarshallingFails.cs index 71cc4e9..c054851 100644 --- a/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.ComInterfaceGenerator/SharedTypes.ComInterfaces.MarshallingFails.IStringArrayMarshallingFails.cs +++ b/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.ComInterfaceGenerator/SharedTypes.ComInterfaces.MarshallingFails.IStringArrayMarshallingFails.cs @@ -147,7 +147,6 @@ { // Cleanup - Perform required cleanup. _ = __value_native__lastIndexMarshalled; - __value_native__numElements = 10; global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.Free(__value_native); } } @@ -186,7 +185,6 @@ finally { // Cleanup - Perform required cleanup. - __value_native__numElements = 10; global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.Free(__value_native); } } @@ -323,7 +321,6 @@ finally { // Cleanup - Perform required cleanup. - __retVal_native__numElements = 10; global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.Free(__retVal_native); } @@ -363,6 +360,7 @@ internal static int ABI_Param(System.Runtime.InteropServices.ComWrappers.ComInte catch (System.Exception __exception) { __retVal = System.Runtime.InteropServices.Marshalling.ExceptionAsHResultMarshaller.ConvertToUnmanaged(__exception); + return __retVal; } return __retVal; @@ -372,7 +370,7 @@ internal static int ABI_Param(System.Runtime.InteropServices.ComWrappers.ComInte internal static int ABI_InParam(System.Runtime.InteropServices.ComWrappers.ComInterfaceDispatch* __this_native, nint** __value_native__param) { global::SharedTypes.ComInterfaces.MarshallingFails.IStringArrayMarshallingFails @this = default; - ref nint* __value_native = ref *__value_native__param; + nint* __value_native = *__value_native__param; string[] value = default; int __retVal = default; // Setup - Perform required setup. @@ -399,6 +397,7 @@ internal static int ABI_InParam(System.Runtime.InteropServices.ComWrappers.ComIn catch (System.Exception __exception) { __retVal = System.Runtime.InteropServices.Marshalling.ExceptionAsHResultMarshaller.ConvertToUnmanaged(__exception); + return __retVal; } return __retVal; @@ -408,14 +407,13 @@ internal static int ABI_InParam(System.Runtime.InteropServices.ComWrappers.ComIn internal static int ABI_RefParam(System.Runtime.InteropServices.ComWrappers.ComInterfaceDispatch* __this_native, nint** __value_native__param) { global::SharedTypes.ComInterfaces.MarshallingFails.IStringArrayMarshallingFails @this = default; - ref nint* __value_native = ref *__value_native__param; + nint* __value_native__out = default; + nint* __value_native = *__value_native__param; string[] value = default; int __retVal = default; // Setup - Perform required setup. int __value_native__numElements; System.Runtime.CompilerServices.Unsafe.SkipInit(out __value_native__numElements); - bool __value_native__ownOriginal = false; - nint* __value_native__original = __value_native; int __value_native__lastIndexMarshalled = 0; try { @@ -435,30 +433,29 @@ internal static int ABI_RefParam(System.Runtime.InteropServices.ComWrappers.ComI @this = System.Runtime.InteropServices.ComWrappers.ComInterfaceDispatch.GetInstance(__this_native); @this.RefParam(ref value); // Marshal - Convert managed data to native data. - __value_native = global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.AllocateContainerForUnmanagedElements(value, out __value_native__numElements); - __value_native__ownOriginal = true; + __value_native__out = global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.AllocateContainerForUnmanagedElements(value, out __value_native__numElements); { System.ReadOnlySpan __value_native__managedSpan = global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.GetManagedValuesSource(value); - System.Span __value_native__nativeSpan = global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.GetUnmanagedValuesDestination(__value_native, __value_native__numElements); + System.Span __value_native__nativeSpan = global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.GetUnmanagedValuesDestination(__value_native__out, __value_native__numElements); for (int __i0 = 0; __i0 < __value_native__managedSpan.Length; ++__i0, ++__value_native__lastIndexMarshalled) { __value_native__nativeSpan[__i0] = global::SharedTypes.ComInterfaces.MarshallingFails.StringMarshallingFails.ConvertToUnmanaged(__value_native__managedSpan[__i0]); } } + + // AssignOut - Assign to parameters + *__value_native__param = __value_native__out; + // Cleanup - Perform required cleanup. + _ = __value_native__lastIndexMarshalled; + global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.Free(__value_native); } catch (System.Exception __exception) { __retVal = System.Runtime.InteropServices.Marshalling.ExceptionAsHResultMarshaller.ConvertToUnmanaged(__exception); - } - finally - { - // Cleanup - Perform required cleanup. - if (__value_native__ownOriginal) - { - _ = __value_native__lastIndexMarshalled; - __value_native__numElements = 10; - global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.Free(__value_native__original); - } + // CleanupFailure - Perform required cleanup. + _ = __value_native__lastIndexMarshalled; + global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.Free(__value_native__out); + return __retVal; } return __retVal; @@ -468,7 +465,7 @@ internal static int ABI_RefParam(System.Runtime.InteropServices.ComWrappers.ComI internal static int ABI_OutParam(System.Runtime.InteropServices.ComWrappers.ComInterfaceDispatch* __this_native, nint** __value_native__param) { global::SharedTypes.ComInterfaces.MarshallingFails.IStringArrayMarshallingFails @this = default; - ref nint* __value_native = ref *__value_native__param; + nint* __value_native__out = default; string[] value = default; int __retVal = default; // Setup - Perform required setup. @@ -481,20 +478,26 @@ internal static int ABI_OutParam(System.Runtime.InteropServices.ComWrappers.ComI @this = System.Runtime.InteropServices.ComWrappers.ComInterfaceDispatch.GetInstance(__this_native); @this.OutParam(out value); // Marshal - Convert managed data to native data. - __value_native = global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.AllocateContainerForUnmanagedElements(value, out __value_native__numElements); + __value_native__out = global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.AllocateContainerForUnmanagedElements(value, out __value_native__numElements); { System.ReadOnlySpan __value_native__managedSpan = global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.GetManagedValuesSource(value); - System.Span __value_native__nativeSpan = global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.GetUnmanagedValuesDestination(__value_native, __value_native__numElements); + System.Span __value_native__nativeSpan = global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.GetUnmanagedValuesDestination(__value_native__out, __value_native__numElements); __value_native__nativeSpan.Clear(); for (int __i0 = 0; __i0 < __value_native__managedSpan.Length; ++__i0) { __value_native__nativeSpan[__i0] = global::SharedTypes.ComInterfaces.MarshallingFails.StringMarshallingFails.ConvertToUnmanaged(__value_native__managedSpan[__i0]); } } + + // AssignOut - Assign to parameters + *__value_native__param = __value_native__out; } catch (System.Exception __exception) { __retVal = System.Runtime.InteropServices.Marshalling.ExceptionAsHResultMarshaller.ConvertToUnmanaged(__exception); + // CleanupFailure - Perform required cleanup. + global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.Free(__value_native__out); + return __retVal; } return __retVal; @@ -509,17 +512,21 @@ internal static int ABI_ByValueOutParam(System.Runtime.InteropServices.ComWrappe // Setup - Perform required setup. int __value_native__numElements; System.Runtime.CompilerServices.Unsafe.SkipInit(out __value_native__numElements); + scoped System.Span __value_native__out = default; try { // Unmarshal - Convert native data to managed data. __retVal = 0; // S_OK global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.GetManagedValuesDestination(value).Clear(); + __value_native__numElements = 10; + value = global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.AllocateContainerForManagedElements(__value_native, __value_native__numElements); @this = System.Runtime.InteropServices.ComWrappers.ComInterfaceDispatch.GetInstance(__this_native); @this.ByValueOutParam(value); // Marshal - Convert managed data to native data. { __value_native__numElements = global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.GetManagedValuesDestination(value).Length; - System.Span __value_native__nativeSpan = System.Runtime.InteropServices.MemoryMarshal.CreateSpan(ref System.Runtime.CompilerServices.Unsafe.AsRef(in global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.GetUnmanagedValuesSource(__value_native, __value_native__numElements).GetPinnableReference()), __value_native__numElements); + __value_native__out = stackalloc nint[__value_native__numElements]; + System.Span __value_native__nativeSpan = __value_native__out; System.Span __value_native__managedSpan = global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.GetManagedValuesDestination(value); for (int __i0 = 0; __i0 < __value_native__numElements; ++__i0) { @@ -527,10 +534,16 @@ internal static int ABI_ByValueOutParam(System.Runtime.InteropServices.ComWrappe __value_native__nativeSpan[__i0] = global::SharedTypes.ComInterfaces.MarshallingFails.StringMarshallingFails.ConvertToUnmanaged(__value_native__managedSpan[__i0]); } } + + // AssignOut - Assign to parameters + __value_native__out.CopyTo(System.Runtime.InteropServices.MemoryMarshal.CreateSpan(ref System.Runtime.CompilerServices.Unsafe.AsRef(in global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.GetUnmanagedValuesSource(__value_native, __value_native__numElements).GetPinnableReference()), __value_native__numElements)); } catch (System.Exception __exception) { __retVal = System.Runtime.InteropServices.Marshalling.ExceptionAsHResultMarshaller.ConvertToUnmanaged(__exception); + // CleanupFailure - Perform required cleanup. + __value_native__numElements = 10; + return __retVal; } return __retVal; @@ -545,6 +558,7 @@ internal static int ABI_ByValueInOutParam(System.Runtime.InteropServices.ComWrap // Setup - Perform required setup. int __value_native__numElements; System.Runtime.CompilerServices.Unsafe.SkipInit(out __value_native__numElements); + scoped System.Span __value_native__out = default; try { // Unmarshal - Convert native data to managed data. @@ -565,7 +579,8 @@ internal static int ABI_ByValueInOutParam(System.Runtime.InteropServices.ComWrap // Marshal - Convert managed data to native data. { __value_native__numElements = global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.GetManagedValuesDestination(value).Length; - System.Span __value_native__nativeSpan = System.Runtime.InteropServices.MemoryMarshal.CreateSpan(ref System.Runtime.CompilerServices.Unsafe.AsRef(in global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.GetUnmanagedValuesSource(__value_native, __value_native__numElements).GetPinnableReference()), __value_native__numElements); + __value_native__out = stackalloc nint[__value_native__numElements]; + System.Span __value_native__nativeSpan = __value_native__out; System.Span __value_native__managedSpan = global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.GetManagedValuesDestination(value); for (int __i0 = 0; __i0 < __value_native__numElements; ++__i0) { @@ -573,10 +588,18 @@ internal static int ABI_ByValueInOutParam(System.Runtime.InteropServices.ComWrap __value_native__nativeSpan[__i0] = global::SharedTypes.ComInterfaces.MarshallingFails.StringMarshallingFails.ConvertToUnmanaged(__value_native__managedSpan[__i0]); } } + + // AssignOut - Assign to parameters + __value_native__out.CopyTo(System.Runtime.InteropServices.MemoryMarshal.CreateSpan(ref System.Runtime.CompilerServices.Unsafe.AsRef(in global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.GetUnmanagedValuesSource(__value_native, __value_native__numElements).GetPinnableReference()), __value_native__numElements)); + // Cleanup - Perform required cleanup. + __value_native__numElements = 10; } catch (System.Exception __exception) { __retVal = System.Runtime.InteropServices.Marshalling.ExceptionAsHResultMarshaller.ConvertToUnmanaged(__exception); + // CleanupFailure - Perform required cleanup. + __value_native__numElements = 10; + return __retVal; } return __retVal; @@ -586,7 +609,7 @@ internal static int ABI_ByValueInOutParam(System.Runtime.InteropServices.ComWrap internal static int ABI_ReturnValue(System.Runtime.InteropServices.ComWrappers.ComInterfaceDispatch* __this_native, nint** __invokeRetValUnmanaged__param) { global::SharedTypes.ComInterfaces.MarshallingFails.IStringArrayMarshallingFails @this = default; - ref nint* __invokeRetValUnmanaged = ref *__invokeRetValUnmanaged__param; + nint* __invokeRetValUnmanaged__out = default; string[] __invokeRetVal = default; int __retVal = default; // Setup - Perform required setup. @@ -599,20 +622,26 @@ internal static int ABI_ReturnValue(System.Runtime.InteropServices.ComWrappers.C @this = System.Runtime.InteropServices.ComWrappers.ComInterfaceDispatch.GetInstance(__this_native); __invokeRetVal = @this.ReturnValue(); // Marshal - Convert managed data to native data. - __invokeRetValUnmanaged = global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.AllocateContainerForUnmanagedElements(__invokeRetVal, out __invokeRetValUnmanaged__numElements); + __invokeRetValUnmanaged__out = global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.AllocateContainerForUnmanagedElements(__invokeRetVal, out __invokeRetValUnmanaged__numElements); { System.ReadOnlySpan __invokeRetValUnmanaged__managedSpan = global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.GetManagedValuesSource(__invokeRetVal); - System.Span __invokeRetValUnmanaged__nativeSpan = global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.GetUnmanagedValuesDestination(__invokeRetValUnmanaged, __invokeRetValUnmanaged__numElements); + System.Span __invokeRetValUnmanaged__nativeSpan = global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.GetUnmanagedValuesDestination(__invokeRetValUnmanaged__out, __invokeRetValUnmanaged__numElements); __invokeRetValUnmanaged__nativeSpan.Clear(); for (int __i0 = 0; __i0 < __invokeRetValUnmanaged__managedSpan.Length; ++__i0) { __invokeRetValUnmanaged__nativeSpan[__i0] = global::SharedTypes.ComInterfaces.MarshallingFails.StringMarshallingFails.ConvertToUnmanaged(__invokeRetValUnmanaged__managedSpan[__i0]); } } + + // AssignOut - Assign to parameters + *__invokeRetValUnmanaged__param = __invokeRetValUnmanaged__out; } catch (System.Exception __exception) { __retVal = System.Runtime.InteropServices.Marshalling.ExceptionAsHResultMarshaller.ConvertToUnmanaged(__exception); + // CleanupFailure - Perform required cleanup. + global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.Free(__invokeRetValUnmanaged__out); + return __retVal; } return __retVal; diff --git a/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.VtableIndexStubGenerator/ManagedToNativeStubs.g.cs b/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.VtableIndexStubGenerator/ManagedToNativeStubs.g.cs index cd107bd..8e86040 100644 --- a/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.VtableIndexStubGenerator/ManagedToNativeStubs.g.cs +++ b/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.VtableIndexStubGenerator/ManagedToNativeStubs.g.cs @@ -151,7 +151,6 @@ internal unsafe partial interface Native finally { // Cleanup - Perform required cleanup. - __values_native__numElements = numValues; global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.Free(__values_native); } } @@ -493,7 +492,6 @@ internal unsafe partial interface Native } } - __values_native__numElements = numValues; global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.Free(__values_native); } } diff --git a/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.VtableIndexStubGenerator/NativeToManagedStubs.g.cs b/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.VtableIndexStubGenerator/NativeToManagedStubs.g.cs index 17b9691..ba9a1ef 100644 --- a/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.VtableIndexStubGenerator/NativeToManagedStubs.g.cs +++ b/ComInterfaceGenerator.Tests/Microsoft.Interop.ComInterfaceGenerator/Microsoft.Interop.VtableIndexStubGenerator/NativeToManagedStubs.g.cs @@ -12,11 +12,19 @@ internal unsafe partial interface Native [System.Runtime.InteropServices.UnmanagedCallersOnlyAttribute] internal static int ABI_GetData(void* __this_native) { - global::ComInterfaceGenerator.Tests.NativeExportsNE.ImplicitThis.INativeObject @this; + global::ComInterfaceGenerator.Tests.NativeExportsNE.ImplicitThis.INativeObject @this = default; int __retVal = default; - // Unmarshal - Convert native data to managed data. - @this = (global::ComInterfaceGenerator.Tests.NativeExportsNE.ImplicitThis.INativeObject)System.Runtime.InteropServices.Marshalling.UnmanagedObjectUnwrapper.GetObjectForUnmanagedWrapper>(__this_native); - __retVal = @this.GetData(); + try + { + // Unmarshal - Convert native data to managed data. + @this = (global::ComInterfaceGenerator.Tests.NativeExportsNE.ImplicitThis.INativeObject)System.Runtime.InteropServices.Marshalling.UnmanagedObjectUnwrapper.GetObjectForUnmanagedWrapper>(__this_native); + __retVal = @this.GetData(); + } + catch (System.Exception) + { + return __retVal; + } + return __retVal; } } @@ -37,10 +45,17 @@ internal unsafe partial interface Native [System.Runtime.InteropServices.UnmanagedCallersOnlyAttribute] internal static void ABI_SetData(void* __this_native, int x) { - global::ComInterfaceGenerator.Tests.NativeExportsNE.ImplicitThis.INativeObject @this; - // Unmarshal - Convert native data to managed data. - @this = (global::ComInterfaceGenerator.Tests.NativeExportsNE.ImplicitThis.INativeObject)System.Runtime.InteropServices.Marshalling.UnmanagedObjectUnwrapper.GetObjectForUnmanagedWrapper>(__this_native); - @this.SetData(x); + global::ComInterfaceGenerator.Tests.NativeExportsNE.ImplicitThis.INativeObject @this = default; + try + { + // Unmarshal - Convert native data to managed data. + @this = (global::ComInterfaceGenerator.Tests.NativeExportsNE.ImplicitThis.INativeObject)System.Runtime.InteropServices.Marshalling.UnmanagedObjectUnwrapper.GetObjectForUnmanagedWrapper>(__this_native); + @this.SetData(x); + } + catch (System.Exception) + { + return; + } } } } @@ -60,15 +75,25 @@ internal unsafe partial interface Native [System.Runtime.InteropServices.UnmanagedCallersOnlyAttribute] internal static void ABI_ExchangeData(void* __this_native, int* __x_native__param) { - global::ComInterfaceGenerator.Tests.NativeExportsNE.ImplicitThis.INativeObject @this; - ref int __x_native = ref *__x_native__param; - int x; - // Unmarshal - Convert native data to managed data. - x = __x_native; - @this = (global::ComInterfaceGenerator.Tests.NativeExportsNE.ImplicitThis.INativeObject)System.Runtime.InteropServices.Marshalling.UnmanagedObjectUnwrapper.GetObjectForUnmanagedWrapper>(__this_native); - @this.ExchangeData(ref x); - // Marshal - Convert managed data to native data. - __x_native = x; + global::ComInterfaceGenerator.Tests.NativeExportsNE.ImplicitThis.INativeObject @this = default; + int __x_native__out = default; + int __x_native = *__x_native__param; + int x = default; + try + { + // Unmarshal - Convert native data to managed data. + x = __x_native; + @this = (global::ComInterfaceGenerator.Tests.NativeExportsNE.ImplicitThis.INativeObject)System.Runtime.InteropServices.Marshalling.UnmanagedObjectUnwrapper.GetObjectForUnmanagedWrapper>(__this_native); + @this.ExchangeData(ref x); + // Marshal - Convert managed data to native data. + __x_native__out = x; + // AssignOut - Assign to parameters + *__x_native__param = __x_native__out; + } + catch (System.Exception) + { + return; + } } } } @@ -88,21 +113,30 @@ internal unsafe partial interface Native [System.Runtime.InteropServices.UnmanagedCallersOnlyAttribute] internal static void ABI_SumAndSetData(void* __this_native, int* __values_native, int numValues, int* __oldValue_native__param) { - global::ComInterfaceGenerator.Tests.NativeExportsNE.ImplicitThis.INativeObject @this; - int[] values; - ref int __oldValue_native = ref *__oldValue_native__param; - int oldValue; + global::ComInterfaceGenerator.Tests.NativeExportsNE.ImplicitThis.INativeObject @this = default; + int[] values = default; + int __oldValue_native__out = default; + int oldValue = default; // Setup - Perform required setup. int __values_native__numElements; System.Runtime.CompilerServices.Unsafe.SkipInit(out __values_native__numElements); - // Unmarshal - Convert native data to managed data. - __values_native__numElements = numValues; - values = global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.AllocateContainerForManagedElements(__values_native, __values_native__numElements); - global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.GetUnmanagedValuesSource(__values_native, __values_native__numElements).CopyTo(global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.GetManagedValuesDestination(values)); - @this = (global::ComInterfaceGenerator.Tests.NativeExportsNE.ImplicitThis.INativeObject)System.Runtime.InteropServices.Marshalling.UnmanagedObjectUnwrapper.GetObjectForUnmanagedWrapper>(__this_native); - @this.SumAndSetData(values, numValues, out oldValue); - // Marshal - Convert managed data to native data. - __oldValue_native = oldValue; + try + { + // Unmarshal - Convert native data to managed data. + __values_native__numElements = numValues; + values = global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.AllocateContainerForManagedElements(__values_native, __values_native__numElements); + global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.GetUnmanagedValuesSource(__values_native, __values_native__numElements).CopyTo(global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.GetManagedValuesDestination(values)); + @this = (global::ComInterfaceGenerator.Tests.NativeExportsNE.ImplicitThis.INativeObject)System.Runtime.InteropServices.Marshalling.UnmanagedObjectUnwrapper.GetObjectForUnmanagedWrapper>(__this_native); + @this.SumAndSetData(values, numValues, out oldValue); + // Marshal - Convert managed data to native data. + __oldValue_native__out = oldValue; + // AssignOut - Assign to parameters + *__oldValue_native__param = __oldValue_native__out; + } + catch (System.Exception) + { + return; + } } } } @@ -123,15 +157,14 @@ internal unsafe partial interface Native internal static void ABI_SumAndSetData(void* __this_native, int** __values_native__param, int numValues, int* __oldValue_native__param) { global::ComInterfaceGenerator.Tests.NativeExportsNE.ImplicitThis.INativeObject @this = default; - ref int* __values_native = ref *__values_native__param; + int* __values_native__out = default; + int* __values_native = *__values_native__param; int[] values = default; - ref int __oldValue_native = ref *__oldValue_native__param; + int __oldValue_native__out = default; int oldValue = default; // Setup - Perform required setup. int __values_native__numElements; System.Runtime.CompilerServices.Unsafe.SkipInit(out __values_native__numElements); - bool __values_native__ownOriginal = false; - int* __values_native__original = __values_native; try { // Unmarshal - Convert native data to managed data. @@ -141,19 +174,20 @@ internal static void ABI_SumAndSetData(void* __this_native, int** __values_nativ @this = (global::ComInterfaceGenerator.Tests.NativeExportsNE.ImplicitThis.INativeObject)System.Runtime.InteropServices.Marshalling.UnmanagedObjectUnwrapper.GetObjectForUnmanagedWrapper>(__this_native); @this.SumAndSetData(ref values, numValues, out oldValue); // Marshal - Convert managed data to native data. - __oldValue_native = oldValue; - __values_native = global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.AllocateContainerForUnmanagedElements(values, out __values_native__numElements); - __values_native__ownOriginal = true; - global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.GetManagedValuesSource(values).CopyTo(global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.GetUnmanagedValuesDestination(__values_native, __values_native__numElements)); + __oldValue_native__out = oldValue; + __values_native__out = global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.AllocateContainerForUnmanagedElements(values, out __values_native__numElements); + global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.GetManagedValuesSource(values).CopyTo(global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.GetUnmanagedValuesDestination(__values_native__out, __values_native__numElements)); + // AssignOut - Assign to parameters + *__oldValue_native__param = __oldValue_native__out; + *__values_native__param = __values_native__out; + // Cleanup - Perform required cleanup. + global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.Free(__values_native); } - finally + catch (System.Exception) { - // Cleanup - Perform required cleanup. - if (__values_native__ownOriginal) - { - __values_native__numElements = numValues; - global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.Free(__values_native__original); - } + // CleanupFailure - Perform required cleanup. + global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.Free(__values_native__out); + return; } } } @@ -174,17 +208,24 @@ internal unsafe partial interface Native [System.Runtime.InteropServices.UnmanagedCallersOnlyAttribute] internal static void ABI_MultiplyWithData(void* __this_native, int* __values_native, int numValues) { - global::ComInterfaceGenerator.Tests.NativeExportsNE.ImplicitThis.INativeObject @this; - int[] values; + global::ComInterfaceGenerator.Tests.NativeExportsNE.ImplicitThis.INativeObject @this = default; + int[] values = default; // Setup - Perform required setup. int __values_native__numElements; System.Runtime.CompilerServices.Unsafe.SkipInit(out __values_native__numElements); - // Unmarshal - Convert native data to managed data. - __values_native__numElements = numValues; - values = global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.AllocateContainerForManagedElements(__values_native, __values_native__numElements); - global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.GetUnmanagedValuesSource(__values_native, __values_native__numElements).CopyTo(global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.GetManagedValuesDestination(values)); - @this = (global::ComInterfaceGenerator.Tests.NativeExportsNE.ImplicitThis.INativeObject)System.Runtime.InteropServices.Marshalling.UnmanagedObjectUnwrapper.GetObjectForUnmanagedWrapper>(__this_native); - @this.MultiplyWithData(values, numValues); + try + { + // Unmarshal - Convert native data to managed data. + __values_native__numElements = numValues; + values = global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.AllocateContainerForManagedElements(__values_native, __values_native__numElements); + global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.GetUnmanagedValuesSource(__values_native, __values_native__numElements).CopyTo(global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.GetManagedValuesDestination(values)); + @this = (global::ComInterfaceGenerator.Tests.NativeExportsNE.ImplicitThis.INativeObject)System.Runtime.InteropServices.Marshalling.UnmanagedObjectUnwrapper.GetObjectForUnmanagedWrapper>(__this_native); + @this.MultiplyWithData(values, numValues); + } + catch (System.Exception) + { + return; + } } } } @@ -204,14 +245,22 @@ internal unsafe partial interface Native [System.Runtime.InteropServices.UnmanagedCallersOnlyAttribute] internal static int ABI_GetData(void* __this_native) { - global::ComInterfaceGenerator.Tests.NativeExportsNE.UnmanagedToManagedCustomMarshalling.INativeObject @this; - global::SharedTypes.IntWrapper __retVal; + global::ComInterfaceGenerator.Tests.NativeExportsNE.UnmanagedToManagedCustomMarshalling.INativeObject @this = default; + global::SharedTypes.IntWrapper __retVal = default; int __retVal_native = default; - // Unmarshal - Convert native data to managed data. - @this = (global::ComInterfaceGenerator.Tests.NativeExportsNE.UnmanagedToManagedCustomMarshalling.INativeObject)System.Runtime.InteropServices.Marshalling.UnmanagedObjectUnwrapper.GetObjectForUnmanagedWrapper>(__this_native); - __retVal = @this.GetData(); - // Marshal - Convert managed data to native data. - __retVal_native = global::ComInterfaceGenerator.Tests.UnmanagedToManagedCustomMarshallingTests.IntWrapperMarshallerToIntWithFreeCounts.ConvertToUnmanaged(__retVal); + try + { + // Unmarshal - Convert native data to managed data. + @this = (global::ComInterfaceGenerator.Tests.NativeExportsNE.UnmanagedToManagedCustomMarshalling.INativeObject)System.Runtime.InteropServices.Marshalling.UnmanagedObjectUnwrapper.GetObjectForUnmanagedWrapper>(__this_native); + __retVal = @this.GetData(); + // Marshal - Convert managed data to native data. + __retVal_native = global::ComInterfaceGenerator.Tests.UnmanagedToManagedCustomMarshallingTests.IntWrapperMarshallerToIntWithFreeCounts.ConvertToUnmanaged(__retVal); + } + catch (System.Exception) + { + return __retVal_native; + } + return __retVal_native; } } @@ -232,12 +281,19 @@ internal unsafe partial interface Native [System.Runtime.InteropServices.UnmanagedCallersOnlyAttribute] internal static void ABI_SetData(void* __this_native, int __x_native) { - global::ComInterfaceGenerator.Tests.NativeExportsNE.UnmanagedToManagedCustomMarshalling.INativeObject @this; - global::SharedTypes.IntWrapper x; - // Unmarshal - Convert native data to managed data. - x = global::ComInterfaceGenerator.Tests.UnmanagedToManagedCustomMarshallingTests.IntWrapperMarshallerToIntWithFreeCounts.ConvertToManaged(__x_native); - @this = (global::ComInterfaceGenerator.Tests.NativeExportsNE.UnmanagedToManagedCustomMarshalling.INativeObject)System.Runtime.InteropServices.Marshalling.UnmanagedObjectUnwrapper.GetObjectForUnmanagedWrapper>(__this_native); - @this.SetData(x); + global::ComInterfaceGenerator.Tests.NativeExportsNE.UnmanagedToManagedCustomMarshalling.INativeObject @this = default; + global::SharedTypes.IntWrapper x = default; + try + { + // Unmarshal - Convert native data to managed data. + x = global::ComInterfaceGenerator.Tests.UnmanagedToManagedCustomMarshallingTests.IntWrapperMarshallerToIntWithFreeCounts.ConvertToManaged(__x_native); + @this = (global::ComInterfaceGenerator.Tests.NativeExportsNE.UnmanagedToManagedCustomMarshalling.INativeObject)System.Runtime.InteropServices.Marshalling.UnmanagedObjectUnwrapper.GetObjectForUnmanagedWrapper>(__this_native); + @this.SetData(x); + } + catch (System.Exception) + { + return; + } } } } @@ -258,11 +314,9 @@ internal unsafe partial interface Native internal static void ABI_ExchangeData(void* __this_native, int* __data_native__param) { global::ComInterfaceGenerator.Tests.NativeExportsNE.UnmanagedToManagedCustomMarshalling.INativeObject @this = default; - ref int __data_native = ref *__data_native__param; + int __data_native__out = default; + int __data_native = *__data_native__param; global::SharedTypes.IntWrapper data = default; - // Setup - Perform required setup. - bool __data_native__ownOriginal = false; - int __data_native__original = __data_native; try { // Unmarshal - Convert native data to managed data. @@ -270,16 +324,17 @@ internal static void ABI_ExchangeData(void* __this_native, int* __data_native__p @this = (global::ComInterfaceGenerator.Tests.NativeExportsNE.UnmanagedToManagedCustomMarshalling.INativeObject)System.Runtime.InteropServices.Marshalling.UnmanagedObjectUnwrapper.GetObjectForUnmanagedWrapper>(__this_native); @this.ExchangeData(ref data); // Marshal - Convert managed data to native data. - __data_native = global::ComInterfaceGenerator.Tests.UnmanagedToManagedCustomMarshallingTests.IntWrapperMarshallerToIntWithFreeCounts.ConvertToUnmanaged(data); - __data_native__ownOriginal = true; + __data_native__out = global::ComInterfaceGenerator.Tests.UnmanagedToManagedCustomMarshallingTests.IntWrapperMarshallerToIntWithFreeCounts.ConvertToUnmanaged(data); + // AssignOut - Assign to parameters + *__data_native__param = __data_native__out; + // Cleanup - Perform required cleanup. + global::ComInterfaceGenerator.Tests.UnmanagedToManagedCustomMarshallingTests.IntWrapperMarshallerToIntWithFreeCounts.Free(__data_native); } - finally + catch (System.Exception) { - // Cleanup - Perform required cleanup. - if (__data_native__ownOriginal) - { - global::ComInterfaceGenerator.Tests.UnmanagedToManagedCustomMarshallingTests.IntWrapperMarshallerToIntWithFreeCounts.Free(__data_native__original); - } + // CleanupFailure - Perform required cleanup. + global::ComInterfaceGenerator.Tests.UnmanagedToManagedCustomMarshallingTests.IntWrapperMarshallerToIntWithFreeCounts.Free(__data_native__out); + return; } } } @@ -300,29 +355,40 @@ internal unsafe partial interface Native [System.Runtime.InteropServices.UnmanagedCallersOnlyAttribute] internal static void ABI_SumAndSetData(void* __this_native, int* __values_native, int numValues, int* __oldValue_native__param) { - global::ComInterfaceGenerator.Tests.NativeExportsNE.UnmanagedToManagedCustomMarshalling.INativeObject @this; - global::SharedTypes.IntWrapper[] values; - ref int __oldValue_native = ref *__oldValue_native__param; - global::SharedTypes.IntWrapper oldValue; + global::ComInterfaceGenerator.Tests.NativeExportsNE.UnmanagedToManagedCustomMarshalling.INativeObject @this = default; + global::SharedTypes.IntWrapper[] values = default; + int __oldValue_native__out = default; + global::SharedTypes.IntWrapper oldValue = default; // Setup - Perform required setup. int __values_native__numElements; System.Runtime.CompilerServices.Unsafe.SkipInit(out __values_native__numElements); - // Unmarshal - Convert native data to managed data. - __values_native__numElements = numValues; - values = global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.AllocateContainerForManagedElements(__values_native, __values_native__numElements); + try { - System.ReadOnlySpan __values_native__nativeSpan = global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.GetUnmanagedValuesSource(__values_native, __values_native__numElements); - System.Span __values_native__managedSpan = global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.GetManagedValuesDestination(values); - for (int __i0 = 0; __i0 < __values_native__numElements; ++__i0) + // Unmarshal - Convert native data to managed data. + __values_native__numElements = numValues; + values = global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.AllocateContainerForManagedElements(__values_native, __values_native__numElements); { - __values_native__managedSpan[__i0] = global::ComInterfaceGenerator.Tests.UnmanagedToManagedCustomMarshallingTests.IntWrapperMarshallerToIntWithFreeCounts.ConvertToManaged(__values_native__nativeSpan[__i0]); + System.ReadOnlySpan __values_native__nativeSpan = global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.GetUnmanagedValuesSource(__values_native, __values_native__numElements); + System.Span __values_native__managedSpan = global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.GetManagedValuesDestination(values); + for (int __i0 = 0; __i0 < __values_native__numElements; ++__i0) + { + __values_native__managedSpan[__i0] = global::ComInterfaceGenerator.Tests.UnmanagedToManagedCustomMarshallingTests.IntWrapperMarshallerToIntWithFreeCounts.ConvertToManaged(__values_native__nativeSpan[__i0]); + } } - } - @this = (global::ComInterfaceGenerator.Tests.NativeExportsNE.UnmanagedToManagedCustomMarshalling.INativeObject)System.Runtime.InteropServices.Marshalling.UnmanagedObjectUnwrapper.GetObjectForUnmanagedWrapper>(__this_native); - @this.SumAndSetData(values, numValues, out oldValue); - // Marshal - Convert managed data to native data. - __oldValue_native = global::ComInterfaceGenerator.Tests.UnmanagedToManagedCustomMarshallingTests.IntWrapperMarshallerToIntWithFreeCounts.ConvertToUnmanaged(oldValue); + @this = (global::ComInterfaceGenerator.Tests.NativeExportsNE.UnmanagedToManagedCustomMarshalling.INativeObject)System.Runtime.InteropServices.Marshalling.UnmanagedObjectUnwrapper.GetObjectForUnmanagedWrapper>(__this_native); + @this.SumAndSetData(values, numValues, out oldValue); + // Marshal - Convert managed data to native data. + __oldValue_native__out = global::ComInterfaceGenerator.Tests.UnmanagedToManagedCustomMarshallingTests.IntWrapperMarshallerToIntWithFreeCounts.ConvertToUnmanaged(oldValue); + // AssignOut - Assign to parameters + *__oldValue_native__param = __oldValue_native__out; + } + catch (System.Exception) + { + // CleanupFailure - Perform required cleanup. + global::ComInterfaceGenerator.Tests.UnmanagedToManagedCustomMarshallingTests.IntWrapperMarshallerToIntWithFreeCounts.Free(__oldValue_native__out); + return; + } } } } @@ -343,15 +409,14 @@ internal unsafe partial interface Native internal static void ABI_SumAndSetData(void* __this_native, int** __values_native__param, int numValues, int* __oldValue_native__param) { global::ComInterfaceGenerator.Tests.NativeExportsNE.UnmanagedToManagedCustomMarshalling.INativeObject @this = default; - ref int* __values_native = ref *__values_native__param; + int* __values_native__out = default; + int* __values_native = *__values_native__param; global::SharedTypes.IntWrapper[] values = default; - ref int __oldValue_native = ref *__oldValue_native__param; + int __oldValue_native__out = default; global::SharedTypes.IntWrapper oldValue = default; // Setup - Perform required setup. int __values_native__numElements; System.Runtime.CompilerServices.Unsafe.SkipInit(out __values_native__numElements); - bool __values_native__ownOriginal = false; - int* __values_native__original = __values_native; int __values_native__lastIndexMarshalled = 0; try { @@ -370,34 +435,45 @@ internal static void ABI_SumAndSetData(void* __this_native, int** __values_nativ @this = (global::ComInterfaceGenerator.Tests.NativeExportsNE.UnmanagedToManagedCustomMarshalling.INativeObject)System.Runtime.InteropServices.Marshalling.UnmanagedObjectUnwrapper.GetObjectForUnmanagedWrapper>(__this_native); @this.SumAndSetData(ref values, numValues, out oldValue); // Marshal - Convert managed data to native data. - __oldValue_native = global::ComInterfaceGenerator.Tests.UnmanagedToManagedCustomMarshallingTests.IntWrapperMarshallerToIntWithFreeCounts.ConvertToUnmanaged(oldValue); - __values_native = global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.AllocateContainerForUnmanagedElements(values, out __values_native__numElements); - __values_native__ownOriginal = true; + __oldValue_native__out = global::ComInterfaceGenerator.Tests.UnmanagedToManagedCustomMarshallingTests.IntWrapperMarshallerToIntWithFreeCounts.ConvertToUnmanaged(oldValue); + __values_native__out = global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.AllocateContainerForUnmanagedElements(values, out __values_native__numElements); { System.ReadOnlySpan __values_native__managedSpan = global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.GetManagedValuesSource(values); - System.Span __values_native__nativeSpan = global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.GetUnmanagedValuesDestination(__values_native, __values_native__numElements); + System.Span __values_native__nativeSpan = global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.GetUnmanagedValuesDestination(__values_native__out, __values_native__numElements); for (int __i0 = 0; __i0 < __values_native__managedSpan.Length; ++__i0, ++__values_native__lastIndexMarshalled) { __values_native__nativeSpan[__i0] = global::ComInterfaceGenerator.Tests.UnmanagedToManagedCustomMarshallingTests.IntWrapperMarshallerToIntWithFreeCounts.ConvertToUnmanaged(__values_native__managedSpan[__i0]); } } - } - finally - { + + // AssignOut - Assign to parameters + *__oldValue_native__param = __oldValue_native__out; + *__values_native__param = __values_native__out; // Cleanup - Perform required cleanup. - if (__values_native__ownOriginal) { + System.ReadOnlySpan __values_native__nativeSpan = global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.GetUnmanagedValuesSource(__values_native, __values_native__numElements); + for (int __i0 = 0; __i0 < __values_native__lastIndexMarshalled; ++__i0) { - System.ReadOnlySpan __values_native__nativeSpan = global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.GetUnmanagedValuesSource(__values_native__original, __values_native__numElements); - for (int __i0 = 0; __i0 < __values_native__lastIndexMarshalled; ++__i0) - { - global::ComInterfaceGenerator.Tests.UnmanagedToManagedCustomMarshallingTests.IntWrapperMarshallerToIntWithFreeCounts.Free(__values_native__nativeSpan[__i0]); - } + global::ComInterfaceGenerator.Tests.UnmanagedToManagedCustomMarshallingTests.IntWrapperMarshallerToIntWithFreeCounts.Free(__values_native__nativeSpan[__i0]); } + } - __values_native__numElements = numValues; - global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.Free(__values_native__original); + global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.Free(__values_native); + } + catch (System.Exception) + { + // CleanupFailure - Perform required cleanup. + global::ComInterfaceGenerator.Tests.UnmanagedToManagedCustomMarshallingTests.IntWrapperMarshallerToIntWithFreeCounts.Free(__oldValue_native__out); + { + System.ReadOnlySpan __values_native__nativeSpan = global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.GetUnmanagedValuesSource(__values_native__out, __values_native__numElements); + for (int __i0 = 0; __i0 < __values_native__lastIndexMarshalled; ++__i0) + { + global::ComInterfaceGenerator.Tests.UnmanagedToManagedCustomMarshallingTests.IntWrapperMarshallerToIntWithFreeCounts.Free(__values_native__nativeSpan[__i0]); + } } + + global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.Free(__values_native__out); + return; } } } @@ -418,36 +494,67 @@ internal unsafe partial interface Native [System.Runtime.InteropServices.UnmanagedCallersOnlyAttribute] internal static void ABI_MultiplyWithData(void* __this_native, int* __values123_native, int numValues) { - global::ComInterfaceGenerator.Tests.NativeExportsNE.UnmanagedToManagedCustomMarshalling.INativeObject @this; - global::SharedTypes.IntWrapper[] values123; + global::ComInterfaceGenerator.Tests.NativeExportsNE.UnmanagedToManagedCustomMarshalling.INativeObject @this = default; + global::SharedTypes.IntWrapper[] values123 = default; // Setup - Perform required setup. int __values123_native__numElements; System.Runtime.CompilerServices.Unsafe.SkipInit(out __values123_native__numElements); - // Unmarshal - Convert native data to managed data. - __values123_native__numElements = numValues; - values123 = global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.AllocateContainerForManagedElements(__values123_native, __values123_native__numElements); + scoped System.Span __values123_native__out = default; + try { - System.ReadOnlySpan __values123_native__nativeSpan = global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.GetUnmanagedValuesSource(__values123_native, __values123_native__numElements); - System.Span __values123_native__managedSpan = global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.GetManagedValuesDestination(values123); - for (int __i0 = 0; __i0 < __values123_native__numElements; ++__i0) + // Unmarshal - Convert native data to managed data. + __values123_native__numElements = numValues; + values123 = global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.AllocateContainerForManagedElements(__values123_native, __values123_native__numElements); { - __values123_native__managedSpan[__i0] = global::ComInterfaceGenerator.Tests.UnmanagedToManagedCustomMarshallingTests.IntWrapperMarshallerToIntWithFreeCounts.ConvertToManaged(__values123_native__nativeSpan[__i0]); + System.ReadOnlySpan __values123_native__nativeSpan = global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.GetUnmanagedValuesSource(__values123_native, __values123_native__numElements); + System.Span __values123_native__managedSpan = global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.GetManagedValuesDestination(values123); + for (int __i0 = 0; __i0 < __values123_native__numElements; ++__i0) + { + __values123_native__managedSpan[__i0] = global::ComInterfaceGenerator.Tests.UnmanagedToManagedCustomMarshallingTests.IntWrapperMarshallerToIntWithFreeCounts.ConvertToManaged(__values123_native__nativeSpan[__i0]); + } + } + + @this = (global::ComInterfaceGenerator.Tests.NativeExportsNE.UnmanagedToManagedCustomMarshalling.INativeObject)System.Runtime.InteropServices.Marshalling.UnmanagedObjectUnwrapper.GetObjectForUnmanagedWrapper>(__this_native); + @this.MultiplyWithData(values123, numValues); + // Marshal - Convert managed data to native data. + { + __values123_native__numElements = global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.GetManagedValuesDestination(values123).Length; + __values123_native__out = stackalloc int[__values123_native__numElements]; + System.Span __values123_native__nativeSpan = __values123_native__out; + System.Span __values123_native__managedSpan = global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.GetManagedValuesDestination(values123); + for (int __i0 = 0; __i0 < __values123_native__numElements; ++__i0) + { + int __values123_native__nativeSpan____i0__original = __values123_native__nativeSpan[__i0]; + __values123_native__nativeSpan[__i0] = global::ComInterfaceGenerator.Tests.UnmanagedToManagedCustomMarshallingTests.IntWrapperMarshallerToIntWithFreeCounts.ConvertToUnmanaged(__values123_native__managedSpan[__i0]); + } } - } - @this = (global::ComInterfaceGenerator.Tests.NativeExportsNE.UnmanagedToManagedCustomMarshalling.INativeObject)System.Runtime.InteropServices.Marshalling.UnmanagedObjectUnwrapper.GetObjectForUnmanagedWrapper>(__this_native); - @this.MultiplyWithData(values123, numValues); - // Marshal - Convert managed data to native data. + // AssignOut - Assign to parameters + __values123_native__out.CopyTo(System.Runtime.InteropServices.MemoryMarshal.CreateSpan(ref System.Runtime.CompilerServices.Unsafe.AsRef(in global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.GetUnmanagedValuesSource(__values123_native, __values123_native__numElements).GetPinnableReference()), __values123_native__numElements)); + // Cleanup - Perform required cleanup. + { + System.ReadOnlySpan __values123_native__nativeSpan = global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.GetUnmanagedValuesSource(__values123_native, __values123_native__numElements); + for (int __i0 = 0; __i0 < __values123_native__nativeSpan.Length; ++__i0) + { + global::ComInterfaceGenerator.Tests.UnmanagedToManagedCustomMarshallingTests.IntWrapperMarshallerToIntWithFreeCounts.Free(__values123_native__nativeSpan[__i0]); + } + } + + __values123_native__numElements = numValues; + } + catch (System.Exception) { - __values123_native__numElements = global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.GetManagedValuesDestination(values123).Length; - System.Span __values123_native__nativeSpan = System.Runtime.InteropServices.MemoryMarshal.CreateSpan(ref System.Runtime.CompilerServices.Unsafe.AsRef(in global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.GetUnmanagedValuesSource(__values123_native, __values123_native__numElements).GetPinnableReference()), __values123_native__numElements); - System.Span __values123_native__managedSpan = global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.GetManagedValuesDestination(values123); - for (int __i0 = 0; __i0 < __values123_native__numElements; ++__i0) + // CleanupFailure - Perform required cleanup. { - int __values123_native__nativeSpan____i0__original = __values123_native__nativeSpan[__i0]; - __values123_native__nativeSpan[__i0] = global::ComInterfaceGenerator.Tests.UnmanagedToManagedCustomMarshallingTests.IntWrapperMarshallerToIntWithFreeCounts.ConvertToUnmanaged(__values123_native__managedSpan[__i0]); - global::ComInterfaceGenerator.Tests.UnmanagedToManagedCustomMarshallingTests.IntWrapperMarshallerToIntWithFreeCounts.Free(__values123_native__nativeSpan____i0__original); + System.ReadOnlySpan __values123_native__nativeSpan = __values123_native__out; + for (int __i0 = 0; __i0 < __values123_native__nativeSpan.Length; ++__i0) + { + global::ComInterfaceGenerator.Tests.UnmanagedToManagedCustomMarshallingTests.IntWrapperMarshallerToIntWithFreeCounts.Free(__values123_native__nativeSpan[__i0]); + } } + + __values123_native__numElements = numValues; + return; } } } @@ -470,7 +577,7 @@ internal static void ABI_SumAndSetData(void* __this_native, int* __values_native { global::ComInterfaceGenerator.Tests.NativeExportsNE.UnmanagedToManagedCustomMarshalling.INativeObjectStateful @this = default; global::SharedTypes.IntWrapper[] values = default; - ref int __oldValue_native = ref *__oldValue_native__param; + int __oldValue_native__out = default; global::SharedTypes.IntWrapper oldValue = default; // Setup - Perform required setup. global::ComInterfaceGenerator.Tests.UnmanagedToManagedCustomMarshallingTests.StatefulUnmanagedToManagedCollectionMarshaller.In __values_native__marshaller = new(); @@ -495,13 +602,18 @@ internal static void ABI_SumAndSetData(void* __this_native, int* __values_native @this = (global::ComInterfaceGenerator.Tests.NativeExportsNE.UnmanagedToManagedCustomMarshalling.INativeObjectStateful)System.Runtime.InteropServices.Marshalling.UnmanagedObjectUnwrapper.GetObjectForUnmanagedWrapper>(__this_native); @this.SumAndSetData(values, numValues, out oldValue); // Marshal - Convert managed data to native data. - __oldValue_native = global::ComInterfaceGenerator.Tests.UnmanagedToManagedCustomMarshallingTests.IntWrapperMarshallerToIntWithFreeCounts.ConvertToUnmanaged(oldValue); - } - finally - { + __oldValue_native__out = global::ComInterfaceGenerator.Tests.UnmanagedToManagedCustomMarshallingTests.IntWrapperMarshallerToIntWithFreeCounts.ConvertToUnmanaged(oldValue); + // AssignOut - Assign to parameters + *__oldValue_native__param = __oldValue_native__out; // Cleanup - Perform required cleanup. __values_native__marshaller.Free(); } + catch (System.Exception) + { + // CleanupFailure - Perform required cleanup. + global::ComInterfaceGenerator.Tests.UnmanagedToManagedCustomMarshallingTests.IntWrapperMarshallerToIntWithFreeCounts.Free(__oldValue_native__out); + return; + } } } } @@ -522,14 +634,13 @@ internal unsafe partial interface Native internal static void ABI_SumAndSetData(void* __this_native, int** __values_native__param, int numValues, int* __oldValue_native__param) { global::ComInterfaceGenerator.Tests.NativeExportsNE.UnmanagedToManagedCustomMarshalling.INativeObjectStateful @this = default; - ref int* __values_native = ref *__values_native__param; + int* __values_native__out = default; + int* __values_native = *__values_native__param; global::SharedTypes.IntWrapper[] values = default; - ref int __oldValue_native = ref *__oldValue_native__param; + int __oldValue_native__out = default; global::SharedTypes.IntWrapper oldValue = default; // Setup - Perform required setup. global::ComInterfaceGenerator.Tests.UnmanagedToManagedCustomMarshallingTests.StatefulUnmanagedToManagedCollectionMarshaller.Ref __values_native__marshaller = new(); - bool __values_native__ownOriginal = false; - int* __values_native__original = __values_native; int __values_native__numElements; int __values_native__lastIndexMarshalled = 0; System.Runtime.CompilerServices.Unsafe.SkipInit(out __values_native__numElements); @@ -551,12 +662,9 @@ internal static void ABI_SumAndSetData(void* __this_native, int** __values_nativ values = __values_native__marshaller.ToManaged(); @this = (global::ComInterfaceGenerator.Tests.NativeExportsNE.UnmanagedToManagedCustomMarshalling.INativeObjectStateful)System.Runtime.InteropServices.Marshalling.UnmanagedObjectUnwrapper.GetObjectForUnmanagedWrapper>(__this_native); @this.SumAndSetData(ref values, numValues, out oldValue); - // PinnedMarshal - Convert managed data to native data that requires the managed data to be pinned. - __values_native = __values_native__marshaller.ToUnmanaged(); // Marshal - Convert managed data to native data. - __oldValue_native = global::ComInterfaceGenerator.Tests.UnmanagedToManagedCustomMarshallingTests.IntWrapperMarshallerToIntWithFreeCounts.ConvertToUnmanaged(oldValue); + __oldValue_native__out = global::ComInterfaceGenerator.Tests.UnmanagedToManagedCustomMarshallingTests.IntWrapperMarshallerToIntWithFreeCounts.ConvertToUnmanaged(oldValue); __values_native__marshaller.FromManaged(values); - __values_native__ownOriginal = true; { System.ReadOnlySpan __values_native__managedSpan = __values_native__marshaller.GetManagedValuesSource(); System.Span __values_native__nativeSpan = __values_native__marshaller.GetUnmanagedValuesDestination(); @@ -565,22 +673,37 @@ internal static void ABI_SumAndSetData(void* __this_native, int** __values_nativ __values_native__nativeSpan[__i0] = global::ComInterfaceGenerator.Tests.UnmanagedToManagedCustomMarshallingTests.IntWrapperMarshallerToIntWithFreeCounts.ConvertToUnmanaged(__values_native__managedSpan[__i0]); } } + + // PinnedMarshal - Convert managed data to native data that requires the managed data to be pinned. + __values_native__out = __values_native__marshaller.ToUnmanaged(); + // AssignOut - Assign to parameters + *__oldValue_native__param = __oldValue_native__out; + *__values_native__param = __values_native__out; + // Cleanup - Perform required cleanup. + { + System.ReadOnlySpan __values_native__nativeSpan = __values_native__marshaller.GetUnmanagedValuesSource(__values_native__numElements); + for (int __i0 = 0; __i0 < __values_native__lastIndexMarshalled; ++__i0) + { + global::ComInterfaceGenerator.Tests.UnmanagedToManagedCustomMarshallingTests.IntWrapperMarshallerToIntWithFreeCounts.Free(__values_native__nativeSpan[__i0]); + } + } + + __values_native__marshaller.Free(); } - finally + catch (System.Exception) { - // Cleanup - Perform required cleanup. - if (__values_native__ownOriginal) + // CleanupFailure - Perform required cleanup. + global::ComInterfaceGenerator.Tests.UnmanagedToManagedCustomMarshallingTests.IntWrapperMarshallerToIntWithFreeCounts.Free(__oldValue_native__out); { + System.ReadOnlySpan __values_native__nativeSpan = __values_native__marshaller.GetUnmanagedValuesSource(__values_native__numElements); + for (int __i0 = 0; __i0 < __values_native__lastIndexMarshalled; ++__i0) { - System.ReadOnlySpan __values_native__nativeSpan = __values_native__marshaller.GetUnmanagedValuesSource(__values_native__numElements); - for (int __i0 = 0; __i0 < __values_native__lastIndexMarshalled; ++__i0) - { - global::ComInterfaceGenerator.Tests.UnmanagedToManagedCustomMarshallingTests.IntWrapperMarshallerToIntWithFreeCounts.Free(__values_native__nativeSpan[__i0]); - } + global::ComInterfaceGenerator.Tests.UnmanagedToManagedCustomMarshallingTests.IntWrapperMarshallerToIntWithFreeCounts.Free(__values_native__nativeSpan[__i0]); } } __values_native__marshaller.Free(); + return; } } } @@ -607,6 +730,7 @@ internal static void ABI_MultiplyWithData(void* __this_native, int* __values123_ global::ComInterfaceGenerator.Tests.UnmanagedToManagedCustomMarshallingTests.StatefulUnmanagedToManagedCollectionMarshaller.In __values123_native__marshaller = new(); int __values123_native__numElements; System.Runtime.CompilerServices.Unsafe.SkipInit(out __values123_native__numElements); + scoped System.Span __values123_native__out = default; try { // UnmarshalCapture - Capture the native data into marshaller instances in case conversion to managed data throws an exception. @@ -628,20 +752,42 @@ internal static void ABI_MultiplyWithData(void* __this_native, int* __values123_ // Marshal - Convert managed data to native data. { __values123_native__numElements = __values123_native__marshaller.GetManagedValuesDestination(__values123_native__numElements).Length; - System.Span __values123_native__nativeSpan = System.Runtime.InteropServices.MemoryMarshal.CreateSpan(ref System.Runtime.CompilerServices.Unsafe.AsRef(in __values123_native__marshaller.GetUnmanagedValuesSource(__values123_native__numElements).GetPinnableReference()), __values123_native__numElements); + __values123_native__out = stackalloc int[__values123_native__numElements]; + System.Span __values123_native__nativeSpan = __values123_native__out; System.Span __values123_native__managedSpan = __values123_native__marshaller.GetManagedValuesDestination(__values123_native__numElements); for (int __i0 = 0; __i0 < __values123_native__numElements; ++__i0) { int __values123_native__nativeSpan____i0__original = __values123_native__nativeSpan[__i0]; __values123_native__nativeSpan[__i0] = global::ComInterfaceGenerator.Tests.UnmanagedToManagedCustomMarshallingTests.IntWrapperMarshallerToIntWithFreeCounts.ConvertToUnmanaged(__values123_native__managedSpan[__i0]); - global::ComInterfaceGenerator.Tests.UnmanagedToManagedCustomMarshallingTests.IntWrapperMarshallerToIntWithFreeCounts.Free(__values123_native__nativeSpan____i0__original); } } + + // AssignOut - Assign to parameters + __values123_native__out.CopyTo(System.Runtime.InteropServices.MemoryMarshal.CreateSpan(ref System.Runtime.CompilerServices.Unsafe.AsRef(in __values123_native__marshaller.GetUnmanagedValuesSource(__values123_native__numElements).GetPinnableReference()), __values123_native__numElements)); + // Cleanup - Perform required cleanup. + { + System.ReadOnlySpan __values123_native__nativeSpan = __values123_native__marshaller.GetUnmanagedValuesSource(__values123_native__numElements); + for (int __i0 = 0; __i0 < __values123_native__nativeSpan.Length; ++__i0) + { + global::ComInterfaceGenerator.Tests.UnmanagedToManagedCustomMarshallingTests.IntWrapperMarshallerToIntWithFreeCounts.Free(__values123_native__nativeSpan[__i0]); + } + } + + __values123_native__marshaller.Free(); } - finally + catch (System.Exception) { - // Cleanup - Perform required cleanup. + // CleanupFailure - Perform required cleanup. + { + System.ReadOnlySpan __values123_native__nativeSpan = __values123_native__out; + for (int __i0 = 0; __i0 < __values123_native__nativeSpan.Length; ++__i0) + { + global::ComInterfaceGenerator.Tests.UnmanagedToManagedCustomMarshallingTests.IntWrapperMarshallerToIntWithFreeCounts.Free(__values123_native__nativeSpan[__i0]); + } + } + __values123_native__marshaller.Free(); + return; } } } diff --git a/ComInterfaceGenerator.Tests/Microsoft.Interop.LibraryImportGenerator/Microsoft.Interop.LibraryImportGenerator/LibraryImports.g.cs b/ComInterfaceGenerator.Tests/Microsoft.Interop.LibraryImportGenerator/Microsoft.Interop.LibraryImportGenerator/LibraryImports.g.cs index fd35f4d..d4a8a61 100644 --- a/ComInterfaceGenerator.Tests/Microsoft.Interop.LibraryImportGenerator/Microsoft.Interop.LibraryImportGenerator/LibraryImports.g.cs +++ b/ComInterfaceGenerator.Tests/Microsoft.Interop.LibraryImportGenerator/Microsoft.Interop.LibraryImportGenerator/LibraryImports.g.cs @@ -421,7 +421,6 @@ public static partial int SumAndSetNativeObjectData(void* obj, ref int[] arr, in finally { // Cleanup - Perform required cleanup. - __arr_native__numElements = numValues; global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.Free(__arr_native); } diff --git a/LibraryImportGenerator.Tests/Microsoft.Interop.LibraryImportGenerator/Microsoft.Interop.LibraryImportGenerator/LibraryImports.g.cs b/LibraryImportGenerator.Tests/Microsoft.Interop.LibraryImportGenerator/Microsoft.Interop.LibraryImportGenerator/LibraryImports.g.cs index 93747d9..f9e0690 100644 --- a/LibraryImportGenerator.Tests/Microsoft.Interop.LibraryImportGenerator/Microsoft.Interop.LibraryImportGenerator/LibraryImports.g.cs +++ b/LibraryImportGenerator.Tests/Microsoft.Interop.LibraryImportGenerator/Microsoft.Interop.LibraryImportGenerator/LibraryImports.g.cs @@ -123,7 +123,6 @@ public static partial void Duplicate(ref int[] values, int numValues) finally { // Cleanup - Perform required cleanup. - __values_native__numElements = numValues; global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.Free(__values_native); } @@ -166,7 +165,6 @@ public static partial int[] CreateRange(int start, int end, out int numValues) finally { // Cleanup - Perform required cleanup. - __retVal_native__numElements = numValues; global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.Free(__retVal_native); } @@ -210,7 +208,6 @@ public static partial void CreateRange_Out(int start, int end, out int numValues finally { // Cleanup - Perform required cleanup. - __res_native__numElements = numValues; global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.Free(__res_native); } @@ -300,7 +297,6 @@ public static partial void ReverseChars(ref char[] chars, int numElements) finally { // Cleanup - Perform required cleanup. - __chars_native__numElements = numElements; global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.Free(__chars_native); } @@ -429,7 +425,6 @@ public static partial void ReverseStrings_Ref(ref string[] strArray, out int num } } - __strArray_native__numElements = numElements; global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.Free(__strArray_native); } @@ -506,7 +501,6 @@ public static partial string[] ReverseStrings_Return(string[] strArray, out int } } - __retVal_native__numElements = numElements; global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.Free(__retVal_native); { System.ReadOnlySpan __strArray_native__nativeSpan = __strArray_native__marshaller.GetUnmanagedValuesDestination(); @@ -593,7 +587,6 @@ public static partial void ReverseStrings_Out(string[] strArray, out int numElem } } - __res_native__numElements = numElements; global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.Free(__res_native); { System.ReadOnlySpan __strArray_native__nativeSpan = __strArray_native__marshaller.GetUnmanagedValuesDestination(); @@ -642,7 +635,6 @@ public static partial byte[] GetLongBytes(long l) finally { // Cleanup - Perform required cleanup. - __retVal_native__numElements = 8; global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.Free(__retVal_native); } @@ -685,7 +677,6 @@ public static partial void Append(ref int[] values, int numOriginalValues, int n finally { // Cleanup - Perform required cleanup. - __values_native__numElements = checked(1 + numOriginalValues); global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.Free(__values_native); } @@ -976,7 +967,6 @@ public static partial void NegateBools(ref global::SharedTypes.BoolStruct[] bool { // Cleanup - Perform required cleanup. _ = __boolStruct_native__lastIndexMarshalled; - __boolStruct_native__numElements = numValues; global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.Free(__boolStruct_native); } @@ -1043,7 +1033,6 @@ public static partial void NegateBools(global::SharedTypes.BoolStruct[] boolStru finally { // Cleanup - Perform required cleanup. - __pBoolStructOut_native__numElements = numValues; global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.Free(__pBoolStructOut_native); _ = __boolStruct_native__lastIndexMarshalled; __boolStruct_native__marshaller.Free(); @@ -1112,7 +1101,6 @@ public unsafe partial class Arrays finally { // Cleanup - Perform required cleanup. - __retVal_native__numElements = numValues; global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.Free(__retVal_native); _ = __boolStruct_native__lastIndexMarshalled; __boolStruct_native__marshaller.Free(); @@ -1196,12 +1184,10 @@ public static partial int[][] TransposeMatrix(int[][] matrix, int[] numRows, int { int __retVal_native__nativeSpan____i0__numElements; System.Runtime.CompilerServices.Unsafe.SkipInit(out __retVal_native__nativeSpan____i0__numElements); - __retVal_native__nativeSpan____i0__numElements = numRows[__i0]; global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.Free((int*)__retVal_native__nativeSpan[__i0]); } } - __retVal_native__numElements = numColumns; global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.Free(__retVal_native); { System.ReadOnlySpan __matrix_native__nativeSpan = __matrix_native__marshaller.GetUnmanagedValuesDestination(); @@ -1209,7 +1195,6 @@ public static partial int[][] TransposeMatrix(int[][] matrix, int[] numRows, int { int __matrix_native__nativeSpan____i0__numElements; System.Runtime.CompilerServices.Unsafe.SkipInit(out __matrix_native__nativeSpan____i0__numElements); - ; global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.Free((int*)__matrix_native__nativeSpan[__i0]); } } @@ -1324,7 +1309,6 @@ public static unsafe partial void Duplicate(ref int*[] values, int numValues) finally { // Cleanup - Perform required cleanup. - __values_native__numElements = numValues; global::System.Runtime.InteropServices.Marshalling.PointerArrayMarshaller.Free(__values_native); } @@ -1366,7 +1350,6 @@ public unsafe partial class Arrays finally { // Cleanup - Perform required cleanup. - __retVal_native__numElements = numValues; global::System.Runtime.InteropServices.Marshalling.PointerArrayMarshaller.Free(__retVal_native); } @@ -2363,7 +2346,8 @@ public static partial void MarshalMultidimensionalArray_CheckInnerArraysAreClear { global::LibraryImportGenerator.IntegrationTests.EnforceClearedMemoryCleanup.Free(__c_native__nativeSpan____i0__nativeSpan[__i1]); } - }; + } + global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.Free((nint*)__c_native__nativeSpan[__i0]); } } @@ -2433,7 +2417,6 @@ public static partial void MarshalArray_Ref(ref global::SharedTypes.BoolStruct[] } } - __c_native__numElements = 10; global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.Free(__c_native); } @@ -2508,7 +2491,6 @@ public static partial void NegateBoolsOut(global::SharedTypes.BoolStruct[] boolS } } - __pBoolStructOut_native__numElements = numValues; global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.Free(__pBoolStructOut_native); _ = __boolStruct_native__lastIndexMarshalled; __boolStruct_native__marshaller.Free(); @@ -2615,12 +2597,10 @@ public static partial void NegateBoolsOut2D(global::SharedTypes.BoolStruct[][] b } } - __pBoolStructOut_native__nativeSpan____i0__numElements = widths[__i0]; global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.Free((nint*)__pBoolStructOut_native__nativeSpan[__i0]); } } - __pBoolStructOut_native__numElements = length; global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.Free(__pBoolStructOut_native); { System.ReadOnlySpan __boolStruct_native__nativeSpan = __boolStruct_native__marshaller.GetUnmanagedValuesDestination(); @@ -2628,7 +2608,6 @@ public static partial void NegateBoolsOut2D(global::SharedTypes.BoolStruct[][] b { int __boolStruct_native__nativeSpan____i0__numElements; System.Runtime.CompilerServices.Unsafe.SkipInit(out __boolStruct_native__nativeSpan____i0__numElements); - ; global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.Free((global::SharedTypes.BoolStructMarshaller.BoolStructNative*)__boolStruct_native__nativeSpan[__i0]); } } @@ -2779,7 +2758,6 @@ public static partial bool FillRangeArray2D(global::SharedTypes.IntStructWrapper } } - __array_native__nativeSpan____i0__numElements = widths[__i0]; global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.Free((nint*)__array_native__nativeSpan[__i0]); } } @@ -2850,7 +2828,6 @@ public static partial void NegateBoolsRef(ref global::SharedTypes.BoolStruct[] b } } - __boolStruct_native__numElements = numValues; global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.Free(__boolStruct_native); } @@ -2916,7 +2893,6 @@ public static partial void NegateBoolsRef2D_LastElementMarshalling(ref global::S } } - __boolStruct_native__numElements = length; global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.Free(__boolStruct_native); } @@ -3013,12 +2989,10 @@ public static partial void NegateBoolsRef2D_ClearMarshalling(ref global::SharedT } } - __boolStruct_native__nativeSpan____i0__numElements = widths[__i0]; global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.Free((nint*)__boolStruct_native__nativeSpan[__i0]); } } - __boolStruct_native__numElements = length; global::System.Runtime.InteropServices.Marshalling.ArrayMarshaller.Free(__boolStruct_native); } @@ -3058,7 +3032,6 @@ public static partial int Sum(global::System.Collections.Generic.List value finally { // Cleanup - Perform required cleanup. - ; global::SharedTypes.ListMarshaller.Free(__values_native); } @@ -3083,25 +3056,17 @@ public unsafe partial class Stateless [global::System.Runtime.CompilerServices.SkipLocalsInitAttribute] public static partial int SumWithBuffer(global::System.Collections.Generic.List values, int numValues) { - byte* __values_native = default; - int __retVal = default; + byte* __values_native; + int __retVal; // Setup - Perform required setup. int __values_native__numElements; System.Runtime.CompilerServices.Unsafe.SkipInit(out __values_native__numElements); - try - { - // Marshal - Convert managed data to native data. - System.Span __values_native__buffer = stackalloc byte[global::SharedTypes.ListMarshallerWithBuffer.BufferSize]; - __values_native = global::SharedTypes.ListMarshallerWithBuffer.AllocateContainerForUnmanagedElements(values, __values_native__buffer, out __values_native__numElements); - global::SharedTypes.ListMarshallerWithBuffer.GetManagedValuesSource(values).CopyTo(global::SharedTypes.ListMarshallerWithBuffer.GetUnmanagedValuesDestination(__values_native, __values_native__numElements)); - { - __retVal = __PInvoke(__values_native, numValues); - } - } - finally + // Marshal - Convert managed data to native data. + System.Span __values_native__buffer = stackalloc byte[global::SharedTypes.ListMarshallerWithBuffer.BufferSize]; + __values_native = global::SharedTypes.ListMarshallerWithBuffer.AllocateContainerForUnmanagedElements(values, __values_native__buffer, out __values_native__numElements); + global::SharedTypes.ListMarshallerWithBuffer.GetManagedValuesSource(values).CopyTo(global::SharedTypes.ListMarshallerWithBuffer.GetUnmanagedValuesDestination(__values_native, __values_native__numElements)); { - // Cleanup - Perform required cleanup. - ; + __retVal = __PInvoke(__values_native, numValues); } return __retVal; @@ -3157,7 +3122,8 @@ public static unsafe partial int SumWithFreeTracking(global::System.Collections. { global::SharedTypes.IntWrapperMarshallerWithFreeCounts.Free((int*)__values_native__nativeSpan[__i0]); } - }; + } + global::SharedTypes.ListMarshaller.Free(__values_native); } @@ -3227,7 +3193,6 @@ public static partial int SumInArray(in global::System.Collections.Generic.List< finally { // Cleanup - Perform required cleanup. - ; global::SharedTypes.ListMarshaller.Free(__values_native); } @@ -3273,7 +3238,6 @@ public static partial void Duplicate(ref global::System.Collections.Generic.List finally { // Cleanup - Perform required cleanup. - __values_native__numElements = numValues; global::SharedTypes.ListMarshaller.Free(__values_native); } @@ -3319,7 +3283,6 @@ public unsafe partial class Stateless finally { // Cleanup - Perform required cleanup. - __retVal_native__numElements = numValues; global::SharedTypes.ListMarshaller.Free(__retVal_native); } @@ -3366,7 +3329,6 @@ public static partial void CreateRange_Out(int start, int end, out int numValues finally { // Cleanup - Perform required cleanup. - __res_native__numElements = numValues; global::SharedTypes.ListMarshaller.Free(__res_native); } @@ -3409,7 +3371,6 @@ public unsafe partial class Stateless finally { // Cleanup - Perform required cleanup. - __retVal_native__numElements = 8; global::SharedTypes.ListMarshaller.Free(__retVal_native); } @@ -3465,7 +3426,6 @@ public static partial bool AndAllMembers(global::System.Collections.Generic.List { // Cleanup - Perform required cleanup. _ = __pArray_native__lastIndexMarshalled; - ; global::SharedTypes.ListMarshaller.Free(__pArray_native); } @@ -3521,7 +3481,6 @@ public static partial bool AndAllMembersIn(in global::System.Collections.Generic { // Cleanup - Perform required cleanup. _ = __pArray_native__lastIndexMarshalled; - ; global::SharedTypes.ListMarshaller.Free(__pArray_native); } @@ -3584,7 +3543,6 @@ public static partial void NegateBools(ref global::System.Collections.Generic.Li { // Cleanup - Perform required cleanup. _ = __boolStruct_native__lastIndexMarshalled; - __boolStruct_native__numElements = numValues; global::SharedTypes.ListMarshaller.Free(__boolStruct_native); } @@ -3649,10 +3607,8 @@ public static partial void NegateBools(global::System.Collections.Generic.List.Free(__pBoolStructOut_native); _ = __boolStruct_native__lastIndexMarshalled; - ; global::SharedTypes.ListMarshaller.Free(__boolStruct_native); } @@ -3717,10 +3673,8 @@ public unsafe partial class Stateless finally { // Cleanup - Perform required cleanup. - __retVal_native__numElements = numValues; global::SharedTypes.ListMarshaller.Free(__retVal_native); _ = __boolStruct_native__lastIndexMarshalled; - ; global::SharedTypes.ListMarshaller.Free(__boolStruct_native); } @@ -3771,9 +3725,6 @@ public static partial int GuaranteedUnmarshal(out global::System.Collections.Gen __ret_native__numElements = 1; ret = global::LibraryImportGenerator.IntegrationTests.NativeExportsNE.Collections.Stateless.ListGuaranteedUnmarshal.AllocateContainerForManagedElementsFinally(__ret_native, __ret_native__numElements); } - - // Cleanup - Perform required cleanup. - __ret_native__numElements = 1; } return __retVal; @@ -3823,9 +3774,6 @@ public static partial int GuaranteedUnmarshal(out global::System.Collections.Gen __ret_native__numElements = 1; ret = global::LibraryImportGenerator.IntegrationTests.NativeExportsNE.Collections.Stateless.ListGuaranteedUnmarshal.AllocateContainerForManagedElementsFinally(__ret_native, __ret_native__numElements); } - - // Cleanup - Perform required cleanup. - __ret_native__numElements = 1; } return __retVal; @@ -6059,7 +6007,6 @@ public static partial void Duplicate(ref global::System.Span values, int nu finally { // Cleanup - Perform required cleanup. - __values_native__numElements = numValues; global::System.Runtime.InteropServices.Marshalling.SpanMarshaller.Free(__values_native); } @@ -6102,7 +6049,6 @@ public unsafe partial class Span finally { // Cleanup - Perform required cleanup. - __retVal_native__numElements = numValues; global::System.Runtime.InteropServices.Marshalling.SpanMarshaller.Free(__retVal_native); } @@ -6146,7 +6092,6 @@ public static partial void CreateRange_Out(int start, int end, out int numValues finally { // Cleanup - Perform required cleanup. - __res_native__numElements = numValues; global::System.Runtime.InteropServices.Marshalling.SpanMarshaller.Free(__res_native); } @@ -6186,7 +6131,6 @@ public unsafe partial class Span finally { // Cleanup - Perform required cleanup. - __retVal_native__numElements = 8; global::System.Runtime.InteropServices.Marshalling.SpanMarshaller.Free(__retVal_native); }