WhiteFalcon
WhiteFalcon
CC#
Created by WhiteFalcon on 9/18/2024 in #help
Sharing the custom marshalled type across libraries
I followed the guide thoroughly https://learn.microsoft.com/en-us/dotnet/standard/native-interop/tutorial-custom-marshaller and source code is in here https://github.com/dotnet/samples/blob/main/core/interop/source-generation/custom-marshalling/src/custommarshalling/NativeLib.cs so far it works, but things goes south when i try to use the ErrorData outside of host library code used to test it out:
C#
using CustomMarshalling;
using System.Runtime.InteropServices;
using System.Runtime.InteropServices.Marshalling;

namespace CustomMarshalling2
{
internal partial class NativeLib2
{
private const string LibName = "nativelib2";

[LibraryImport(LibName)]
internal static partial void PrintErrorData(ErrorData errorData);

[LibraryImport(LibName)]
internal static partial ErrorData GetFatalErrorIfNegative(int code);

[LibraryImport(LibName)]
[return: MarshalUsing(CountElementName = "len")]
internal static partial ErrorData[] GetErrors(int[] codes, int len);

[LibraryImport(LibName)]
internal static partial void GetErrorCodes(ErrorBuffer buffer, int[] codes);
}
}
C#
using CustomMarshalling;
using System.Runtime.InteropServices;
using System.Runtime.InteropServices.Marshalling;

namespace CustomMarshalling2
{
internal partial class NativeLib2
{
private const string LibName = "nativelib2";

[LibraryImport(LibName)]
internal static partial void PrintErrorData(ErrorData errorData);

[LibraryImport(LibName)]
internal static partial ErrorData GetFatalErrorIfNegative(int code);

[LibraryImport(LibName)]
[return: MarshalUsing(CountElementName = "len")]
internal static partial ErrorData[] GetErrors(int[] codes, int len);

[LibraryImport(LibName)]
internal static partial void GetErrorCodes(ErrorBuffer buffer, int[] codes);
}
}
PS: unlike the original source code, i've made modification to ErrorData and their marshallers by making everything public just for testing. it generates SYSLIB1051 error error SYSLIB1051: Runtime marshalling must be disabled in this project by applying the 'System.Runtime.CompilerServices.DisableRuntimeMarshallingAttribute' to the assembly to enable marshalling this type. The generated source will not handle marshalling of parameter 'errorData'. (https://learn.microsoft.com/dotnet/fundamentals/syslib-diagnostics/syslib1051)
6 replies