Skip to content

Commit

Permalink
[windows] Update AppAutoLauncherImplWindows
Browse files Browse the repository at this point in the history
  • Loading branch information
lijy91 committed Nov 20, 2021
1 parent 5014b3d commit 87643ef
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 45 deletions.
2 changes: 1 addition & 1 deletion example/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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"
99 changes: 60 additions & 39 deletions lib/src/app_auto_launcher_impl_windows.dart
Original file line number Diff line number Diff line change
@@ -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<HANDLE>();
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<bool> isEnabled() async {
final ProcessResult result = await Process.run(
'REG',
[
'QUERY',
'HKCU\\Software\\Microsoft\\Windows\\CurrentVersion\\Run',
'/v',
appName
],
int hKey = _regOpenKey();
final lpData = calloc<BYTE>(_kRegValueMaxLength);
final lpcbData = calloc<DWORD>()..value = _kRegValueMaxLength;

RegQueryValueEx(
hKey,
appName.toNativeUtf16(),
nullptr,
nullptr,
lpData,
lpcbData,
);
return result.stdout.toString().contains(appName);
String value = lpData.cast<Utf16>().toDartString();

free(lpData);
free(lpcbData);
_regCloseKey(hKey);

return value.trim().isNotEmpty;
}

@override
Future<bool> 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<bool> 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;
}
}
24 changes: 19 additions & 5 deletions pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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:
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand All @@ -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"
2 changes: 2 additions & 0 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ dependencies:
flutter:
sdk: flutter

win32: ^2.3.0

dev_dependencies:
flutter_test:
sdk: flutter
Expand Down

0 comments on commit 87643ef

Please sign in to comment.