Home > Software design >  How to launch an external executable on windows in go
How to launch an external executable on windows in go

Time:12-12

I am trying to launch an external executable (msi driver installer) on windows with go. It is referred to with the relative path bin\launchme.msi

As of now, I am using the error screenshot

What is especially confusing to me is that, as you can see on the screenshot, the msi installer created a %SystemDrive% folder in the working directory.
This folder in turn contains a Program Files folder and some more subfolders.

Otherwise, if I simply launch the msi installer by double-clicking it, everything is fine, no error occurs.

Any help with this would be greatly appreciated.

CodePudding user response:

one workaround, for testing, would be to launch that Go program in C:\ as working directory:

// caputred is the captured output from all executed source code
// fnResult contains the result of the executed function
captured, fnResult := cmd.CaptureStandardOut(func() interface{} {
    c := NewCommand("C:\path\to\bin\launchme.msi", cmd.WithWorkingDir("C:\\")
    err := c.Execute()
    return err
})

// prints "hello"
fmt.Println(captured)

CodePudding user response:

It could be, that your .MSI file is using cmd.exe in the background.

If you type echo %my_env_var% in cmd.exe, there are two cases what can happen:

  • If the environment variable my_env_var is defined, it prints it's value
  • if the environment variable my_env_var is not defined, the built-in echo command of cmd.exe prints %my_env_var% literally.

Every process inherits the environment variables of it's parent process. If a process is started from the (Windows) file explorer (explorer.exe), it inherits the system settings for environment variables.

It seems like your library is dropping some environment variables like SystemDrive when starting a new process from the executable bin\launchme.msi.

Try to use import("os/exec") from the Go standard library https://pkg.go.dev/os/exec .

For more information about environment variable in Microsoft Windows see for example: https://docs.microsoft.com/en-us/windows/win32/procthread/environment-variables

Tip: You can use Process Explorer (procexp.exe) to see the environment variables of running processes, by doing this:

  • right-click on the process you are intrested in, to open the context menu
  • Click on Preferences
  • In the new window click on the Tab Environment.
  • Related