|
1 | | -import 'dart:ffi'; |
2 | | - |
3 | | -import 'package:ffi/ffi.dart' if (dart.library.html) 'noop_ffi.dart'; |
4 | | -import 'package:win32/win32.dart' if (dart.library.html) 'noop_win32.dart'; |
| 1 | +import 'package:win32_registry/win32_registry.dart' |
| 2 | + if (dart.library.html) 'noop.dart'; |
5 | 3 |
|
6 | 4 | import 'app_auto_launcher.dart'; |
7 | 5 |
|
8 | | -final _kRegSubKey = |
9 | | - r'Software\Microsoft\Windows\CurrentVersion\Run'.toNativeUtf16(); |
10 | | - |
11 | 6 | class AppAutoLauncherImplWindows extends AppAutoLauncher { |
12 | 7 | AppAutoLauncherImplWindows({ |
13 | 8 | required String appName, |
14 | 9 | required String appPath, |
15 | 10 | }) : super(appName: appName, appPath: appPath); |
16 | 11 |
|
17 | | - int get _regValueMaxLength => appPath.codeUnits.length * 2; |
18 | | - |
19 | | - int _regOpenKey() { |
20 | | - final phkResult = calloc<HANDLE>(); |
21 | | - try { |
22 | | - RegOpenKeyEx( |
23 | | - HKEY_CURRENT_USER, |
24 | | - _kRegSubKey, |
25 | | - 0, |
26 | | - KEY_ALL_ACCESS, |
27 | | - phkResult, |
| 12 | + RegistryKey get _regKey => Registry.openPath( |
| 13 | + RegistryHive.currentUser, |
| 14 | + path: r'Software\Microsoft\Windows\CurrentVersion\Run', |
| 15 | + desiredAccessRights: AccessRights.allAccess, |
28 | 16 | ); |
29 | | - return phkResult.value; |
30 | | - } finally { |
31 | | - free(phkResult); |
32 | | - } |
33 | | - } |
34 | | - |
35 | | - int _regCloseKey(int hKey) { |
36 | | - return RegCloseKey(hKey); |
37 | | - } |
38 | 17 |
|
39 | 18 | @override |
40 | 19 | Future<bool> isEnabled() async { |
41 | | - int hKey = _regOpenKey(); |
42 | | - final lpData = calloc<BYTE>(_regValueMaxLength); |
43 | | - final lpcbData = calloc<DWORD>()..value = 1024; |
44 | | - |
45 | | - RegQueryValueEx( |
46 | | - hKey, |
47 | | - appName.toNativeUtf16(), |
48 | | - nullptr, |
49 | | - nullptr, |
50 | | - lpData, |
51 | | - lpcbData, |
52 | | - ); |
53 | | - var value = lpData.cast<Utf16>().toDartString(); |
54 | | - |
55 | | - free(lpData); |
56 | | - free(lpcbData); |
57 | | - _regCloseKey(hKey); |
58 | | - |
59 | | - return value.isNotEmpty; |
| 20 | + String? value = _regKey.getValueAsString(appName); |
| 21 | + return value == appPath; |
60 | 22 | } |
61 | 23 |
|
62 | 24 | @override |
63 | 25 | Future<bool> enable() async { |
64 | | - int hKey = _regOpenKey(); |
65 | | - |
66 | | - RegSetKeyValue( |
67 | | - hKey, |
68 | | - ''.toNativeUtf16(), |
69 | | - appName.toNativeUtf16(), |
70 | | - REG_SZ, |
71 | | - appPath.toNativeUtf16(), |
72 | | - _regValueMaxLength, |
73 | | - ); |
74 | | - _regCloseKey(hKey); |
| 26 | + _regKey.createValue(RegistryValue( |
| 27 | + appName, |
| 28 | + RegistryValueType.string, |
| 29 | + appPath, |
| 30 | + )); |
75 | 31 | return true; |
76 | 32 | } |
77 | 33 |
|
78 | 34 | @override |
79 | 35 | Future<bool> disable() async { |
80 | | - int hKey = _regOpenKey(); |
81 | | - RegDeleteValue(hKey, appName.toNativeUtf16()); |
82 | | - _regCloseKey(hKey); |
| 36 | + if (await isEnabled()) { |
| 37 | + _regKey.deleteValue(appName); |
| 38 | + } |
83 | 39 | return true; |
84 | 40 | } |
85 | 41 | } |
0 commit comments