0

Hi I have several code on C# and I want call in on flutter. I add https://github.com/3F/DllExport to my C# project and create .dll. I checked this .dll in other C# project and in works.

My code on C#

    { 
        [DllExport("scanResult", CallingConvention = CallingConvention.Cdecl)]
        public static void scanResult()
        {
            Console.WriteLine("Hello, World!");
        }
   }

I add this .dll to Flutter project:

1 in .yaml add .dll 2 type

final DynamicLibrary nativePointerTestLib = DynamicLibrary.open("assets/Bluetooth.dll");

final void Function() testdllflutter =
nativePointerTestLib
    .lookup<NativeFunction<Void Function()>>("scanResult")
    .asFunction();

and I get error:

======== Exception caught by services library ======================================================
The following assertion was thrown during a platform message callback:
A KeyUpEvent is dispatched, but the state shows that the physical key is not pressed. If this occurs in real application, please report this bug to Flutter. If this occurs in unit tests, please ensure that simulated events follow Flutter's event model as documented in `HardwareKeyboard`. This was the event: KeyUpEvent#31063(physicalKey: PhysicalKeyboardKey#7002c(usbHidUsage: "0x0007002c", debugName: "Space"), logicalKey: LogicalKeyboardKey#00020(keyId: "0x00000020", keyLabel: " ", debugName: "Space"), character: null, timeStamp: 4:40:07.409359)
'package:flutter/src/services/hardware_keyboard.dart':
Failed assertion: line 441 pos 16: '_pressedKeys.containsKey(event.physicalKey)'


Either the assertion indicates an error in the framework itself, or we should provide substantially more information in this error message to help you determine and fix the underlying cause.
In either case, please report this assertion by filing a bug on GitHub:
  https://github.com/flutter/flutter/issues/new?template=2_bug.md

When the exception was thrown, this was the stack: 
#2      HardwareKeyboard._assertEventIsRegular.<anonymous closure> (package:flutter/src/services/hardware_keyboard.dart:441:16)
#3      HardwareKeyboard._assertEventIsRegular (package:flutter/src/services/hardware_keyboard.dart:452:6)
#4      HardwareKeyboard.handleKeyEvent (package:flutter/src/services/hardware_keyboard.dart:544:5)
#5      KeyEventManager.handleRawKeyMessage (package:flutter/src/services/hardware_keyboard.dart:867:35)
#6      BasicMessageChannel.setMessageHandler.<anonymous closure> (package:flutter/src/services/platform_channel.dart:77:49)
#7      BasicMessageChannel.setMessageHandler.<anonymous closure> (package:flutter/src/services/platform_channel.dart:76:47)
#8      _DefaultBinaryMessenger.setMessageHandler.<anonymous closure> (package:flutter/src/services/binding.dart:380:35)
#9      _DefaultBinaryMessenger.setMessageHandler.<anonymous closure> (package:flutter/src/services/binding.dart:377:46)
#10     _invoke2 (dart:ui/hooks.dart:187:13)
#11     _ChannelCallbackRecord.invoke (dart:ui/channel_buffers.dart:42:5)
#12     _Channel._drainStep (dart:ui/channel_buffers.dart:224:31)
(elided 12 frames from class _AssertionError and dart:async)
============================================================

Can you hepl me. What I do wrong?

Nikolay
  • 344
  • 1
  • 15
  • Have a look at this question to see how to export the function more expressively - name and calling convention https://stackoverflow.com/questions/4428267/calling-c-sharp-from-c. Use `dumpbin /exports` to confirm that the export is visible. – Richard Heap Aug 27 '22 at 19:35
  • @RichardHeap thank you, `dumpbin /exports` looks that all `.dll` is good – Nikolay Aug 29 '22 at 00:04
  • The updated question shows an error that has nothing to do with ffi. As you are on Windows (?) start with a simple Dart console app as you work out the ffi issues. This removes the Flutter issues like asset location, etc. (As an fyi, I don't think you will be able to load the dll from assets in a production flutter app, without additional steps.) – Richard Heap Aug 29 '22 at 07:39
  • @RichardHeap it loaded correctly. checked on other dll that created from c++ – Nikolay Aug 29 '22 at 22:04

0 Answers0