Starting Chrome on windows needs to be bidirectional (Kernel32)
Closed this issue · 1 comments
Probably need help with this issue
When starting chrome on windows in go with exec.command or go-cmd (github repos), then the application start without any way of signalling (exec.command) or only with one-way signalling (go-cmd), while we needs to signal both ways.
An example is that in linux, we can open chromium and interrupt the software from the terminal, while we can also interrupt the terminal by closing the chromium window that we just opened.
This issue could be solved with the C# project that can be found at https://github.com/NineNineFive/windowsProcessLauncher (private repository - ask @NineNineFive to get access to the repository). The project worked by importing kernel32 dll and setting a ctrlhandler and setting process.enableRaisingEvents to true on a new process
OpenAI's ChatGPT helped me find a solution after i tried for multiple hours to feed it the right set of questions. I ended up with the most simple part of using the exec.Command to open chrome, and then i asked it to wrap it with Graceful shutdown
Took me at least 8 hours to find the right question to ask it.
It came up with this solution that works for windows 10, this solve the issues i had:
/*
Code is generated by ChatGPT, 20 december 2022
Explanation:
This is a Go program that starts a new Chrome browser process and opens the URL "https://google.dk/" in a new window.
The program also sets up a signal handler to gracefully shut down the program when a SIGINT or SIGTERM signal is received.
When a signal is received, the program will kill the Chrome process and exit.
The Chrome process is started using the exec.Command function, which creates and returns an *exec.Cmd struct.
The path to the Chrome executable, along with the URL to open and the --user-data-dir flag, are passed as arguments to the function.
The --user-data-dir flag specifies the location where Chrome should store user data, such as browsing history and bookmarks.
In this case, the user data directory is set to a subdirectory within the local app data directory for the current user.
The cmd.Start function is called to start the Chrome process. The program then sets up a signal handler to listen for
SIGINT and SIGTERM signals, and a goroutine is launched to handle the signals.
When a signal is received, the goroutine will kill the Chrome process and exit the program.
Finally, the program waits for the Chrome process to finish using the cmd.Wait function.
*/
package main
import (
"os"
"os/exec"
"os/signal"
"syscall"
)
func main() {
// Start a new Chrome process
path := "C:\\Program Files\\Google\\Chrome\\Application\\chrome.exe"
//path = "chrome.exe"
cmd := exec.Command(path, "--app=https://google.dk/", "--user-data-dir="+os.Getenv("localappdata")+"/Google/Chrome/InstalledApps/NewCompanyName/NewProjectName")
cmd.Start()
// Set up a signal handler to gracefully shutdown the program
sigs := make(chan os.Signal, 1)
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)
go func() {
<-sigs
cmd.Process.Kill()
os.Exit(0)
}()
// Wait for the process to finish
cmd.Wait()
}
Now i will focus on implementing this into the launcher for windows
Thanks to ChatGPT!