Skip to content

Commit

Permalink
Merge pull request #6 from jakub-stefaniak/fix/end-of-line-bug
Browse files Browse the repository at this point in the history
fix: use correct max length of registry key
  • Loading branch information
lijy91 authored Mar 10, 2022
2 parents 1c639d0 + e4baacc commit 03c2477
Showing 1 changed file with 9 additions and 7 deletions.
16 changes: 9 additions & 7 deletions lib/src/app_auto_launcher_impl_windows.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,15 @@ 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 get _regValueMaxLength => appPath.codeUnits.length * 2;

int _regOpenKey() {
final phkResult = calloc<HANDLE>();
try {
Expand All @@ -38,8 +39,8 @@ class AppAutoLauncherImplWindows extends AppAutoLauncher {
@override
Future<bool> isEnabled() async {
int hKey = _regOpenKey();
final lpData = calloc<BYTE>(_kRegValueMaxLength);
final lpcbData = calloc<DWORD>()..value = _kRegValueMaxLength;
final lpData = calloc<BYTE>(_regValueMaxLength);
final lpcbData = calloc<DWORD>()..value = 1024;

RegQueryValueEx(
hKey,
Expand All @@ -49,25 +50,26 @@ class AppAutoLauncherImplWindows extends AppAutoLauncher {
lpData,
lpcbData,
);
String value = lpData.cast<Utf16>().toDartString();
var value = lpData.cast<Utf16>().toDartString();

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

return value.trim().isNotEmpty;
return value.isNotEmpty;
}

@override
Future<bool> enable() async {
int hKey = _regOpenKey();

RegSetKeyValue(
hKey,
''.toNativeUtf16(),
appName.toNativeUtf16(),
REG_SZ,
('"' + appPath + '"').toNativeUtf16(),
_kRegValueMaxLength,
appPath.toNativeUtf16(),
_regValueMaxLength,
);
_regCloseKey(hKey);
return true;
Expand Down

0 comments on commit 03c2477

Please sign in to comment.