Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Add inline array marshaller sample to our custom marshalling sample. #6064

Merged
merged 5 commits into from
Aug 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ This sample implements and uses custom marshallers for a built-in type and a use

## Prerequisites

- [.NET 7 SDK](https://dotnet.microsoft.com/download) Preview 7 or later
- [.NET 8 SDK](https://dotnet.microsoft.com/download) Preview 7 or later

- C++ compiler
- Windows: `cl.exe`
Expand Down
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,11 +2,12 @@

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net7.0</TargetFramework>
<TargetFramework>net8.0</TargetFramework>
jkoritzinsky marked this conversation as resolved.
Show resolved Hide resolved
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<RootNamespace>CustomMarshalling</RootNamespace>
<AllowUnsafeBlocks>True</AllowUnsafeBlocks>
<LangVersion>Preview</LangVersion>
</PropertyGroup>

<ItemGroup>
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;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"host": "visualstudio"
}
Loading