如何在颤振构建窗口中包含dll



我正在开发一个运行良好的扑动项目。但是,我不知道如何让构建包括使用FFI引用的dll。

我找不到关于如何做这件事的明确说明。

我尝试按照步骤在这里构建一个msix,它工作,但似乎不包括dll(它以与常规构建相同的方式失败)

让构建过程考虑dll的过程是什么?

其他dll的显示在构建目录从第三方包,所以必须有一个方法对吗?

这很难自己发现,但确实可以将这些库绑定到MSIX。在我的情况下,我只是使用Dart FFI和制造商提供的DLL为标签打印机制作了一个包,这就是我如何做到的。

您需要将这些DLL添加到软件包中的pubspec.yaml上的assets设置中。这是我的情况:

[...]
flutter:
[...]
assets:
- assets/WinPort.dll
- assets/Winppla.dll
- assets/Winpplb.dll
- assets/Winpplz.dll

使用此设置,您将在最终的MSIX中嵌入DLL文件,但这是简单的部分。现在您已经确保正确加载了这些文件的代码。根据我自己的测试,我仍然处理两种方法来开发和测试代码,第一种是当我通过flutter run在我的机器上运行一个项目时,我必须为当前设置目标。当我完成它并开始为部署构建时,我将其更改为resolvedExecutable.parent.path。哪里是你需要做的。在开发环境(flutter run)中加载DLL:

final String _packageAssetsDirPath = normalize(join(Directory.current.path,'assets'));

在生产环境下(从安装的。exe或MSIX运行):

final String _assetsPackageDir = normalize(
join('data', 'flutter_assets', 'packages', 'YOUR_PACKAGE_NAME', 'assets'));
final String _exeDirPath = File(Platform.resolvedExecutable).parent.path;
final String _packageAssetsDirPath =
normalize(join(_exeDirPath, _assetsPackageDir));

使用这个名为_packageAssetsDirPath的变量后,将很容易加载您的DLL,现在您调用DynamicLibrary构造函数:

// Path for DLL file
final String _libDllSourceFullPath =
normalize(join(_packageAssetsDirPath, 'Winppla.dll'));
// Target for copy, place DLL in same place the .exe you are running
final String _libDllDestFullPath =
normalize(join(_packageAssetsDirPath, 'YOUROWN.dll'));
// Try to copy for running exe path
File(_libDllSourceFullPath).copySync(_libDllDestFullPath);
// With this copy, would be simple to load, and if it fails, try in full path
// LOAD DLL
try {
String _packageAssetsDirPath =
normalize(join(Directory.current.path, 'assets'));
String _printerLibraryPath =
normalize(join(_packageAssetsDirPath, 'Winppla.dll'));
DynamicLibrary _library = DynamicLibrary.open(_printerLibraryPath);
return _library;
} catch (e) {
try {
DynamicLibrary _library = DynamicLibrary.open('Winppla.dll');
return _library;
} catch (e) {
// Avoing errors creating a fake DLL, but you could deal with an exception
return DynamicLibrary.process();
}
}

在这一点上,你可以加载一个DLL并使用它,你可以检查我的包的完整代码在https://github.com/saviobatista/argox_printer检查lib/src/ppla.dart_setupDll()函数,你会看到加载。

受Sávio Batista解决方案的启发,我构建了一个更简单的选项

(你必须有你的。dll在你的资产文件夹)

if (kReleaseMode) {
// I'm on release mode, absolute linking
final String local_lib =  join('data',  'flutter_assets', 'assets', 'libturbojpeg.dll');
String pathToLib = join(Directory(Platform.resolvedExecutable).parent.path, local_lib);
DynamicLibrary lib = DynamicLibrary.open(pathToLib);
} else {
// I'm on debug mode, local linking
var path = Directory.current.path;
DynamicLibrary lib = DynamicLibrary.open('$path/assets/libturbojpeg.dll');
}

将libturbojpeg.dll替换为你的.dll

最新更新