PrintToVeraeTimeStamp/cmd/verae-print/open_windows.go

46 lines
1.3 KiB
Go

//go:build windows
package main
import (
"fmt"
"os"
"os/exec"
"path/filepath"
"syscall"
"unsafe"
"github.com/VeraeLLC/PrintToVeraeTimeStamp/internal/settings"
)
var procShellExecuteW = syscall.NewLazyDLL("shell32.dll").NewProc("ShellExecuteW")
func shellOpen(path string) error {
verb, _ := syscall.UTF16PtrFromString("open")
file, _ := syscall.UTF16PtrFromString(path)
r, _, _ := procShellExecuteW.Call(0, uintptr(unsafe.Pointer(verb)), uintptr(unsafe.Pointer(file)), 0, 0, 1 /*SW_SHOWNORMAL*/)
if r <= 32 {
return fmt.Errorf("ShellExecute returned %d", r)
}
return nil
}
// openResult runs in the helper, which already lives in the logged-in user's
// session (scheduled task, Interactive), so Explorer opens on their desktop.
func openResult(mode, cert string) error {
switch mode {
case settings.OpenPDF:
return shellOpen(cert)
default:
explorer := filepath.Join(os.Getenv("WINDIR"), "explorer.exe")
cmd := exec.Command(explorer)
// explorer needs exactly: /select,"C:\path with spaces\file.pdf" — Go's
// default argument quoting breaks that, so set the raw command line.
cmd.SysProcAttr = &syscall.SysProcAttr{CmdLine: fmt.Sprintf(`"%s" /select,"%s"`, explorer, cert)}
if err := cmd.Start(); err != nil {
return err
}
go cmd.Wait() // explorer exits 1 even on success; don't treat as error
return nil
}
}