Using standard input and output handle
Compiler: Visual C++ Express Edition 2005
Compiled on Platform: Windows XP Pro SP2
Header file: Standard and Windows
Additional library: Windows Platform SDK
Additional project setting: Set project to be compiled as C
Project -> your_project_name Properties -> Configuration Properties -> C/C++ -> Advanced -> Compiled As: Compiled as C Code (/TC)
Other info: non-CLR or unmanaged
To do: Using standard input and output handle
To show: Handle to standard input and output
// Handle to standard input and output
#include <windows.h>
#include <stdio.h>
#define BUFSIZE 2048
int main(void)
{
char chrBuf[BUFSIZE];
DWORD dwRead, dwWritten;
// standard input and output handles
HANDLE hStdin, hStdout;
BOOL fSuccess;
// Standard output handle
hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
// Standard input handle
hStdin = GetStdHandle(STD_INPUT_HANDLE);
// If something wrong to the handle for standard input or output...
if((hStdout == INVALID_HANDLE_VALUE) || (hStdin == INVALID_HANDLE_VALUE))
// Just exit with error
exit(1);
else
{
printf("Waiting data from standard input:\n");
printf("--EOF to end--\n");
}
// To stop, press end of file characters
for(;;)
{
// Read from standard input, keyboard...
fSuccess = ReadFile(hStdin, chrBuf, BUFSIZE, &dwRead, NULL);
if(!fSuccess || dwRead == 0)
break;
else
printf("ReadFile() is OK\n");
printf("Data to the standard output:\n");
// Write to standard output, console...
fSuccess = WriteFile(hStdout, chrBuf, dwRead, &dwWritten, NULL);
if(!fSuccess)
break;
else
printf("WriteFile() is OK\n");
}
return 0;
}
Output example:
Waiting data from standard input:
--EOF to end--
This is a test string from standard input
ReadFile() is OK
Data to the standard output:
This is a test string from standard input
WriteFile() is OK
Another line of text from standard input lor!
ReadFile() is OK
Data to the standard output:
Another line of text from standard input lor!
WriteFile() is OK
^Z
Press any key to continue . . .