Skip to content
New issue

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

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

Already on GitHub? # to your account

Native dependencies in Blazor WebAssembly - Issue when try use a callback from C++ #60824

Closed
harveytriana opened this issue Oct 25, 2021 · 5 comments
Labels
arch-wasm WebAssembly architecture area-Interop-mono question Answer questions and provide assistance, not an issue with source code or documentation.

Comments

@harveytriana
Copy link

Excellent and amazing progress in Blazor. I illustrate some experience with this new feature. Also, the purpose is to show that there is an issue when using a C++ callback.

To get into context, I have first written the experiment with a C# net6 console and a C++ library. The same code from this C++ library is then copied into a Blazor WASM project, in a folder and referenced in project file as native reference.

When run in Blazor project then following images show the result. Perfect.

bz-cpp-1

When try to run the code with callback, an exception occurs.

bz-cpp-2

Assertion at /__w/1/s/src/mono/mono/metadata/loader.c:1806, condition '<disabled>' not met

Code details in following repository: BlazorCrank

Thanks in advance

@dotnet-issue-labeler
Copy link

I couldn't figure out the best area label to add to this issue. If you have write-permissions please help me learn by adding exactly one area label.

@dotnet-issue-labeler dotnet-issue-labeler bot added the untriaged New issue has not been triaged by the area owner label Oct 25, 2021
@SingleAccretion SingleAccretion added area-Interop-mono arch-wasm WebAssembly architecture labels Oct 25, 2021
@ghost
Copy link

ghost commented Oct 25, 2021

Tagging subscribers to 'arch-wasm': @lewing
See info in area-owners.md if you want to be subscribed.

Issue Details

Excellent and amazing progress in Blazor. I illustrate some experience with this new feature. Also, the purpose is to show that there is an issue when using a C++ callback.

To get into context, I have first written the experiment with a C# net6 console and a C++ library. The same code from this C++ library is then copied into a Blazor WASM project, in a folder and referenced in project file as native reference.

When run in Blazor project then following images show the result. Perfect.

bz-cpp-1

When try to run the code with callback, an exception occurs.

bz-cpp-2

Assertion at /__w/1/s/src/mono/mono/metadata/loader.c:1806, condition '<disabled>' not met

Code details in following repository: BlazorCrank

Thanks in advance

Author: harveytriana
Assignees: -
Labels:

arch-wasm, untriaged, area-Interop-mono

Milestone: -

@danroth27
Copy link
Member

@lewing @radical

@lewing lewing removed the untriaged New issue has not been triaged by the area owner label Oct 25, 2021
@lambdageek
Copy link
Member

lambdageek commented Oct 27, 2021

@harveytriana To use native code make sure that:

  1. You install the wasm-tools workload
  2. You build with the property WasmBuildNative set to true. (ie: dotnet build and dotnet run with -p:WasmBuildNative=true or set it in your .csproj)
  3. You must label managed methods that are passed to C++ with a [UnmanagedCallersOnly] attribute.
  4. The method marked with the UnmanagedCallersOnly attribute must be static. That means to call an instance method on a razor page you will need to pass a GCHandle for the instance to C++, and then pass it back to native. Or use some other method to identify the instance of the Razor page.
  5. The method marked with [DllImport] must use a C# 9.0 function pointer rather than a delegate type for the callback argument.
  6. But there's currently an issue with using C# function pointer types in [DllImport] methods on WebAssembly. So we have to use IntPtr in the method signature on the managed side instead of delegate *unmanaged<int, void>

Here is a patch for the BlazorCrank project that shows how to make it work.
https://gist.github.com/lambdageek/ebba06a8a5786694e14658d2633cb99f

Result:

Screen Shot 2021-10-27 at 11 03 40

@lambdageek lambdageek added the question Answer questions and provide assistance, not an issue with source code or documentation. label Oct 27, 2021
@harveytriana
Copy link
Author

Excelent. Thanks.

@ghost ghost locked as resolved and limited conversation to collaborators Dec 2, 2021
# for free to subscribe to this conversation on GitHub. Already have an account? #.
Labels
arch-wasm WebAssembly architecture area-Interop-mono question Answer questions and provide assistance, not an issue with source code or documentation.
Projects
None yet
Development

No branches or pull requests

6 participants