2

I am creating a console application for read and write. I have created my screen buffer for reading and writing using "CreateConsoleScreenBuffer". I am to write to the console but unable to read the input from the user. I have added the code.

#include <windows.h>
#include <stdio.h>

#define BUFSIZE 4096
int main(void)
{
   CHAR chBuf[BUFSIZE];
   DWORD dwRead, dwWritten,dwFlags,fdwMode;
   HANDLE hWrite;
   WORD wOldColorAttrs,len;
   SECURITY_ATTRIBUTES saAttr;
   FILE *FD = NULL;
   FD = fopen ( "temp7.txt","w+");

   AllocConsole();
   LPSTR lpszPrompt1 = "********** Hi I am here ************* \n";

   hWrite = CreateConsoleScreenBuffer(GENERIC_READ|GENERIC_WRITE,FILE_SHARE_WRITE|FILE_SHARE_READ,NULL,CONSOLE_TEXTMODE_BUFFER,NULL);

   SetConsoleActiveScreenBuffer(hWrite);

   GetConsoleMode(hWrite,&dwFlags);

   fdwMode = dwFlags & ~(ENABLE_LINE_INPUT | ENABLE_ECHO_INPUT);
   if (!SetConsoleMode(hWrite, fdwMode))
   {
      MessageBox(NULL, TEXT("SetConsoleMode"), TEXT("Console Error"), MB_OK);
      return 1;
   }

   fprintf(FD," ********** Flag for Get Console is %d ************ \n",dwFlags);

   if ( WriteConsole(hWrite,lpszPrompt1,strlen(lpszPrompt1),len,NULL)!= 0)
   {
    fprintf(FD," ********** Buffer Written %d and %d ************* \n",GetLastError(),hWrite);
    MessageBox(NULL, TEXT("SetConsoleMode"), TEXT("Write Error"), MB_OK);
        return 1;
   }
   for (;;)
   {
    if ( ReadFile(hWrite,chBuf,255,&dwRead,NULL)!= 0 )    // not overlapped
    {
       fprintf(FD," ********** Error Receiving User Buffer %d and %d ************* \n",GetLastError(),hWrite);
       break;
    }
    fprintf(FD," ********** Following is the Buffer (%d) Received from user ************* \n",dwRead);
    fprintf(FD," %s", chBuf);
    if( chBuf[0]== 'q')
    {
        fprintf(FD," ********** User Pressed Quit************* \n");
        break;
    }

    chBuf[0]='\0';
   }
   getch();
   return 0;
}

Following is the output:

 ********** Flag for Get Console is 3 ************ 
 ********** Following is the Buffer (0) Received from user ************* 
 # ********** Following is the Buffer (0) Received from user ************* 

Please help

~ Johnnie

Davide Piras
  • 43,984
  • 10
  • 98
  • 147
Johnnie
  • 235
  • 5
  • 13

2 Answers2

2

Thanks for the inputs. I have found out the Error. CreateConsoleScreenBuffer Can be used for output buffers. In addition, windows provides a way to create a file with CONIN$ and cONOUT$ which is nothing but the console input and output buffer files. This buffer will be of process console's even if standard In and OUT are redirected.

 hRead=CreateFile("CONIN$",GENERIC_READ|GENERIC_WRITE,FILE_SHARE_READ,0,OPEN_EXISTING,0,0);
 hWrite=CreateFile("CONOUT$",GENERIC_READ|GENERIC_WRITE,FILE_SHARE_READ,0,OPEN_EXISTING,0,0);

Regards Johnnie

Johnnie
  • 235
  • 5
  • 13
0

you should null-terminate the string entered by the user, ReadFile doesn't do this automatically

 chBuf[ dwRead ] = 0;
stijn
  • 34,664
  • 13
  • 111
  • 163