Could you please explain me the difference?

Hi everyone,

I am a beginner in windows programming.Since I am self learning with a book which I have purchased recently so I cannot find a solution to this silly doubt.Here is my doubt,
I am using Visual Studio 2015 as per the suggestion of an expert in this forum for building Win32 applications.There are two modes for creating a win32 application.
One is console and the other is windows. I know about console , But
a window, message box, etc., can be created in both console and windows mode so what is the difference between them.

Does it make any difference when we create a windows application on console?

Thank you in advance
Last edited on
A console application will spawn a console. A windows application doesn't. Also, there's a byte in the PE header that's different that defines whether the application is a console app or a windows application. This is contained in the PE NT specific field within the PE header. 0x2 for GUI, 0x3 for console. There's an image here I found on Google (you can also use a hex editor to view this yourself): https://www.simple-talk.com/blogbits/simon.cooper/PE%20Headers%20annotated.png

But, basically a console app doesn't have a user interface. A windows app does.
Last edited on
I don't know about Visual Studio 2015 (that's 7 years younger than the version I use), but when a GUI app is specified within the IDE it automatically adds various linker settings to the compiler command line strings that it feeds to the compiler/linker. So if you set up your projects as console apps and start getting 'unresolved external' linker errors when you call GDI or user functions, you'll know why.

Also, a console is actually a GUI app. An unusual one with unusual properties to bw sure, but a GUI app nontheless.

Further, from a GUI app that doesn't have a console associated with the GUI process in any way, you can create one very easily with the Api function AllocConsole().
Last edited on
Also, a console is actually a GUI app


I wouldn't say this was true. If you run the console app from a remote shell then there's no GUI.

I wouldn't say this was true.


Here's the function to return the HWND of the console window....


GetConsoleWindow Function

Retrieves the window handle used by the console associated with the calling process.

HWND WINAPI GetConsoleWindow(void);


If it looks like a duck and quacks like a duck, its a duck. :)


The return value is a handle to the window used by the console associated with the calling process or NULL if there is no such associated console.


Would this be NULL if running the application from a shell? I've never actually tried it.
Last edited on
Don't know CoderJames. I don't work with networked stuff much, so you likely know more about this than I. I'm stretching a bit here, but I suppose, conceptually, a console process is logically distinct from a GUI process. I know a process can be started without a console, so I wouldn't expect in such a process, if a call was made to obtain a HWND, that one could be assigned to the variable.

But in typical Windows desktop apps such as the OP is speaking of running locally, when the console is created if that type of app was specified in the project setup, Windows internally executes some kind of CreateWindow() call which generates a HWND which can be obtained, as I said. Also, many other Windows Api functions usually associated with GUI processes can be executed against a console window. For one example, older compilers won't have that GetConsoleWindow() function I discussed above. However, in cases where it is missing, FindWindow() can be called to obtain it instead. And of course, FindWindow() is about as GUI as you can get. And here is a little routine I put together to prevent the user from closing a console window through clicking the [x] in the Title Bar...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
void DisableCloseButton()
{ 
 char szBuffer[256];  
 
 if(GetConsoleTitle(szBuffer,256)) 
 {    
    HWND hConsole=FindWindow(NULL,szBuffer);
    if(hConsole)
    {
       HMENU hSysMenu=GetSystemMenu(hConsole,0);
       DeleteMenu(hSysMenu,6,MF_BYPOSITION);
    }
 }
}


Many other GUI Api functions also can be used against consoles, even TextOut(). With a HWND as obtained above, one can obtain a Handle to a Device Context, and use that function. Now doing that likely won't insert the text printed to the console's underlying screen buffer, but it more or less proves my point.

So in a practical sense, a console is that alternate byte you spoke of in the PE Header, some kind of internal CreateWindow() call to provide the visible console, and the underlying plumbing to provide the Api console functions, e.g., ReadConsoleInput(), WriteConsole(), Set ConsoleMode(), etc.
Topic archived. No new replies allowed.