Skip to content

Commit

Permalink
Add inline array marshaller sample to our custom marshalling sample.
Browse files Browse the repository at this point in the history
  • Loading branch information
jkoritzinsky committed Aug 14, 2023
1 parent 9e63fe9 commit 8a201e3
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.InteropServices.Marshalling;

namespace CustomMarshalling
{
[NativeMarshalling(typeof(ErrorBufferMarshaller))]
[InlineArray(4)]
public struct ErrorBuffer
{
private ErrorData _data;
}

[CustomMarshaller(typeof(ErrorBuffer), MarshalMode.ManagedToUnmanagedIn, typeof(ErrorBufferMarshaller))]
public static class ErrorBufferMarshaller
{
[InlineArray(4)]
public struct ErrorBufferUnmanaged
{
private ErrorDataMarshaller.ErrorDataUnmanaged _data;
}

public static ErrorBufferUnmanaged ConvertToUnmanaged(ErrorBuffer managed)
{
ErrorBufferUnmanaged unmanaged = new();
for (int i = 0; i < 4; i++)
{
unmanaged[i] = ErrorDataMarshaller.ConvertToUnmanaged(managed[i]);
}
return unmanaged;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,8 @@ internal partial class NativeLib
[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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -79,11 +79,20 @@ static void MarshalErrorData()

// Marshals ErrorData using ErrorDataMarshaller
Console.WriteLine($"--- {nameof(NativeLib.GetErrors)}: uses {nameof(ErrorDataMarshaller)}.{nameof(ErrorDataMarshaller.Out)}");
int[] codes = new int[] { -3, 2, 5 };
int[] codes = new int[] { -3, 2, 5, 12 };
ErrorData[] errors = NativeLib.GetErrors(codes, codes.Length);
foreach (ErrorData error in errors)
{
error.Print();
Console.WriteLine();
}

ErrorBuffer errorBuffer = new();
errors.AsSpan().CopyTo(errorBuffer);
int[] retrievedCodes = new int[4];
NativeLib.GetErrorCodes(errorBuffer, retrievedCodes);
foreach (int code in retrievedCodes)
{
Console.WriteLine($"Code from error data: {code}");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net7.0</TargetFramework>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<RootNamespace>CustomMarshalling</RootNamespace>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,3 +108,14 @@ extern "C" DLL_EXPORT error_data* STDMETHODCALLTYPE GetErrors(int* codes, int le

return ret;
}

struct error_data_buffer
{
error_data data[4];
};

extern "C" DLL_EXPORT void STDMETHODCALLTYPE GetErrorCodes(error_data_buffer errors, int* codes)
{
for (int i = 0; i < 4; ++i)
codes[i] = errors.data[i].code;
}

0 comments on commit 8a201e3

Please sign in to comment.