Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Using the Console from a GUI app

347 views
Skip to first unread message

Nick Shaffner

unread,
Jun 25, 2002, 5:39:15 PM6/25/02
to
Hello there,

I've been trying to write to a console created with AllocConsole() in my C#
GUI app, and everything seems to work ok, except for the fact that nothing I
write actually shows up in the console. I was hoping perhaps someone could
point me to an example of how to use the console from the GUI, or perhaps
tell me what I'm doing wrong:

[DllImport("Kernel32.dll")] public static extern bool AllocConsole();
[DllImport("Kernel32.dll")] public static extern bool FreeConsole();
[DllImport("Kernel32.dll")] public static extern bool SetConsoleTitle(
string ConsoleTitle );
[DllImport("Kernel32.dll")] public static extern bool
SetConsoleTextAttribute( int hConsoleOutput, short wAttributes );
[DllImport("Kernel32.dll")] public static extern bool WriteFile( int hFile,
string lpBuffer, int nNumberOfBytesToWrite, ref int lpNumberOfBytesWritten,
int lpOverlapped);
[DllImport("Kernel32.dll")] public static extern int GetStdHandle( int
nStdHandle );
[DllImport("Kernel32.dll")] public static extern bool
SetConsoleActiveScreenBuffer( int hConsoleOutput );

const int STD_INPUT_HANDLE = -10;
const int STD_OUTPUT_HANDLE = -11;
const int STD_ERROR_HANDLE = -12;

// Here's the code that attempts to write to the console, note that none of
the exceptions are thrown, it's just that nothing happens.
private void Form1_Load(object sender, System.EventArgs e)
{
// Allocate a console
AllocConsole();
SetConsoleTitle( "Test Title" );
int output = 0;
int hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
if( hStdout == -1 )
throw new Exception("Stdout bad");

if( !WriteFile( hStdout, "1234\n", 5, ref output, 0) )
{
throw new Exception("Write File Failed");
}
}


Chad Myers

unread,
Jun 25, 2002, 6:11:14 PM6/25/02
to
There's a large Q article in Microsoft's knowledge base on this
(writing to a console with C# or something).

I don't know the URL but a quick search on MSDN should turn
it up. It has a good example too.

-c

"Nick Shaffner" <ni...@wxp3d.com> wrote in message
news:u5xulCJHCHA.2772@tkmsftngp12...

Nick Shaffner

unread,
Jun 25, 2002, 7:01:53 PM6/25/02
to
Thanks for the information, but I couldn't locate the article you were
referring to on the massive quagmire of information that is MSDN,

As an interesting aside, I've managed to get it working using

int hStdout = CreateFile("CONOUT$",
GENERIC_READ | GENERIC_WRITE, // access mode
FILE_SHARE_READ | FILE_SHARE_WRITE,//int dwShareMode, // share mode
0, // SD
OPEN_EXISTING, // how to create
0, // file attributes
0 // handle to template file
);

(with CreateFile imported of course), but I get a file not found doing:

FileStream ConOut = File.Open( "CONOUT$", FileMode.Open,
FileAccess.ReadWrite, FileShare.ReadWrite);

I'd still like to figure out how to set up the console without using
CreateFile if possible, however.

Nick

Jon Shemitz

unread,
Jul 22, 2002, 8:05:24 PM7/22/02
to
"Nick Shaffner" <ni...@wxp3d.com> wrote in message news:<#FABxwJHCHA.2496@tkmsftngp08>...

> As an interesting aside, I've managed to get it working using
>
> int hStdout = CreateFile("CONOUT$",

> I'd still like to figure out how to set up the console without using
> CreateFile if possible, however.

I'd like to know the same thing - did you ever get anywhere with this?

Jon Shemitz

unread,
Jul 22, 2002, 8:51:52 PM7/22/02
to
> Also check the sample attached.

Works like a champ ... I've attached a rewritten form, that's a bit
easier to plug into arbitrary apps.

--

My Kylix book http://www.midnightbeach.com/kylix
Consulting & contract programming http://www.midnightbeach.com
Stale homeschool resources http://www.midnightbeach.com/hs

GuiConsole.cs
0 new messages