Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Making sure startup is approved on Windows #129

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
40 changes: 35 additions & 5 deletions src/library/autoLaunchAPI/autoLaunchAPIWindows.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,14 @@ import path from 'path';
import Winreg from 'winreg';
import AutoLaunchAPI from './autoLaunchAPI.js';

const regKey = new Winreg({
const runRegKey = new Winreg({
hive: Winreg.HKCU,
key: '\\Software\\Microsoft\\Windows\\CurrentVersion\\Run'
key: '\\Software\\Microsoft\\Windows\\CurrentVersion\\Run',
});

const startupApprovedRegKey = new Winreg({
hive: Winreg.HKCU,
key: '\\Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\StartupApproved\\Run',
});

export default class AutoLaunchAPIWindows extends AutoLaunchAPI {
Expand Down Expand Up @@ -64,10 +69,18 @@ export default class AutoLaunchAPIWindows extends AutoLaunchAPI {
}
}

regKey.set(this.appName, Winreg.REG_SZ, `"${pathToAutoLaunchedApp}"${args}`, (err) => {
runRegKey.set(this.appName, Winreg.REG_SZ, `"${pathToAutoLaunchedApp}"${args}`, (err) => {
if (err != null) {
return reject(err);
}

// Enable the startup task in StartupApproved
startupApprovedRegKey.set(this.appName, Winreg.REG_BINARY, Buffer.from('03000000', 'hex'), (err_) => {
if (err_ != null) {
return reject(err_);
}
return resolve();
});
return resolve();
});
});
Expand All @@ -76,7 +89,7 @@ export default class AutoLaunchAPIWindows extends AutoLaunchAPI {
// Returns a Promise
disable() {
return new Promise((resolve, reject) => {
regKey.remove(this.appName, (err) => {
runRegKey.remove(this.appName, (err) => {
if (err != null) {
// The registry key should exist but, in case it fails because it doesn't exist,
// resolve false instead of rejecting with an error
Expand All @@ -85,6 +98,14 @@ export default class AutoLaunchAPIWindows extends AutoLaunchAPI {
}
return reject(err);
}

// Disable the startup task in StartupApproved
startupApprovedRegKey.set(this.appName, Winreg.REG_BINARY, Buffer.from('02000000', 'hex'), (err_) => {
if (err_ != null) {
return reject(err_);
}
return resolve();
});
return resolve();
});
});
Expand All @@ -93,10 +114,19 @@ export default class AutoLaunchAPIWindows extends AutoLaunchAPI {
// Returns a Promise which resolves to a {Boolean}
isEnabled() {
return new Promise((resolve) => {
regKey.valueExists(this.appName, (err, exists) => {
runRegKey.valueExists(this.appName, (err, exists) => {
if (err != null) {
return resolve(false);
}

// Check if the startup task is enabled
startupApprovedRegKey.get(this.appName, (err_, item) => {
if (err_ != null || !item || item.value.toString('hex') !== '03000000') {
return resolve(false);
}
return resolve(true);
});

return resolve(exists);
});
});
Expand Down