Skip to content

Commit eeca18b

Browse files
committed
[windows] Replace win32 with win32_registry
1 parent 24f12ae commit eeca18b

File tree

5 files changed

+43
-70
lines changed

5 files changed

+43
-70
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 0.1.7
2+
3+
- [windows] Replace `win32` with `win32_registry`
4+
15
## 0.1.6
26

37
- [windows] fix: use correct max length of registry key #

example/pubspec.lock

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ packages:
120120
path: ".."
121121
relative: true
122122
source: path
123-
version: "0.1.6"
123+
version: "0.1.7"
124124
lints:
125125
dependency: transitive
126126
description:
@@ -279,7 +279,14 @@ packages:
279279
name: win32
280280
url: "https://pub.dartlang.org"
281281
source: hosted
282-
version: "2.3.0"
282+
version: "2.4.2"
283+
win32_registry:
284+
dependency: transitive
285+
description:
286+
name: win32_registry
287+
url: "https://pub.dartlang.org"
288+
source: hosted
289+
version: "0.1.2"
283290
sdks:
284-
dart: ">=2.14.0 <3.0.0"
291+
dart: ">=2.15.0 <3.0.0"
285292
flutter: ">=1.20.0"
Lines changed: 16 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1,85 +1,41 @@
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';
53

64
import 'app_auto_launcher.dart';
75

8-
final _kRegSubKey =
9-
r'Software\Microsoft\Windows\CurrentVersion\Run'.toNativeUtf16();
10-
116
class AppAutoLauncherImplWindows extends AppAutoLauncher {
127
AppAutoLauncherImplWindows({
138
required String appName,
149
required String appPath,
1510
}) : super(appName: appName, appPath: appPath);
1611

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,
2816
);
29-
return phkResult.value;
30-
} finally {
31-
free(phkResult);
32-
}
33-
}
34-
35-
int _regCloseKey(int hKey) {
36-
return RegCloseKey(hKey);
37-
}
3817

3918
@override
4019
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;
6022
}
6123

6224
@override
6325
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+
));
7531
return true;
7632
}
7733

7834
@override
7935
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+
}
8339
return true;
8440
}
8541
}

pubspec.lock

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ packages:
5151
source: hosted
5252
version: "1.2.0"
5353
ffi:
54-
dependency: "direct main"
54+
dependency: transitive
5555
description:
5656
name: ffi
5757
url: "https://pub.dartlang.org"
@@ -171,12 +171,19 @@ packages:
171171
source: hosted
172172
version: "2.1.1"
173173
win32:
174-
dependency: "direct main"
174+
dependency: transitive
175175
description:
176176
name: win32
177177
url: "https://pub.dartlang.org"
178178
source: hosted
179-
version: "2.3.0"
179+
version: "2.4.2"
180+
win32_registry:
181+
dependency: "direct main"
182+
description:
183+
name: win32_registry
184+
url: "https://pub.dartlang.org"
185+
source: hosted
186+
version: "0.1.2"
180187
sdks:
181-
dart: ">=2.14.0 <3.0.0"
188+
dart: ">=2.15.0 <3.0.0"
182189
flutter: ">=1.20.0"

pubspec.yaml

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name: launch_at_startup
22
description: This plugin allows Flutter desktop apps to Auto launch on startup / login.
3-
version: 0.1.6
3+
version: 0.1.7
44
homepage: https://github.com/leanflutter/launch_at_startup
55

66
platforms:
@@ -16,8 +16,7 @@ dependencies:
1616
flutter:
1717
sdk: flutter
1818

19-
ffi: ^1.0.0
20-
win32: ^2.3.0
19+
win32_registry: ^0.1.2
2120

2221
dev_dependencies:
2322
flutter_test:

0 commit comments

Comments
 (0)