Skip to content

[WASM] callback fron native code to .Net: Parsing function pointer types in signatures is not supported #56145

Description

@emepetres

I'm trying to do a call from .Net a native function which call backs to .Net. I'm using function pointers as suggested in discord channel, but I'm getting the following error (maybe related to #43791?):

1:7>Target "_GeneratePInvokeTable: (TargetId:229)" in file "C:\dotnet\packs\Microsoft.NET.Runtime.WebAssembly.Sdk\6.0.0-rc.1.21371.1\Sdk\WasmApp.Native.targets" from project "C:\Users\jcarnero\source\dotnet-wasm-sample\src\console\Wasm.Console.Sample.csproj" (target "_WasmBuildNativeCore" depends on it):
                   Added Item(s): _WasmPInvokeModules=libWasmNative
                   Added Item(s): _WasmPInvokeModules=libSystem.Native
                   Added Item(s): _WasmPInvokeModules=libSystem.IO.Compression.Native
                   Added Item(s): _WasmPInvokeModules=libSystem.Globalization.Native
                   Using "PInvokeTableGenerator" task from assembly "C:\dotnet\packs\Microsoft.NET.Runtime.WebAssembly.Sdk\6.0.0-rc.1.21371.1\Sdk\..\tasks\net6.0\WasmAppBuilder.dll".
                   Task "PInvokeTableGenerator" (TaskId:151)
                     Task Parameter:
                         Assemblies=
                             C:\Users\jcarnero\source\dotnet-wasm-sample\src\console\obj\Debug\net6.0\linked\Wasm.Console.Sample.dll
                             C:\Users\jcarnero\source\dotnet-wasm-sample\src\console\obj\Debug\net6.0\linked\System.Console.dll
                             C:\Users\jcarnero\source\dotnet-wasm-sample\src\console\obj\Debug\net6.0\linked\System.Private.Runtime.InteropServices.JavaScript.dll
                             C:\Users\jcarnero\source\dotnet-wasm-sample\src\console\obj\Debug\net6.0\linked\System.Private.Uri.dll
                             C:\Users\jcarnero\source\dotnet-wasm-sample\src\console\obj\Debug\net6.0\linked\System.Runtime.CompilerServices.Unsafe.dll
                             C:\Users\jcarnero\source\dotnet-wasm-sample\src\console\obj\Debug\net6.0\linked\System.Runtime.dll
                             C:\Users\jcarnero\source\dotnet-wasm-sample\src\console\obj\Debug\net6.0\linked\System.Private.CoreLib.dll (TaskId:151)
                     Task Parameter:OutputPath=C:\Users\jcarnero\source\dotnet-wasm-sample\src\console\obj\Debug\net6.0\wasm\pinvoke-table.h (TaskId:151)
                     Task Parameter:
                         Modules=
                             libWasmNative
                             libSystem.Native
                             libSystem.IO.Compression.Native
                             libSystem.Globalization.Native (TaskId:151)
                     Generating pinvoke table to 'C:\Users\jcarnero\source\dotnet-wasm-sample\src\console\obj\Debug\net6.0\wasm\pinvoke-table.h'. (TaskId:151)
13:27:49.489   1:7>C:\dotnet\packs\Microsoft.NET.Runtime.WebAssembly.Sdk\6.0.0-rc.1.21371.1\Sdk\WasmApp.Native.targets(209,5): error MSB4018: The "PInvokeTableGenerator" task failed unexpectedly. [C:\Users\jcarnero\source\dotnet-wasm-sample\src\console\Wasm.Console.Sample.csproj]
C:\dotnet\packs\Microsoft.NET.Runtime.WebAssembly.Sdk\6.0.0-rc.1.21371.1\Sdk\WasmApp.Native.targets(209,5): error MSB4018: System.NotSupportedException: Parsing function pointer types in signatures is not supported. [C:\Users\jcarnero\source\dotnet-wasm-sample\src\console\Wasm.Console.Sample.csproj]
C:\dotnet\packs\Microsoft.NET.Runtime.WebAssembly.Sdk\6.0.0-rc.1.21371.1\Sdk\WasmApp.Native.targets(209,5): error MSB4018:    at System.Reflection.TypeLoading.Ecma.EcmaModule.GetFunctionPointerType(MethodSignature`1 signature) in System.Reflection.MetadataLoadContext.dll:token 0x6000619+0xa [C:\Users\jcarnero\source\dotnet-wasm-sample\src\console\Wasm.Console.Sample.csproj]
C:\dotnet\packs\Microsoft.NET.Runtime.WebAssembly.Sdk\6.0.0-rc.1.21371.1\Sdk\WasmApp.Native.targets(209,5): error MSB4018:    at System.Reflection.Metadata.Ecma335.SignatureDecoder`2.DecodeType(BlobReader& blobReader, Boolean allowTypeSpecifications, Int32 typeCode) in System.Reflection.Metadata.dll:token 0x6000abf+0x100 [C:\Users\jcarnero\source\dotnet-wasm-sample\src\console\Wasm.Console.Sample.csproj]
C:\dotnet\packs\Microsoft.NET.Runtime.WebAssembly.Sdk\6.0.0-rc.1.21371.1\Sdk\WasmApp.Native.targets(209,5): error MSB4018:    at System.Reflection.Metadata.Ecma335.SignatureDecoder`2.DecodeMethodSignature(BlobReader& blobReader) in System.Reflection.Metadata.dll:token 0x6000ac1+0x5a [C:\Users\jcarnero\source\dotnet-wasm-sample\src\console\Wasm.Console.Sample.csproj]
C:\dotnet\packs\Microsoft.NET.Runtime.WebAssembly.Sdk\6.0.0-rc.1.21371.1\Sdk\WasmApp.Native.targets(209,5): error MSB4018:    at System.Reflection.TypeLoading.Ecma.EcmaMethodDecoder.SpecializeMethodSig(IRoMethodBase roMethodBase) in System.Reflection.MetadataLoadContext.dll:token 0x60005e8+0x0 [C:\Users\jcarnero\source\dotnet-wasm-sample\src\console\Wasm.Console.Sample.csproj]
C:\dotnet\packs\Microsoft.NET.Runtime.WebAssembly.Sdk\6.0.0-rc.1.21371.1\Sdk\WasmApp.Native.targets(209,5): error MSB4018:    at System.Reflection.TypeLoading.RoDefinitionMethod`1.ComputeMethodSig() in System.Reflection.MetadataLoadContext.dll:token 0x6000245+0x7 [C:\Users\jcarnero\source\dotnet-wasm-sample\src\console\Wasm.Console.Sample.csproj]
C:\dotnet\packs\Microsoft.NET.Runtime.WebAssembly.Sdk\6.0.0-rc.1.21371.1\Sdk\WasmApp.Native.targets(209,5): error MSB4018:    at System.Reflection.TypeLoading.RoMethod.get_MethodSig() in System.Reflection.MetadataLoadContext.dll:token 0x6000278+0xc [C:\Users\jcarnero\source\dotnet-wasm-sample\src\console\Wasm.Console.Sample.csproj]
C:\dotnet\packs\Microsoft.NET.Runtime.WebAssembly.Sdk\6.0.0-rc.1.21371.1\Sdk\WasmApp.Native.targets(209,5): error MSB4018:    at System.Reflection.TypeLoading.RoMethod.get_ReturnParameter() in System.Reflection.MetadataLoadContext.dll:token 0x6000276+0x0 [C:\Users\jcarnero\source\dotnet-wasm-sample\src\console\Wasm.Console.Sample.csproj]
C:\dotnet\packs\Microsoft.NET.Runtime.WebAssembly.Sdk\6.0.0-rc.1.21371.1\Sdk\WasmApp.Native.targets(209,5): error MSB4018:    at System.Reflection.TypeLoading.RoMethod.get_ReturnType() in System.Reflection.MetadataLoadContext.dll:token 0x600027d+0x0 [C:\Users\jcarnero\source\dotnet-wasm-sample\src\console\Wasm.Console.Sample.csproj]
C:\dotnet\packs\Microsoft.NET.Runtime.WebAssembly.Sdk\6.0.0-rc.1.21371.1\Sdk\WasmApp.Native.targets(209,5): error MSB4018:    at PInvokeTableGenerator.GenPInvokeDecl(PInvoke pinvoke) in WasmAppBuilder.dll:token 0x6000020+0xc9 [C:\Users\jcarnero\source\dotnet-wasm-sample\src\console\Wasm.Console.Sample.csproj]
C:\dotnet\packs\Microsoft.NET.Runtime.WebAssembly.Sdk\6.0.0-rc.1.21371.1\Sdk\WasmApp.Native.targets(209,5): error MSB4018:    at PInvokeTableGenerator.EmitPInvokeTable(StreamWriter w, Dictionary`2 modules, List`1 pinvokes) in WasmAppBuilder.dll:token 0x600001e+0x59 [C:\Users\jcarnero\source\dotnet-wasm-sample\src\console\Wasm.Console.Sample.csproj]
C:\dotnet\packs\Microsoft.NET.Runtime.WebAssembly.Sdk\6.0.0-rc.1.21371.1\Sdk\WasmApp.Native.targets(209,5): error MSB4018:    at PInvokeTableGenerator.GenPInvokeTable(String[] pinvokeModules, String[] assemblies) in WasmAppBuilder.dll:token 0x600001c+0xaf [C:\Users\jcarnero\source\dotnet-wasm-sample\src\console\Wasm.Console.Sample.csproj]
C:\dotnet\packs\Microsoft.NET.Runtime.WebAssembly.Sdk\6.0.0-rc.1.21371.1\Sdk\WasmApp.Native.targets(209,5): error MSB4018:    at PInvokeTableGenerator.Execute() in WasmAppBuilder.dll:token 0x600001b+0x75 [C:\Users\jcarnero\source\dotnet-wasm-sample\src\console\Wasm.Console.Sample.csproj]
C:\dotnet\packs\Microsoft.NET.Runtime.WebAssembly.Sdk\6.0.0-rc.1.21371.1\Sdk\WasmApp.Native.targets(209,5): error MSB4018:    at Microsoft.Build.BackEnd.TaskExecutionHost.Microsoft.Build.BackEnd.ITaskExecutionHost.Execute() in Microsoft.Build.dll:token 0x60015fb+0x3e [C:\Users\jcarnero\source\dotnet-wasm-sample\src\console\Wasm.Console.Sample.csproj]
C:\dotnet\packs\Microsoft.NET.Runtime.WebAssembly.Sdk\6.0.0-rc.1.21371.1\Sdk\WasmApp.Native.targets(209,5): error MSB4018:    at Microsoft.Build.BackEnd.TaskBuilder.ExecuteInstantiatedTask(ITaskExecutionHost taskExecutionHost, TaskLoggingContext taskLoggingContext, TaskHost taskHost, ItemBucket bucket, TaskExecutionMode howToExecuteTask) in Microsoft.Build.dll:token 0x600147a+0x2b5 [C:\Users\jcarnero\source\dotnet-wasm-sample\src\console\Wasm.Console.Sample.csproj]
                   Done executing task "PInvokeTableGenerator" -- FAILED. (TaskId:151)

This are the most relevants parts of the code:

[DllImport(DllName)]
public static extern unsafe void callCbk(delegate*<void> cbk);
typedef void (*callback)(void);

void callCbk(callback cbk)
{
    cbk();
}

And I'm using version

  Microsoft.AspNetCore.App 6.0.0-rc.1.21370.12 [C:\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.NETCore.App 6.0.0-rc.1.21369.14 [C:\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.WindowsDesktop.App 6.0.0-rc.1.21369.7 [C:\dotnet\shared\Microsoft.WindowsDesktop.App]

Activity

  1. ghost added
    untriagedNew issue has not been triaged by the area owner
    on Jul 22, 2021
  2. emepetres commented on Jul 22, 2021

    @emepetres
    Author

    Just for reference, I modified the test to use "old delegates" and MonoPInvokeCallback attribute and it is working,

  3. vargaz commented on Jul 22, 2021

    @vargaz
    Contributor
  4. modified the milestones: 7.0.0, 6.0.0 on Jul 26, 2021
  5. removed
    untriagedNew issue has not been triaged by the area owner
    on Jul 26, 2021
  6. 10 remaining items

  7. emepetres commented on Aug 9, 2022

    @emepetres
    Author
  8. devshgraphicsprogramming commented on Oct 21, 2022

    @devshgraphicsprogramming

    Its a small world of dependencies, this essentially renders SWIG's Cross Language Polymorphism (Directors) feature unusable:
    swig/swig#1262

    Which we really need because C++ interfaces with JS (embind) bindings from which C# inherits is how we're working around:
    dotnet/aspnetcore#44088

  9. removed
    blockedIssue/PR is blocked on something - see comments
    on Feb 17, 2023
  10. maraf commented on Feb 23, 2023

    @maraf
    Member

    Still blocked until we can update System.Reflection.Metadata in the WasmAppBuilder. Once the API is available, the rest of the pipeline works.

  11. added
    blockedIssue/PR is blocked on something - see comments
    on Feb 23, 2023
  12. removed their assignment
    on Mar 8, 2023
  13. modified the milestones: 8.0.0, Future on Jul 12, 2023
  14. maraf commented on Oct 30, 2023

    @maraf
    Member

    An alternative would be to rewrite manage to native generator to use System.Reflection.Metadata API instead of System.Reflection.MetadataLoadContext

  15. reopened this on Jul 20, 2026
  16. lewing commented on Jul 20, 2026

    @lewing
    Member

    This was fixed in .Net 9 I believe.

  17. locked and limited conversation to collaborators on Aug 20, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

  • Status
    No status

Relationships

None yet

Development

No branches or pull requests

Issue actions