home *** CD-ROM | disk | FTP | other *** search
/ Programming Win32 Under the API / ProgrammingWin32UnderTheApiPatVillani.iso / Chapter4 / 4-5 / ConsGets.c
Encoding:
C/C++ Source or Header  |  2000-07-17  |  1019 b   |  57 lines

  1.  
  2. #include <windows.h>
  3.  
  4. #define MAXBUF    4096
  5.  
  6. DWORD ConsPuts(char *pszString);
  7. DWORD ConsGets(char *pszString, DWORD nLen);
  8.  
  9.  
  10. //
  11. // ConsPuts:
  12. //    Print a string to StdOut on the console
  13. //
  14. DWORD ConsPuts(char *pszString)
  15. {
  16.     DWORD nLen = strlen(pszString);
  17.     DWORD nWritten;
  18.     HANDLE hOutput = GetStdHandle(STD_OUTPUT_HANDLE);
  19.     BOOL bRet;
  20.  
  21.     bRet = WriteConsole(hOutput, pszString, nLen, &nWritten, NULL);
  22.     return (!bRet || (nWritten != nLen)) ? 0 : nLen;
  23. }
  24.  
  25.  
  26.  
  27. //
  28. // ConsGets:
  29. //    Get a string to StdIn on the console
  30. //
  31. DWORD ConsGets(char *pszString, DWORD nLen)
  32. {
  33.     DWORD nRead;
  34.     HANDLE hInput = GetStdHandle(STD_INPUT_HANDLE);
  35.     BOOL bRet;
  36.  
  37.     bRet = ReadConsole(hInput, pszString, nLen, &nRead, NULL);
  38.     return (!bRet) ? 0 : nLen;
  39. }
  40.  
  41.  
  42.  
  43. int main(int argc, char *argv[])
  44. {
  45.     char szBuffer[MAXBUF];
  46.     DWORD nRead;
  47.  
  48.     ConsPuts("Welcome to the your second console program.\r\n");
  49.     nRead = ConsGets(szBuffer, MAXBUF);
  50.     szBuffer[nRead] = '\0';
  51.     ConsPuts(szBuffer);
  52.     return 0;
  53. }
  54.  
  55.  
  56.  
  57.