Skip to content

Commit

Permalink
Specify call convention on function pointers (#173)
Browse files Browse the repository at this point in the history
* Specify call convention on function pointers

* Update example
  • Loading branch information
lithiumtoast authored Jan 8, 2025
1 parent 2a6a549 commit d98fdda
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// <auto-generated>
// This code was generated by the following tool on 2025-01-07 13:23:17 GMT-05:00:
// https://github.com/bottlenoselabs/c2cs (v2025-01-07 13:23:17 GMT-05:00)
// This code was generated by the following tool on 2025-01-07 22:28:42 GMT-05:00:
// https://github.com/bottlenoselabs/c2cs (v2025-01-07 22:28:42 GMT-05:00)
//
// Changes to this file may cause incorrect behavior and will be lost if the code is regenerated.
// </auto-generated>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// <auto-generated>
// This code was generated by the following tool on 2025-01-07 13:23:17 GMT-05:00:
// This code was generated by the following tool on 2025-01-07 22:28:42 GMT-05:00:
// https://github.com/bottlenoselabs/c2cs (v0.0.0.0)
//
// Changes to this file may cause incorrect behavior and will be lost if the code is regenerated.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// <auto-generated>
// This code was generated by the following tool on 2025-01-07 13:23:17 GMT-05:00:
// This code was generated by the following tool on 2025-01-07 22:28:42 GMT-05:00:
// https://github.com/bottlenoselabs/c2cs (v0.0.0.0)
//
// Changes to this file may cause incorrect behavior and will be lost if the code is regenerated.
Expand Down Expand Up @@ -180,9 +180,9 @@ public enum hw_week_day : int
[StructLayout(LayoutKind.Sequential)]
public partial struct hw_callback
{
public delegate* unmanaged<CString, void> Pointer;
public delegate* unmanaged[Cdecl]<CString, void> Pointer;

public hw_callback(delegate* unmanaged<CString, void> pointer)
public hw_callback(delegate* unmanaged[Cdecl]<CString, void> pointer)
{
Pointer = pointer;
}
Expand All @@ -191,9 +191,9 @@ public hw_callback(delegate* unmanaged<CString, void> pointer)
[StructLayout(LayoutKind.Sequential)]
public partial struct FnPtr_CString_Void
{
public delegate* unmanaged<CString, void> Pointer;
public delegate* unmanaged[Cdecl]<CString, void> Pointer;

public FnPtr_CString_Void(delegate* unmanaged<CString, void> pointer)
public FnPtr_CString_Void(delegate* unmanaged[Cdecl]<CString, void> pointer)
{
Pointer = pointer;
}
Expand Down
2 changes: 1 addition & 1 deletion src/cs/examples/helloworld/helloworld-app/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ private static unsafe void Main()

#if NET5_0_OR_GREATER
// NOTE: Function pointers need to use the UnmanagedCallersOnly attribute. See https://learn.microsoft.com/en-us/dotnet/api/system.runtime.interopservices.unmanagedcallersonlyattribute?view=net-9.0
[UnmanagedCallersOnly]
[UnmanagedCallersOnly(CallConvs = [typeof(CallConvCdecl)])]
#endif
private static void Callback(CString param)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

using System.Collections.Immutable;
using System.Text;
using bottlenoselabs.Common.Tools;
using c2ffi.Data;
using c2ffi.Data.Nodes;
using JetBrains.Annotations;
using Microsoft.Extensions.Logging;
Expand Down Expand Up @@ -75,7 +77,19 @@ private static string GetFunctionPointerTypeNameCSharp(
var parameterTypesAndReturnTypeString = string.IsNullOrEmpty(parameterTypesString)
? returnTypeString
: $"{parameterTypesString}, {returnTypeString}";
return $"delegate* unmanaged<{parameterTypesAndReturnTypeString}>";

#pragma warning disable IDE0072
// ReSharper disable once SwitchExpressionHandlesSomeKnownEnumValuesWithExceptionInDefault
var callingConvention = node.CallingConvention switch
#pragma warning restore IDE0072
{
CFunctionCallingConvention.Cdecl => "Cdecl",
CFunctionCallingConvention.FastCall => "Fastcall",
CFunctionCallingConvention.StdCall => "Stdcall",
_ => throw new ToolException($"Unknown calling convention for function pointer '{node.Name}'.")
};

return $"delegate* unmanaged[{callingConvention}] <{parameterTypesAndReturnTypeString}>";
}

private static string GenerateCodeParameters(
Expand Down

0 comments on commit d98fdda

Please sign in to comment.