What does CreateFile(“CONIN$” ..) do?

2019-04-25 12:05发布

问题:

I was hacking away the source code for plink to make it compatible with unison.

If you don't know, unison is a file synchronization tool, it runs an "ssh" command to connect to a remote server, but there's no ssh.exe for windows; there's plink, which is very close but not close enough (it doesn't behave like unison expects it to), so people usually make wrappers around it, like this one.

one of the problems is that unison expects the password prompt to print to stderr (but plink prints it to stdout, and causes unison to be confused), so I thought, well, should be simple enough, hack my thru plink's code and make it print the prompt to stdout. so I hacked my way through and did that.

Next problem: I can't respond to the prompt!! no matter what I type, it has no effect.

the code for getting input is roughly like this:

hin = GetStdHandle(STD_INPUT_HANDLE);
....
r = ReadFile(hin, .....);

I'm not sure why it's done this way, but I'm not an expert in designing command line tools for windows, so what do I know! But I figure something is missing in setting up the input handle.

I looked at the source code for the above wrapper tool and I see this: hconin=CreateFile("CONIN$",GENERIC_READ|GENERIC_WRITE,FILE_SHARE_READ,0,OPEN_EXISTING,0,0)

and I try it (just for the heck of it)

hin=CreateFile("CONIN$",GENERIC_READ|GENERIC_WRITE,FILE_SHARE_READ,0,OPEN_EXISTING,0,0);
....
r = ReadFile( hin ...... )

and surprisingly it works! I can now respond to the prompt!

Why is this? what is "CONIN$"? and why is it different from the STD_INPUT_HANDLE?

I can sort of "guess" that FILE_SHARE_READ and OPEN_EXISTING are playing a role in this (since ssh is being run from within another process), but I want to understand what's going on here, and make sure that this code doesn't have some unwanted side effects or security holes or something scary like that!

回答1:

CONIN$ is the console input device. Normally, stdin is an open file handle to this, but if stdin is redirected for some reason, then using CONIN$ will allow you to get access to the console despite the redirection. Reference.