Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to hide the console window of a C program?

Tags:

c

window

hide

I've been looking around but I couldn't find the solution to my problem, even with some supposedly solved problems that resemble mine.

I want to hide the console window when my C program runs.

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <windows.h>
#define _WIN32_WINNT 0x0500

int main(){   
    HWND hWnd = GetConsoleWindow();
    ShowWindow( hWnd, SW_MINIMIZE );  //won't hide the window without SW_MINIMIZE
    ShowWindow( hWnd, SW_HIDE );
}

This is what I tried but the compiler gives me

initialization makes pointer from integer without a cast

and the fatal one which actually stops the compiling:

undefined reference to 'GetConsoleWindow'

PS: I've checked wincon.h and the GetConsoleWindow() function is defined.

like image 461
Athropos Avatar asked Aug 04 '12 21:08

Athropos


People also ask

How do you hide a console window?

"SW_HIDE" hides the window, while "SW_SHOW" shows the window.

How do I stop the console window from closing in C++?

Before the end of your code, insert this line: system("pause"); This will keep the console until you hit a key. It also printed "Press any key to continue . . ." for me.

What is Mwindows?

-mwindows. This option is available for Cygwin and MinGW targets. It specifies that a GUI application is to be generated by instructing the linker to set the PE header subsystem type appropriately.


1 Answers

Your

#define _WIN32_WINNT 0x0500

(which is needed to use GetConsoleWindow - see the documentation) must be before

#include <windows.h>

That #define is used by windows.h to know which version of Windows you are targeting (and thus which declarations it has to provide/which additional fields it has to add to structures/other magic that may be related to that linker error); if you define it after you include windows.h it will be useless.

like image 61
Matteo Italia Avatar answered Oct 09 '22 13:10

Matteo Italia