Pelles C forum
C language => Windows questions => Topic started by: skirby on April 06, 2007, 05:43:25 PM
-
Hello,
I have a small problem with AllocConsole function.
I am used to debug my windows applications with an msdos console.
Like that, I can send traces to the console and see what the program do.
With GCC, it works without problem but I cannot do the same thing with Pelles C.
Here is a small piece of code:
#include <windows.h>
#include <stdio.h>
int WINAPI WinMain (HINSTANCE hThisInstance,
HINSTANCE hPrevInstance,
LPSTR lpszArgument,
int nCmdShow)
{
AllocConsole(); // affiche console
// redirection flux entree/sortie
freopen("conin$", "r", stdin);
freopen("conout$", "w", stdout);
freopen("conout$", "w", stderr);
printf("Test\n");
MessageBox(0, "Can you see the text in the console?", "", 0);
FreeConsole();
system("PAUSE");
return 0;
}
What's the problem?
Thanks in advance and have a nice day.
-
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
-
Hello JohnF,
It is a bit "complicated" but it works :)
I have test my code with Visual Studio 2005 and it works too.
So, do you know why my exemple works with GCC/MinGW and VS 2005 but not with Pelles C?
Thanks.
-
No I don't sorry. Just thought I'd help out with some working code.
John
-
Flush the output stream...
-
Hello Pelle,
You are right, it works if I add fflush(stdout); after my printf.
Could you tell me why I have to flush the output stream whereas there is no need to do that with GCC or Visual Studio 2005?
In any case, thank you for the trick ;)
-
This is by design. Use a non-standard feature, and you will get a different result at some point...
-
The reason it doesn't work is because Pelles uses an internal implementation of the stdio library which is not aware of created command windows. The other two compilers mentioned use MSVCRT or similar Microsoft library which are aware of created command windows. I have a mostly complete include file that makes Pelles use the MSVCRT stdio library natively instead of the Pelles internal stdio library. Try it with another compiler that uses an internal implementation of stdio like Watcom or Borland and it probably won't work right either.