I want to call c++ function using dll in flutter.
It's dll fuction Header. It worked well when I called dll on mfc. The name of DLL is "DllFont.dll".
#pragma once
#ifdef CREATEDLL_EXPORTS
#define FONTDLL_API __declspec(dllexport)
#else
#define FONTDLL_API __declspec(dllimport)
#endif
extern "C" FONTDLL_API void AddResourceFont(TCHAR * chFontPath);
extern "C" FONTDLL_API void RemoveResourceFont(TCHAR * chFontPath);
It's dll fuction implement CPP. The following implementation is a function of receiving the path of the .ttf file in TCHAR* type and installing/deleting fonts.
#include "pch.h"
#include "DllFont.h"
#include <wininet.h>
#include "urlmon.h"
#include <afxwin.h>
#include "tchar.h"
#include "atlstr.h"
void AddResourceFont(TCHAR* chFontPath)
{
CString strFontPath;
strFontPath.Format(_T("%s"), chFontPath);
strFontPath = (LPCTSTR)chFontPath;
AddFontResourceW(strFontPath);
SendMessage((HWND)0xffff, WM_FONTCHANGE, 0, 0);
}
void RemoveResourceFont(TCHAR* chFontPath)
{
CString strFontPath;
strFontPath.Format(_T("%s"), chFontPath);
strFontPath = (LPCTSTR)chFontPath;;
RemoveFontResourceW(strFontPath);
SendMessage((HWND)0xffff, WM_FONTCHANGE, 0, 0);
}
I checked that the DLL was good.
It's Flutter Code. When I call Func "AddResourceFont(str.toNativeUtf16());" It's not work,,, Why...?
I copied .Dll and .lib files to the Flutter project "lib" folder.
import 'package:flutter/material.dart';
import 'package:path_provider/path_provider.dart';
import 'package:flutter/foundation.dart';
import 'dart:io';
import 'dart:ffi';
import "package:ffi/ffi.dart";
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return const MaterialApp(
title: 'desktopfont',
home: MyCustomForm(),
);
}
}
class MyCustomForm extends StatefulWidget {
const MyCustomForm({super.key});
@override
State<MyCustomForm> createState() => _MyCustomFormState();
}
class _MyCustomFormState extends State<MyCustomForm> {
// Create a text controller and use it to retrieve the current value
// of the TextField.
final myController = TextEditingController();
@override
void dispose() {
// Clean up the controller when the widget is disposed.
myController.dispose();
super.dispose();
}
@override
Future<File> _downloadFile(String url) async {
String strFileName;
final splitted = url.split('/');
strFileName = splitted.last;
var httpClient = HttpClient();
try {
var request = await httpClient.getUrl(Uri.parse(url));
var response = await request.close();
var bytes = await consolidateHttpClientResponseBytes(response);
final dir =
await getTemporaryDirectory(); //(await getApplicationDocumentsDirectory()).path;
File file = File('${dir.path}/$strFileName');
await file.writeAsBytes(bytes);
//print('downloaded file path = ${file.path}');
return file;
} catch (error) {
//print('pdf downloading error = $error');
return File('');
}
}
@override
Widget build(BuildContext context) {
final DynamicLibrary nativeAddLib = DynamicLibrary.open('DllFont.dll');
final void Function(Pointer<Utf16> y) AddResourceFont = nativeAddLib
.lookup<NativeFunction<Void Function(Pointer<Utf16>)>>(
"AddResourceFont")
.asFunction();
final void Function(Pointer<Utf16> y) RemoveResourceFont = nativeAddLib
.lookup<NativeFunction<Void Function(Pointer<Utf16>)>>(
"RemoveResourceFont")
.asFunction();
return Scaffold(
appBar: AppBar(
title: const Text('desktopfont'),
),
body: Center(
child: Column(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
TextField(
controller: myController,
),
const SizedBox(height: 30),
TextButton(
style: TextButton.styleFrom(
textStyle: const TextStyle(fontSize: 20),
),
onPressed: () async {
_downloadFile(myController.text);
//DLL CallFunc.
String strFileName;
final splitted = myController.text.split('/');
strFileName = splitted.last;
final dir = await getTemporaryDirectory();
String str = dir.path + strFileName;
AddResourceFont(str.toNativeUtf16());
},
child: const Text('Install'),
),
const SizedBox(height: 30),
TextButton(
style: TextButton.styleFrom(
textStyle: const TextStyle(fontSize: 20),
),
onPressed: () {
showDialog(
context: context,
builder: (context) {
return AlertDialog(
// Retrieve the text the that user has entered by using the
// TextEditingController.
content: Text(myController.text),
);
},
);
},
child: const Text('Uninstall'),
),
],
),
),
);
}
}