Nick
Nick

Reputation: 21

No stdout from executables after pinvoke of go c-shared dll via Windows Powershell

Here's a bizarre one - After pinvoke of a function from a golang c-shared dll via Powershell, subsequent executions of executables (e.g. hostname.exe, ipconfig, etc) show no output to stdout. This only happens when powershell is the one invoking the dll functions (python on Windows does not reproduce this issue).

What did I do?

I cross-compiled the simplest c-shared dll I could for Windows with one exported function (HelloWorld). I then invoked the dll from Powershell.

The go code was:

package main

import "C"
import (
        "fmt"
)

//export HelloWorld
func HelloWorld() {
        fmt.Println("Hello World")
}

func main() {}

The build command (from linux) used was:

GOARCH=amd64 GOOS=windows CGO_ENABLED=1 CC=x86_64-w64-mingw32-gcc go build -v -buildmode=c-shared -o helloworld.dll .

I then copied the resultant .dll to Windows and invoked the HelloWorld() function using the following Powershell script:

$signature = @"
[DllImport("c:\\cshared\\helloworld.dll")]
public static extern void HelloWorld();
"@

Add-Type -MemberDefinition $signature -Name World -Namespace Hello

[Hello.World]::HelloWorld()

What did I expect to see?

I expected the dll's HelloWorld() function to be invoked successfully (causing the text "Hello World" to be written to the console) and then I expected to be able to use my powershell prompt to execute further console commands such as "hostname" or "ipconfig" etc.

What did I see instead?

This is really odd. The HelloWorld() function is indeed executed successfully and I see the text "Hello World" on the console. However, after the function has been invoked, I am no longer able to see any console output from cmd.exe commands in my powershell window. For example, calling "hostname" just returns with no output and the same happens with "ipconfig".

e.g. initially hostname.exe works and provides output, but after executing the cshared dll I am unable to see any further output from hostname..exe on the console:

PS C:\cshared> hostname
nick-host
PS C:\cshared> hostname
nick-host
PS C:\cshared> .\helloworld.ps1
Hello World
PS C:\cshared> hostname
PS C:\cshared> ipconfig
PS C:\cshared> hostname

If I pipe the output of hostname.exe to a file (e.g. "hostname > out.txt") then I can confirm that the output is present and the command is succeeding (same happens if you pipe the executable commands to "Out-Host" cmdlet)....so it seems like executing the c-shared dll somehow causes stdout to be redirected or not be shown properly? or something about the encoding of the PS session to break?

I tried removing the fmt.Println from the go code to see if that helps, but the issues is still the same. I am also only seeing this with Powershell (if I do the same exact experiment but invoke the helloworld.dll from python on Windows, I do not see any issue in the shell afterwards).

Any ideas?

Upvotes: 1

Views: 238

Answers (1)

Nick
Nick

Reputation: 21

This has been identified by the console owner for Windows as a bug in golang itself. The go issue thread is here: https://github.com/golang/go/issues/44876

Upvotes: 1

Related Questions