Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 23 additions & 2 deletions lib/src/app_auto_launcher_impl_windows.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import 'dart:io';
import 'dart:typed_data';

import 'package:launch_at_startup/src/app_auto_launcher.dart';
import 'package:win32/win32.dart'
show ERROR_FILE_NOT_FOUND, HRESULT_FROM_WIN32, WindowsException;
import 'package:win32_registry/win32_registry.dart'
if (dart.library.html) 'noop.dart';

Expand Down Expand Up @@ -40,7 +42,17 @@ class AppAutoLauncherImplWindows extends AppAutoLauncher {

@override
Future<bool> isEnabled() async {
String? value = _regKey.getStringValue(appName);
final String? value;
try {
value = _regKey.getStringValue(appName);
} on WindowsException catch (e) {
// The Run key may not exist yet on a fresh Windows install where no
// application has registered for startup, which means auto-start is
// not enabled. Rethrow anything else (for example access denied) so a
// genuine failure is not silently masked.
if (e.hr != HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND)) rethrow;
return false;
}

return value == _registryValue && await _isStartupApproved();
}
Expand Down Expand Up @@ -74,7 +86,16 @@ class AppAutoLauncherImplWindows extends AppAutoLauncher {
// Odd first byte will prevent the app from autostarting
// Empty or any other value will allow the app to autostart
Future<bool> _isStartupApproved() async {
final value = _startupApprovedRegKey.getBinaryValue(appName);
final Uint8List? value;
try {
value = _startupApprovedRegKey.getBinaryValue(appName);
} on WindowsException catch (e) {
// The StartupApproved\Run key is created lazily and is often absent on
// a fresh install; a missing key is treated as approved by the null
// check below. Rethrow anything else so a genuine failure surfaces.
if (e.hr != HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND)) rethrow;
value = null;
}

if (value == null) {
return true;
Expand Down
1 change: 1 addition & 0 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ environment:
dependencies:
flutter:
sdk: flutter
win32: ^5.8.0
win32_registry: ^2.0.0

dev_dependencies:
Expand Down