0
//random_number.c:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include "randomnumber.h"

int main()
{
    int number = fetch_number();
    printf("Random number: %d\n", number);
    return 0;
}

int fetch_number()
{
    int c, n;
    srand(time(0));
    n = rand() % 100 + 1;
    return n;
}

//randomnumber.h:
int fetch_number();

//lib/shared/services/ffi/ffi.service.dart:
import 'dart:ffi';
import 'package:ffi/ffi.dart';

typedef fetch_number_func = Int32 Function();
typedef FetchNumber = int Function();

class FfiService {
  int fetchRandomNumber() {
    try {
      final dyLib = DynamicLibrary.open('lib/library/windows/randomnumber.dll');
      final fetchNumberPointer = dyLib.lookup<NativeFunction<fetch_number_func>>('fetch_number');
      final number = fetchNumberPointer.asFunction<FetchNumber>();
      return number();
    } catch (exc) {
      print('Something went wrong in fetchRandomNumber ${exc.toString()}');
    }
    return 0;
  }
}

//lib/locator.dart:
import 'package:get_it/get_it.dart';
import 'shared/services/ffi/ffi.service.dart';

final GetIt locator = GetIt.instance;

void setupLocator() {
  locator.registerLazySingleton(() => FfiService());
}

//lib/main.dart:
import 'package:flutter/material.dart';
import 'locator.dart';
import 'shared/services/ffi/ffi.service.dart';

Future<void> main() async {
  WidgetsFlutterBinding.ensureInitialized();
  // INIT SERVICE LOCATOR
  setupLocator();
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: const Home(),
    );
  }
}

class Home extends StatelessWidget {
  const Home({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    final ffiService = locator<FfiService>();
    final val = ffiService.fetchRandomNumber();

    return Scaffold(
      appBar: AppBar(
        title: const Text('FFI Linux'),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            const Text('Getting Value from C'),
            Text('Random Number : ${val.toString()}'),
            const SizedBox(height: 50),
          ],
        ),
      ),
    );
  }
}

//pubspec.yaml
dependencies:
  ffi: ^1.1.2
  get_it: ^7.2.0

//in android > AndroidManifest.xml:
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

//in ios > Runner > Info.plist:
<key>NSPhotoLibraryUsageDescription</key>
<string>Access required to load dynamic library.</string>
<key>NSPhotoLibraryAddUsageDescription</key>
<string>Access required to load dynamic library.</string>


I'm using FFI library to connect a DLL with a Flutter desktop application, but getting: "Failed to load dynamic library".

For the DLL, I wrote a C program to generate a random number

Then, in the Flutter project, I inserted the randomnumber.dll file in the lib/library/windows/ directory. I created a new directory inside lib and added a new file ffi.service.dart

But I am still getting the same error. How can I resolve this error?

Richard Heap
  • 48,344
  • 9
  • 130
  • 112
  • Try using an absolute path to the dll, for example: `DynamicLibrary.open('C:\\Windows\\System32\\.....`. (Note that you should likely put this is an if/else statement on your supported operating systems (e.g. `Platform.isWindows`).) Also, don't load the shared library on each invocation of your function. Load it once, perhaps when you create the FfiService class. Examples of these suggestions here: https://github.com/richardheap/aws_encryption_sdk/blob/main/lib/src/internal/sodium.dart – Richard Heap Jun 28 '23 at 13:25
  • Still getting this after doing all the changes: Exception has occurred. ArgumentError (Invalid argument(s): Failed to load dynamic library 'C:\Users\KAVANGOWDA\Desktop\demo\randomnumber.dll': error code 193) if (Platform.isWindows) { dyLib = DynamicLibrary.open('C:\\Users\\KAVANGOWDA\\Desktop\\demo\\randomnumber.dll'); } – kavan gowda Jun 30 '23 at 04:59
  • Make sure that the bit size (32 vs 64) of the DLL matches that of the Dart SDK. Assuming 64 for the latter, make sure that the DLL is compiled and linked for x64 (vs x86). https://answers.microsoft.com/en-us/windows/forum/all/load-library-error-code-193/f35767f2-a26b-404a-a362-a842cc17f869 – Richard Heap Jun 30 '23 at 12:07

0 Answers0