일상생활

Connecting Dll from Flutter 본문

tech

Connecting Dll from Flutter

SM_SNAIL 2025. 1. 22. 09:14

1. 패키지 설치

pubspec.yaml 에 다음과 같이  패키지를 추가합니다.

dependencies:
  flutter:
    sdk: flutter
  ffi: ^2.0.1 # 최신 버전 확인 후 입력
  charset_converter: ^2.0.1 # cp949
  # The following adds the Cupertino Icons font to your application.
  # Use with the CupertinoIcons class for iOS style icons.
  cupertino_icons: ^1.0.6
  intl: ^0.18.0
 
dev_dependencies:
  flutter_test:
    sdk: flutter
  ffi: ^2.0.1 # 최신 버전 확인 후 입력
  charset_converter: ^2.0.1 # cp949
  # The "flutter_lints" package below contains a set of recommended lints to
  # encourage good coding practices. The lint set provided by the package is
  # activated in the `analysis_options.yaml` file located at the root of your
  # package. See that file for information about deactivating specific lint
  # rules and activating additional ones.
  flutter_lints: ^3.0.0
  intl: ^0.18.0

추가하고 패키지를 설치합니다. 

$ flutter pub get

 

2. DLL 선언

import 'dart:convert';
import 'dart:ffi';
import 'dart:io';
import 'package:ffi/ffi.dart'; 
import 'dart:typed_data';
import 'package:charset_converter/charset_converter.dart';

// DLL 파일 로드
final DynamicLibrary kisPosAgentLibrary = DynamicLibrary.open('Dll.dll');

typedef CInitDll = Int32 Function();
typedef DartInitDll = int Function();
final DartInitDll initDll = DllLibrary
    .lookup<NativeFunction<CInitDll>>('InitDll')
    .asFunction<DartInitDll>();

 

3. mart.dart 연결하기

// InitDll 호출
final initResult = initDll();
print('InitDll result: $initResult');
if (initResult != 0) {
  print('InitDll failed with code: $initResult');
  return;
}

 

반응형