This is the mail archive of the cygwin@cygwin.com mailing list for the Cygwin project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

RE: .*Console.* Functions vs. tty


Just so the list contains the resolution to this in case some one has a
similar problem in the future...

> -----Original Message-----
> From: Corinna Vinschen [mailto:cygwin@cygwin.com]

> According to MSDN you need a handle with GENERIC_READ access to
> call GetConsoleScreenBufferInfo with. That's not necessarily 
> true for an output handle...

Ack, I didn't realize that normally CONOUT$ is attached to STD_OUTPUT_HANDLE
with GENERIC_READ access, but not when run under Cygwin with "tty".

> -----Original Message-----
> From: Christopher Faylor [mailto:cgf@redhat.com]
> You could open a console using 'conin$' as the name or, if it is a
> cygwin program, '/dev/conin'.  Use the handle from that as input for
> console routines.

The handle that is passed to GetConsoleScreenBufferInfo must be a CONOUT$
handle.  It is obscurely documented in the latest CreateFile info on MSDN
that CONOUT$ is attached to the screen buffer.  It does not say the same for
CONIN$.  As a matter of fact, the following code fails with the same error
as before if CONIN$ is used in place of CONOUT$.

So, if you are going to have a CONOUT$ handle to pass to
GetConsoleScreenBufferInfo and that can also be used for output from the
program, it must have both read and write settings as in the code below.
This is an update of my original code fragment that works as intended.  It
functions whether compiled by gcc for cygwin, gcc for MinGW, or MSVC.  And
runs just fine in bash, cmd.exe, or command.com.


#include <windows.h>

int main()
{
  int retval = 0;
  CONSOLE_SCREEN_BUFFER_INFO csbi;
  
  HANDLE file = CreateFile("CONOUT$", GENERIC_READ|GENERIC_WRITE,
FILE_SHARE_READ|FILE_SHARE_WRITE, NULL, OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL, NULL);

  if (file == INVALID_HANDLE_VALUE)
  {
    printf("CreateFile failed: %ld\n", GetLastError());
    retval = 1;
  }
  else if (!GetConsoleScreenBufferInfo(file, &csbi))
  {
    printf("GetConsoleScreenBufferInfo failed: %ld\n", GetLastError());
    retval = 2;
  }
  else
    printf("Success.");

  return retval;
}

--
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple
Bug reporting:         http://cygwin.com/bugs.html
Documentation:         http://cygwin.com/docs.html
FAQ:                   http://cygwin.com/faq/


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]