I have a legacy C# code that has some math logic in it. Re-writing all of that in Dart will be time taking. Is there a way to use the C# library into Flutter app ?
Directly this probably will be difficult but maybe someone has an idea. Maybe by using managed c++ in .Net as a proxy that you could reach via FFI?
I would try to see if there is a way via FFI to access a c# library.
Alternatively I m pretty sure that AI would be able to convert c# code to Dart
Thanks for your quick response. I think FFI is mainly for C libraries and not for C#
I wrote a Medium article on this very topic some time ago, may help you.
You can do this: Is is possible to export functions from a C# DLL like in VS C++? - Stack Overflow
And then use it from dart as if you were using a C library
Honestly, I’d really love to see Dart’s larger FFI strategy also seek to extend to Web Assembly’s emerging component model. That would open up a huge amount of safe and standardised cross language interoperability where the value would grow over time without necessarily needing much in the way of ongoing investment from the team.
Specifically, I was thinking of maybe using the existing C interoperability to wrap something like the Wasmtime runtime (a single C header file) as many other languages have done but that would be a good example of how C# could be supported without needing to do a lot of C# specific work since they already have web assembly modules as a compilation target in that ecosystem.
Note: this isn’t the same as also having Dart be able to compile to Wasm runtimes outside of the browser although I would love to see that as well very much but this seemed like a much smaller and reasonable bit of work to simple be able to embed a WASI runtime to consume those modules.
@shamblett , Thanks a lot for your response. I am trying to execute your example in Windows. I am publishing the library using the command
dotnet publish /p:NativeLib=Shared /p:SelfContained=true -r win-x64 -c debug
and edited Line 9 of Dart file as follows
final lib = DynamicLibrary.open('../bin/debug/net9.0/win-x64/publish/NativeLibrary.dll');
NativeLibrary.dll exists in the proper location , However, I am getting an error when the add function is called in line 12
Erroring code:
Pointer<NativeFunction<Cfunc>> cadd = lib.lookup<NativeFunction<Cfunc>>('add')
Error message
Opening.....
Associating.....
Unhandled exception:
Invalid argument(s): Failed to lookup symbol 'add': The specified procedure could not be found.
(error code: 127)
#0 DynamicLibrary.lookup (dart:ffi-patch/ffi_dynamic_library_patch.dart:33:70)
#1 main (file:///C:/example/NativeLibrary/dart/add.dart:12:45)
#2 _delayEntrypointInvocation.<anonymous closure> (dart:isolate-patch/isolate_patch.dart:297:19)
#3 _RawReceivePort._handleMessage (dart:isolate-patch/isolate_patch.dart:184:12)
What am I doing wrong ?
Not sure, I haven’t tried this on windows myself, however there is an issue raised on the repo by another Windows user about a similar situation. This may help you.