gpt4 book ai didi

c - 如何在 Dart 中扩展 Pointer

转载 作者:行者123 更新时间:2023-12-03 03:56:32 26 4
gpt4 key购买 nike

目前在做一个flutter项目,需要用ffi调用c语言的函数。经过研究,它需要创建 cstring 类扩展指针,在我创建后,它通知我 The class 'CString' can't extend Pointer。那么我需要导入特定的库还是只有特定的 dart 版本才能导入?

import 'dart:async';
import 'dart:typed_data';

import 'package:flutter/services.dart';

import 'dart:ffi'; // For FFI
import 'dart:io' show Platform;
import "dart:convert";


final DynamicLibrary nativeAddLib =
Platform.isAndroid
? DynamicLibrary.open("libnative_add.so")
: DynamicLibrary.process();

typedef NativeEncrypt = Void Function(CString,CString,Int32);
typedef DartEncrypt = void Function(CString,CString,int);


final int Function(int x, int y) nativePlus =
nativeAddLib
.lookup<NativeFunction<Int32 Function(Int32, Int32)>>("native_plus")
.asFunction();

final int Function(int x, int y) nativeMinus =
nativeAddLib
.lookup<NativeFunction<Int32 Function(Int32, Int32)>>("native_minus")
.asFunction();

final int Function(int x, int y) nativeTimes =
nativeAddLib
.lookup<NativeFunction<Int32 Function(Int32, Int32)>>("native_times")
.asFunction();

final int Function(int x, int y) nativeDivide =
nativeAddLib
.lookup<NativeFunction<Int32 Function(Int32, Int32)>>("native_divide")
.asFunction();

final int Function(List<Int32>, int length) nativeTOTP =
nativeAddLib
.lookup<NativeFunction<Int32 Function(List<Int32>,Int32)>>("native_totp")
.asFunction();

var encrypt = nativeAddLib.lookupFunction<NativeEncrypt, DartEncrypt>("encrypt");




class NativeAdd {
static const MethodChannel _channel =
const MethodChannel('native_add');


static Future<String> get platformVersion async {
final String version = await _channel.invokeMethod('getPlatformVersion');
return version;
}

static String encryptFlutter(String value){
CString data = CString.allocate(value);
CString enResult = CString.malloc(100);
encrypt(data,enResult,100);
return CString.fromUtf8(enResult);
}
}


class CString extends Pointer<Int8> {

/// 开辟内存控件,将Dart字符串转为C语言字符串
factory CString.allocate(String dartStr, [Reference ref]) {
List<int> units = Utf8Encoder().convert(dartStr);
Pointer<Int8> str = allocate(count: units.length + 1);
for (int i = 0; i < units.length; ++i) {
str.elementAt(i).store(units[i]);
}
str.elementAt(units.length).store(0);

ref?.ref(str);
return str.cast();
}

factory CString.malloc(int size, [Reference ref]) {
Pointer<Int8> str = allocate(count: size);
ref?.ref(str);
return str.cast();
}

/// 将C语言中的字符串转为Dart中的字符串
static String fromUtf8(CString str) {
if (str == null) return null;
int len = 0;
while (str.elementAt(++len).load<int>() != 0);
List<int> units = List(len);
for (int i = 0; i < len; ++i) units[i] = str.elementAt(i).load();
return Utf8Decoder().convert(units);
}
}

我的C语言函数是这样的

#include <stdint.h>
#include "sub_lib.h"

int32_t native_plus(int32_t x, int32_t y) {
return x + y + getDigits();
}

int32_t native_minus(int32_t x, int32_t y) {
return x - y + getDigits();
}

int32_t native_times(int32_t x, int32_t y) {
return x * y + getDigits();
}

int32_t native_divide(int32_t x, int32_t y) {
return x / y + getDigits();
}

int32_t native_totp(int32_t x[], int32_t y) {
int32_t totp = 103355;
return x;
}

void encrypt(char *str, char *r, int r_len){
int len = strlen(str);
for(int i = 0; i < len && i < r_len; i++){
r[i] = str[i] ^ KEY;
}

if (r_len > len) r[len] = '\0';
else r[r_len] = '\0';
}

最佳答案

我正在努力将一些 C 库插入到 flutter 插件中。经过一些研究,我发现指针不再是可扩展的。我认为他们更改了 API。

https://api.dart.dev/stable/2.8.4/dart-ffi/Pointer-class.html

Represents a pointer into the native C memory. Cannot be extended.

现在拥有自定义签名的唯一方法是将其嵌入到您的类中

https://github.com/dart-lang/sdk/blob/2513d37525eba631a92b0f7845cd19be37c45e96/samples/ffi/sqlite/lib/src/database.dart#L26

关于c - 如何在 Dart 中扩展 Pointer<Int8>?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60883241/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com