Here's one way.
#include <stdio.h>
#include <io.h>
#include <windows.h>
#include <fcntl.h>
void RedirectStd(void);
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpszCmdLine, int nCmdShow)
{
RedirectStd();
printf("\nType any key to continue...\n");
getchar();
return 0;
}
void RedirectStd(void)
{
int hCrt = -1;
FILE *hf = NULL;
int i = -1;
AllocConsole();
hCrt = _open_osfhandle((long)GetStdHandle(STD_OUTPUT_HANDLE), _O_TEXT);
hf = _fdopen(hCrt, "w");
*stdout = *hf;
i = setvbuf(stdout, NULL, _IONBF, 0);
printf("stdout//\n");
hCrt = _open_osfhandle((long)GetStdHandle(STD_INPUT_HANDLE), _O_TEXT);
hf = _fdopen(hCrt, "r");
*stdin = *hf;
i = setvbuf(stdin, NULL, _IONBF, 0);
printf("stdin//\n");
hCrt = _open_osfhandle((long)GetStdHandle(STD_ERROR_HANDLE), _O_TEXT);
hf = _fdopen(hCrt, "w");
*stderr = *hf;
i = setvbuf(stderr, NULL, _IONBF, 0);
printf("stderr//\n");
}
John