stop crashing for nothing, fix reboot to firmware
This commit is contained in:
31
main.go
31
main.go
@@ -1,6 +1,7 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"Win2Linux/pkg/windows"
|
||||
_ "embed"
|
||||
"fmt"
|
||||
"os"
|
||||
@@ -49,7 +50,10 @@ func onReady() {
|
||||
mCustom := systray.AddMenuItem(key, "Switch to"+key)
|
||||
go func() {
|
||||
<-mCustom.ClickedCh
|
||||
fmt.Println(uuid)
|
||||
if uuid == "{fwbootmgr}" {
|
||||
rebootToFirmware()
|
||||
return
|
||||
}
|
||||
reboot(uuid)
|
||||
}()
|
||||
|
||||
@@ -71,7 +75,7 @@ func list() []Entry {
|
||||
cmd := exec.Command("bcdedit", "/enum", "firmware")
|
||||
out, err := cmd.Output()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
fatal(fmt.Sprintf("failed to run bcdedit: %s\n\nRun this program in administrator mode", err), 1)
|
||||
}
|
||||
return parse(string(out))
|
||||
}
|
||||
@@ -122,13 +126,26 @@ func parse(out string) []Entry {
|
||||
}
|
||||
|
||||
func reboot(uuid string) {
|
||||
if err := exec.Command("bcdedit", "/Set", "{fwbootmgr}", "BootSequence", uuid, "/addFirst").Run(); err != nil {
|
||||
panic(err)
|
||||
out, err := exec.Command("bcdedit", "/Set", "{fwbootmgr}", "BootSequence", uuid, "/addFirst").Output()
|
||||
if err != nil {
|
||||
windows.MessageBox(windows.NULL, fmt.Sprintf("failed to execute bcdedit: %s", out), "Win2Linux", windows.MB_OK)
|
||||
return
|
||||
}
|
||||
|
||||
if err := exec.Command("shutdown", "/r", "/t", "0").Run(); err != nil {
|
||||
panic(err)
|
||||
out, err = exec.Command("shutdown", "/r", "/t", "0").Output()
|
||||
if err != nil {
|
||||
windows.MessageBox(windows.NULL, fmt.Sprintf("failed to restart the computer: %s", out), "Win2Linux", windows.MB_OK)
|
||||
}
|
||||
}
|
||||
|
||||
os.Exit(0)
|
||||
func rebootToFirmware() {
|
||||
out, err := exec.Command("shutdown", "/r", "/fw", "/t", "0").Output()
|
||||
if err != nil {
|
||||
windows.MessageBox(windows.NULL, fmt.Sprintf("failed to restart the computer: %s", out), "Win2Linux", windows.MB_OK)
|
||||
}
|
||||
}
|
||||
|
||||
func fatal(message string, exitCode int) {
|
||||
windows.MessageBox(windows.NULL, message, "Win2Linux", windows.MB_OK)
|
||||
os.Exit(exitCode)
|
||||
}
|
||||
|
||||
30
pkg/windows/windows.go
Normal file
30
pkg/windows/windows.go
Normal file
@@ -0,0 +1,30 @@
|
||||
package windows
|
||||
|
||||
import (
|
||||
"syscall"
|
||||
"unsafe"
|
||||
)
|
||||
|
||||
const (
|
||||
NULL = 0
|
||||
MB_OK = 0
|
||||
)
|
||||
|
||||
// MessageBox of Win32 API.
|
||||
func MessageBox(hwnd uintptr, caption, title string, flags uint) int {
|
||||
_caption, err := syscall.UTF16PtrFromString(caption)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
_title, err := syscall.UTF16PtrFromString(title)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
ret, _, _ := syscall.NewLazyDLL("user32.dll").NewProc("MessageBoxW").Call(
|
||||
uintptr(hwnd),
|
||||
uintptr(unsafe.Pointer(_caption)),
|
||||
uintptr(unsafe.Pointer(_title)),
|
||||
uintptr(flags))
|
||||
|
||||
return int(ret)
|
||||
}
|
||||
Reference in New Issue
Block a user