Skip to content

Instantly share code, notes, and snippets.

@donovansolms
Created March 3, 2019 20:20
Show Gist options
  • Save donovansolms/e5439c333454273a6ce21b06e838ff7a to your computer and use it in GitHub Desktop.
Save donovansolms/e5439c333454273a6ce21b06e838ff7a to your computer and use it in GitHub Desktop.
Hack ProtonMail/go-autostart for Windows start menu item
package autostart
// #cgo LDFLAGS: -lole32 -luuid
/*
#define WIN32_LEAN_AND_MEAN
#include <stdint.h>
#include <windows.h>
uint64_t CreateShortcut(char *shortcutA, char *path, char *args);
*/
import "C"
import (
"errors"
"fmt"
"os"
"path/filepath"
"strings"
)
var startupDir string
var startMenuDir string
func init() {
startupDir = filepath.Join(os.Getenv("USERPROFILE"), "AppData", "Roaming", "Microsoft", "Windows", "Start Menu", "Programs", "Startup")
startMenuDir = filepath.Join(os.Getenv("USERPROFILE"), "AppData", "Roaming", "Microsoft", "Windows", "Start Menu", "Programs")
}
func (a *App) path(startMenu bool) string {
if startMenu {
return filepath.Join(startMenuDir, a.Name+".lnk")
}
return filepath.Join(startupDir, a.Name+".lnk")
}
func (a *App) IsEnabled(startMenu bool) bool {
_, err := os.Stat(a.path(startMenu))
return err == nil
}
func (a *App) Enable(startMenu bool) error {
path := a.Exec[0]
args := strings.Join(a.Exec[1:], " ")
if !startMenu{
if err := os.MkdirAll(startupDir, 0777); err != nil {
return err
}
}
res := C.CreateShortcut(C.CString(a.path(startMenu)), C.CString(path), C.CString(args))
if res != 0 {
return errors.New(fmt.Sprintf("autostart: cannot create shortcut '%s' error code: 0x%.8x", a.path(startMenu), res))
}
return nil
}
func (a *App) Disable(startMenu bool) error {
return os.Remove(a.path(startMenu))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment