From 87643efacf7b8122fd9053b49a0e52a4552792d6 Mon Sep 17 00:00:00 2001 From: LiJianying Date: Sat, 20 Nov 2021 18:26:22 +0800 Subject: [PATCH] [windows] Update AppAutoLauncherImplWindows --- example/pubspec.lock | 2 +- lib/src/app_auto_launcher_impl_windows.dart | 99 +++++++++++++-------- pubspec.lock | 24 +++-- pubspec.yaml | 2 + 4 files changed, 82 insertions(+), 45 deletions(-) diff --git a/example/pubspec.lock b/example/pubspec.lock index c36980b..3523f7f 100644 --- a/example/pubspec.lock +++ b/example/pubspec.lock @@ -272,7 +272,7 @@ packages: name: win32 url: "https://pub.dartlang.org" source: hosted - version: "2.2.10" + version: "2.3.0" sdks: dart: ">=2.14.0 <3.0.0" flutter: ">=1.20.0" diff --git a/lib/src/app_auto_launcher_impl_windows.dart b/lib/src/app_auto_launcher_impl_windows.dart index c689e51..a820215 100644 --- a/lib/src/app_auto_launcher_impl_windows.dart +++ b/lib/src/app_auto_launcher_impl_windows.dart @@ -1,62 +1,83 @@ -import 'dart:io'; +import 'dart:ffi'; + +import 'package:ffi/ffi.dart'; +import 'package:win32/win32.dart'; import 'app_auto_launcher.dart'; +final _kRegSubKey = + r'Software\Microsoft\Windows\CurrentVersion\Run'.toNativeUtf16(); +const _kRegValueMaxLength = 1024; + class AppAutoLauncherImplWindows extends AppAutoLauncher { AppAutoLauncherImplWindows({ required String appName, required String appPath, }) : super(appName: appName, appPath: appPath); + int _regOpenKey() { + final phkResult = calloc(); + try { + RegOpenKeyEx( + HKEY_CURRENT_USER, + _kRegSubKey, + 0, + KEY_ALL_ACCESS, + phkResult, + ); + return phkResult.value; + } finally { + free(phkResult); + } + } + + int _regCloseKey(int hKey) { + return RegCloseKey(hKey); + } + @override Future isEnabled() async { - final ProcessResult result = await Process.run( - 'REG', - [ - 'QUERY', - 'HKCU\\Software\\Microsoft\\Windows\\CurrentVersion\\Run', - '/v', - appName - ], + int hKey = _regOpenKey(); + final lpData = calloc(_kRegValueMaxLength); + final lpcbData = calloc()..value = _kRegValueMaxLength; + + RegQueryValueEx( + hKey, + appName.toNativeUtf16(), + nullptr, + nullptr, + lpData, + lpcbData, ); - return result.stdout.toString().contains(appName); + String value = lpData.cast().toDartString(); + + free(lpData); + free(lpcbData); + _regCloseKey(hKey); + + return value.trim().isNotEmpty; } @override Future enable() async { - final ProcessResult result = await Process.run( - 'REG', - [ - 'ADD', - 'HKCU\\Software\\Microsoft\\Windows\\CurrentVersion\\Run', - '/v', - appName, - '/t', - 'REG_SZ', - '/d', - '"$appPath"', - '/f' - ], + int hKey = _regOpenKey(); + RegSetKeyValue( + hKey, + ''.toNativeUtf16(), + appName.toNativeUtf16(), + REG_SZ, + appPath.toNativeUtf16(), + _kRegValueMaxLength, ); - return result.stdout - .toString() - .contains('The operation completed successfully.'); + _regCloseKey(hKey); + return true; } @override Future disable() async { - final ProcessResult result = await Process.run( - 'REG', - [ - 'DELETE', - 'HKCU\\Software\\Microsoft\\Windows\\CurrentVersion\\Run', - '/v', - appName, - '/f' - ], - ); - return result.stdout - .toString() - .contains('The operation completed successfully.'); + int hKey = _regOpenKey(); + RegDeleteValue(hKey, appName.toNativeUtf16()); + _regCloseKey(hKey); + return true; } } diff --git a/pubspec.lock b/pubspec.lock index ab09c70..a05a4ff 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -7,7 +7,7 @@ packages: name: async url: "https://pub.dartlang.org" source: hosted - version: "2.8.2" + version: "2.8.1" boolean_selector: dependency: transitive description: @@ -21,7 +21,7 @@ packages: name: characters url: "https://pub.dartlang.org" source: hosted - version: "1.2.0" + version: "1.1.0" charcode: dependency: transitive description: @@ -50,6 +50,13 @@ packages: url: "https://pub.dartlang.org" source: hosted version: "1.2.0" + ffi: + dependency: transitive + description: + name: ffi + url: "https://pub.dartlang.org" + source: hosted + version: "1.1.2" flutter: dependency: "direct main" description: flutter @@ -80,7 +87,7 @@ packages: name: matcher url: "https://pub.dartlang.org" source: hosted - version: "0.12.11" + version: "0.12.10" meta: dependency: transitive description: @@ -141,7 +148,7 @@ packages: name: test_api url: "https://pub.dartlang.org" source: hosted - version: "0.4.3" + version: "0.4.2" typed_data: dependency: transitive description: @@ -155,7 +162,14 @@ packages: name: vector_math url: "https://pub.dartlang.org" source: hosted - version: "2.1.1" + version: "2.1.0" + win32: + dependency: "direct main" + description: + name: win32 + url: "https://pub.dartlang.org" + source: hosted + version: "2.3.0" sdks: dart: ">=2.14.0 <3.0.0" flutter: ">=1.20.0" diff --git a/pubspec.yaml b/pubspec.yaml index 5e359e9..4c612c8 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -11,6 +11,8 @@ dependencies: flutter: sdk: flutter + win32: ^2.3.0 + dev_dependencies: flutter_test: sdk: flutter