how to use a console window in a win32 application..-- not console application 
Author Message
 how to use a console window in a win32 application..-- not console application

hi,

i read about that it is possible to use a console window to display text
output  in a  win32 application.   Could anyone please give me a clue how ?
I simply put printf()  in the code.  The programmes is successfully built,
but printf() seems to do nothing.

thanks..

ray



Mon, 07 May 2001 03:00:00 GMT  
 how to use a console window in a win32 application..-- not console application

Quote:

>i read about that it is possible to use a console window to display text
>output  in a  win32 application.   Could anyone please give me a clue how ?
>I simply put printf()  in the code.  The programmes is successfully built,
>but printf() seems to do nothing.

The code between the braces ( {} ) was taken from a post of a certain
self-described "broken-down Hungarian half-breed" who sometimes hangs out in
the kernel group: <BWG>

#include <io.h>
#include <stdio.h>

void DoIt()
{
 int fd;
 FILE *fp;

 AllocConsole();

 fd = _open_osfhandle( (long)GetStdHandle( STD_OUTPUT_HANDLE ), 0);
 fp = _fdopen( fd, "w" );

 *stdout = *fp;
 setvbuf( stdout, NULL, _IONBF, 0 );

 printf("This is a test");

Quote:
}

Regards,
Will


Mon, 07 May 2001 03:00:00 GMT  
 how to use a console window in a win32 application..-- not console application
Surf on over to my WWW site at http://www.halcyon.com/ast/swdev.htm and
grab the source to my ConGUI sample.  It has code to hook up C++
stream I/O and C stdio to a console from a GUI app.

HTH

: hi,
:
: i read about that it is possible to use a console window to display text
: output  in a  win32 application.   Could anyone please give me a clue how ?
: I simply put printf()  in the code.  The programmes is successfully built,
: but printf() seems to do nothing.
:
: thanks..
:
: ray
:
:

--
--
/*  Andrew  */
WWW: http://www.halcyon.com/ast        



Mon, 07 May 2001 03:00:00 GMT  
 how to use a console window in a win32 application..-- not console application

Two ways:  (1) If the app is created as a console application, it has a main()
and it will run in a DOS window; (2) use the console API as suggested in
the following code

================================
#pragma message("Compiling using Win32 Console API")

static HANDLE consoleHandle = INVALID_HANDLE_VALUE;
static BOOL triedToCreate  = FALSE;

static void
consoleInit ()
{
 consoleHandle = CreateConsoleScreenBuffer
 (
  GENERIC_READ|GENERIC_WRITE,
  FILE_SHARE_READ|FILE_SHARE_WRITE,
  NULL,                       // security attributes ignored
  CONSOLE_TEXTMODE_BUFFER,    // dwbuffertype (only 1 useable!)
  NULL                       // screen buffer data (reserved)
 );
 if (SetConsoleActiveScreenBuffer(consoleHandle) == FALSE)
 {
  (void) CloseHandle(consoleHandle);
  consoleHandle = INVALID_HANDLE_VALUE;
  showstring("\n%%Problem opening Win32 console; using column output\n\n");
 }
 triedToCreate = TRUE;

Quote:
}

void
gotoxy (int x, int y)
{
 if (triedToCreate == FALSE)
  consoleInit();

 if (consoleHandle != INVALID_HANDLE_VALUE)
  (void) SetCursorPos(x, y);
 else
    showstring("\n");

Quote:
}

void
clrscr ()
{
 int line;
 if (triedToCreate == FALSE)
  consoleInit();

 for (line = 0; line < 25; line++)
  showstring("\n");

Quote:
}

static int
getcharacter ()
{
 int result;
 if (consoleHandle != INVALID_HANDLE_VALUE)
 {
  char temp[8];
  DWORD nNumberOfCharsToRead = 1;
  DWORD nNumberOfCharsRead = 0;
  (void) ReadConsole
  (
   consoleHandle, temp, nNumberOfCharsToRead,
   &nNumberOfCharsRead, NULL
  );
  result = (nNumberOfCharsRead >= 1);
 }
 else
 {
  (void) getch();
  result = 1;
 }
 return result;

Quote:
}

static void
showstring (const char * str)
{
 if (consoleHandle != INVALID_HANDLE_VALUE)
 {
  DWORD nNumberOfCharsToWrite = (DWORD) strlen(str);
  DWORD nNumberOfCharsWritten = 0;
  (void) SetConsoleTextAttribute
  (
   consoleHandle,
   FOREGROUND_BLUE|FOREGROUND_GREEN|FOREGROUND_RED|FOREGROUND_INTENSITY
  );
  (void) WriteConsole
  (
   consoleHandle, str, nNumberOfCharsToWrite,
   &nNumberOfCharsWritten, NULL
  );
 }
 else
  printf(str);

Quote:
}

================================

Quote:

> hi,

> i read about that it is possible to use a console window to display text
> output  in a  win32 application.   Could anyone please give me a clue how ?
> I simply put printf()  in the code.  The programmes is successfully built,
> but printf() seems to do nothing.

> thanks..

> ray

--
Chris Ahlstrom
major league software, llc
1259 Center Lake Dr.
Mt. Pleasant, SC 29464-7419
(843)849-0985

They don't let Bill Gates code anymore.



Mon, 07 May 2001 03:00:00 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. Console o/p from windows application (not in new console)

2. Starting a console application without the console window

3. Console application command window does not close

4. Using Console in Windows Applications

5. Executing a console application without it opening a console

6. Console control in console application

7. console application pauses when mouse clicks inside the console

8. Win32 specific: Calling modal dialog box from a console application

9. #include statement compiler error and Win32 Simple Console Application

10. Icon in a Win32 console application

11. Size of Win32 and Console applications.

12. Iconized C++ Win32 console application.

 

 
Powered by phpBB® Forum Software