Pelles C forum

C language => Tips & tricks => Topic started by: golite on October 01, 2013, 02:55:58 am

Title: Windows App with Console Running [SOLVED]
Post by: golite on October 01, 2013, 02:55:58 am
Where do I apply the following POLINK option? This is copied from the help files. I need to understand it.

To be able to write information to the screen the process must have access to a console window. This is normally not the case for a GUI program, which is expected to create it's own windows. You can make sure the process have access to a console window by adding the following POLINK options:

-subsystem:console -entry:_WinMainCRTStartup. You should only use this in debug builds.

Title: Re: Windows App with Console Running
Post by: TimoVJL on October 01, 2013, 03:31:58 pm
Example in source code:
Code: [Select]
#define WIN32_LEAN_AND_MEAN
#include <windows.h>

#define _DEBUG

#ifdef _DEBUG
#include <stdio.h>
#pragma comment(linker, "-subsystem:console")
#pragma comment(linker, "-entry:WinMainCRTStartup")
#endif

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
#ifdef _DEBUG
printf("to console\n");
#endif
MessageBox(0, "Test1", "Test1", MB_OK);
return 0;
}
Title: Re: Windows App with Console Running
Post by: golite on October 02, 2013, 03:25:20 pm
Thank you very much. That worked just fine.
Title: Re: Windows App with Console Running
Post by: Persay on March 04, 2014, 10:18:48 am
Great you found out and shared ! Can you also update the thread title prepending [solved] :)
Title: Re: Windows App with Console Running [SOLVED]
Post by: henrin on November 05, 2014, 03:55:49 pm
In a recent program I have written:

int __stdcall WinMain(HINSTANCE hI, HINSTANCE hP, LPSTR CmdLine, int CmdShow)
{
   AllocConsole() ;
   ...
   _cprintf(fmt, ...) ;

and it worked.
I do not know why  ::)
Title: Re: Windows App with Console Running [SOLVED]
Post by: frankie on November 05, 2014, 05:31:34 pm
In a recent program I have written:

int __stdcall WinMain(HINSTANCE hI, HINSTANCE hP, LPSTR CmdLine, int CmdShow)
{
   AllocConsole() ;
   ...
   _cprintf(fmt, ...) ;

and it worked.
I do not know why  ::)
Yes these are console output functions that directly write to the console window, so they always work.
The other method, using the standard stream functions (fprintf, etc) let you use all features of a consolle (redirection, etc).