Skip to content

Instantly share code, notes, and snippets.

@randy3k
Created January 28, 2018 05:36
Show Gist options
  • Select an option

  • Save randy3k/c2de86cf23312c0fdd505522c73240b2 to your computer and use it in GitHub Desktop.

Select an option

Save randy3k/c2de86cf23312c0fdd505522c73240b2 to your computer and use it in GitHub Desktop.
use PeekNamedPipe to check bytes available
#include <windows.h>
#include <stdio.h>
#include <stdbool.h>
#define BUFSIZE 1024
int main(int ac, char** av) {
HANDLE h = GetStdHandle(STD_INPUT_HANDLE);
DWORD rd;
CHAR chBuf[BUFSIZE];
DWORD dwRead;
bool bSuccess = FALSE;
while(1) {
if (!PeekNamedPipe(h, NULL, 0, NULL, &rd, NULL)) {
printf("eof\n");
return 1;
}
if (rd > 0) {
ReadFile(h, chBuf, BUFSIZE, &dwRead, NULL);
chBuf[dwRead] = '\0';
printf("dwRead: %d\n", dwRead);
printf("read: %s\n", chBuf);
Sleep(1000);
} else {
Sleep(1000);
}
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment