Windows invalid handles

710 views
Skip to first unread message

larzc...@gmail.com

unread,
May 13, 2014, 10:30:00 AM5/13/14
to golan...@googlegroups.com
I'm using the godoc.org/code.google.com/p/go.crypto/ssh/terminal package to do some terminal stuff in Windows, and randomly when calling MakeRaw it fails with "The handle is invalid.".

Here's the code I'm using that fails:
termState, err := terminal.MakeRaw(int(os.Stdin.Fd()))
if err != nil {
  log.Fatalln(err)
}

And another example of the same error, but using the GetConsoleMode WinAPI routine:
err := syscall.GetConsoleMode(syscall.Stdin, &mode)
if err != nil {
  log.Fatalln(err)
}

Is there any reason this would fail with the "The handle is invalid." error?

brainman

unread,
May 14, 2014, 12:18:42 AM5/14/14
to golan...@googlegroups.com, larzc...@gmail.com
On Wednesday, 14 May 2014 00:30:00 UTC+10, larzc...@gmail.com wrote:
> ... Is there any reason this would fail with the "The handle is invalid." error?

It might fail with this error, if syscall.Stdin is not "windows console". For example:

U:\>type main.go
package main

import (
        "fmt"
        "log"
        "syscall"
)

func main() {
        var mode uint32
        err := syscall.GetConsoleMode(syscall.Stdin, &mode)
        if err != nil {
                log.Fatalln(err)
        }
        fmt.Printf("all good")
}

U:\>go build main.go

U:\>.\main.exe
all good
U:\>echo abc | .\main.exe
2014/05/14 14:14:15 The handle is invalid.

U:\>

Alex

larzc...@gmail.com

unread,
May 14, 2014, 10:49:09 AM5/14/14
to golan...@googlegroups.com, larzc...@gmail.com
Interesting, I didn't even know you could pipe input in the Windows console. Thank you Alex.

Konstantin Khomoutov

unread,
May 14, 2014, 10:54:49 AM5/14/14
to larzc...@gmail.com, golan...@googlegroups.com
It does even have redirections like

foo <in.txt >out.txt 2>errors.txt

and

foo 2>&1 >mixed.txt

And you even have the null device, so

foo >NUL

and

foo <NUL

are there.

(there's nothing like /dev/zero though.)

The overall syntax is far away from that of the /bin/sh but otherwise
many things are there.
Reply all
Reply to author
Forward
0 new messages