NO

Author Topic: Problem with AllocConsole API function  (Read 10005 times)

skirby

  • Guest
Problem with AllocConsole API function
« 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:
Code: [Select]
#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.

JohnF

  • Guest
Re: Problem with AllocConsole API function
« Reply #1 on: April 06, 2007, 07:19:08 PM »
Here's one way.

Code: [Select]
#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

skirby

  • Guest
Re: Problem with AllocConsole API function
« Reply #2 on: April 06, 2007, 08:21:39 PM »
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.

JohnF

  • Guest
Re: Problem with AllocConsole API function
« Reply #3 on: April 06, 2007, 09:26:10 PM »
No I don't sorry. Just thought I'd help out with some working code.

John

Offline Pelle

  • Administrator
  • Member
  • *****
  • Posts: 2266
    • http://www.smorgasbordet.com
Re: Problem with AllocConsole API function
« Reply #4 on: April 10, 2007, 04:33:53 PM »
Flush the output stream...
/Pelle

skirby

  • Guest
Re: Problem with AllocConsole API function
« Reply #5 on: April 12, 2007, 10:57:28 PM »
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  ;)

Offline Pelle

  • Administrator
  • Member
  • *****
  • Posts: 2266
    • http://www.smorgasbordet.com
Re: Problem with AllocConsole API function
« Reply #6 on: April 13, 2007, 02:24:41 PM »
This is by design. Use a non-standard feature, and you will get a different result at some point...
/Pelle

severach

  • Guest
Re: Problem with AllocConsole API function
« Reply #7 on: April 21, 2007, 03:39:19 AM »
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.