How to hide the console window of a C program? - c

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.

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.

Related

#include <windows.h> causes errors

I'm trying to use GetTickCount() from the windows.h header file. Right now my code looks like this:
#include <stdio.h>
#include <string.h>
#include "declarations.h"
#define INPUTBUFFER 400 * 6
#define START_POS "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1"
#include <windows.h>
void interface() {
.....
.....
}
The compiler returns 2 errors:
Expected '{' before '(' token (pointing to the '(' after "interface"
2 or more data types in declaration specifiers (pointing to "interface")
When I comment out #include < windows.h >, the interface function works just fine. I don't even have GetTickCount() in my code yet. I checked the gcc path to make sure windows.h is there, so I'm not sure why I'm getting this error.
As RbMm points out in a comment, the windows headers contain "#define interface struct", so the compiler sees your code as "void struct() { ....}" which is not valid C++
You need to either rename your function (probablly the better option) or add a "#undef interface" before your function.

How to change console program for unicode support in windows?

The following program can be compiled using msvc or mingw. However, the mingw version cannot display unicode correctly. Why? How can I fix that?
Code:
#include <stdio.h>
#include <windows.h>
#include <io.h>
#include <fcntl.h>
int wmain(void)
{
_setmode(_fileno(stdout), _O_U16TEXT);
_putws(L"哈哈哈");
system("pause");
return 0;
}
Mingw64 Compile Command:
i686-w64-mingw32-gcc -mconsole -municode play.c
MSVC Compiled:
Mingw Compiled:
Edit:
After some testing, the problem seems not causing by mingw. If I run the program directly by double clicking the app. The unicode string cannot be displayed correct either. The code page however, is the same, 437.
It turns out the problem is related to console font instead of the compiler. See the following demo code for changing console font.
This is happening because of missing #define UNICODE & #define _UNICODE . You should try adding it along with other headers. The _UNICODE symbol is used with headers such as tchar.h to direct standard C functions such as printf() and fopen() to the Unicode versions.
Please Note - The -municode option is still required when linking if Unicode mode is used.
After doing some research, it turns out the default console font does not support chainese glyphs. One can change the console font by using SetCurrentConsoleFontEx function.
Demo Code:
#ifdef _MSC_VER
#define _CRT_SECURE_NO_WARNINGS
#endif
#include <stdio.h>
#include <io.h>
#include <fcntl.h>
#include <windows.h>
#define FF_SIMHEI 54
int main(int argc, char const *argv[])
{
CONSOLE_FONT_INFOEX cfi = {0};
cfi.cbSize = sizeof(CONSOLE_FONT_INFOEX);
cfi.nFont = 0;
cfi.dwFontSize.X = 8;
cfi.dwFontSize.Y = 16;
cfi.FontFamily = FF_SIMHEI;
cfi.FontWeight = FW_NORMAL;
wcscpy(cfi.FaceName, L"SimHei");
SetCurrentConsoleFontEx(GetStdHandle(STD_OUTPUT_HANDLE), FALSE, &cfi);
/* UTF-8 String */
SetConsoleOutputCP(CP_UTF8); /* Thanks for Eryk Sun's notice: Remove this line if you are using windows 7 or 8 */
puts(u8"UTF-8你好");
/* UTF-16 String */
_setmode(_fileno(stdout), _O_U16TEXT);
_putws(L"UTF-16你好");
system("pause");
return 0;
}

Setting Console Font in C on Windows 10

I am trying to change the size of console font in my console application during runtime.
Looking into the Windows documentation, I found the following functions:
GetCurrentConsoleFont (and GetCurrentConsoleFontEx)
and SetCurrentConsoleFontEx.
CONSOLE_FONT_INFO prevFontInfo = {sizeof(prevFontInfo)};
GetCurrentConsoleFont( GetStdHandle(STD_OUTPUT_HANDLE), FALSE, &prevFontInfo
);
CONSOLE_FONT_INFO fontInfo = {0};
//fontInfo.cbSize = sizeof(prevFontInfo);
fontInfo.dwFontSize.X = 30;
fontInfo.dwFontSize.Y = 70;
//fontInfo.FontWeight = 700;
SetCurrentConsoleFontEx( GetStdHandle(STD_OUTPUT_HANDLE), FALSE, &fontInfo );
... (Some Irrelevant Code Here)
SetCurrentConsoleFontEx( GetStdHandle(STD_OUTPUT_HANDLE), FALSE,
&prevFontInfo );
Currently I am getting the following warnings:
implicit declaration of function 'GetCurrentConsoleFont'
and
implicit declaration of function 'SetCurrentConsoleFont'
I tried using GetConsoleFontEx (the reason FontWeight and cbSize are commented out) to no prevail. I used CONSOLE_FONT_INFOEX as well, which resulted in the following error:
Unkown type name 'CONSOLE_FONT_INFOEX'
I read that it is necessary to use the following:
#define _WIN32_WINNT 0x0500
I also tried several variations of this (0x0502, 0x0600, ect.), but nothing seemed to change the warnings/errors.
I include the following windows headers, and I compile with -lkernel32.
#define _WIN32_WINNT 0x0500
#include <windows.h>
#include <Wincon.h>
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <time.h>
I wish to change the font size in C for the Windows terminal at runtime.
How do I do that? Why is this not working?

Sleep | warning implicit declaration of function `sleep'?

I am learning C.
In this program
I use sleep function to slowdown a count down.
My text book doesn't specify a library I should include to use the sleep function.
So I use it without including any special library for it and it works.
But it gives me this warning message in codeblocks.
I tried to include <windows.h> but still the same warning message appears.
warning D:\Project\C language\trial8\trial8.c|19|warning: implicit
declaration of function `sleep'|
And here is my code.
#include <stdio.h>
int main()
{
int start;
do
{
printf("Please enter the number to start\n");
printf("the countdown (1 to 100):");
scanf("%d",&start);
}
while(start<1 || start>100);
do
{
printf("T-minus %d\n",start);
start--;
sleep(3000);
}
while(start>0);
printf("Zero!\n Go!\n");
return(0);
}
I want to know what does the warning message mean? How important is it? Is there anything that I should do about it? Note that the program works anyway.
The issue is in the libraries (header files):
on Windows:
#include <windows.h> and Sleep(1000); => 1000 milliseconds
on Linux:
#include <unistd.h> and sleep(1); => 1 second
The function sleep is not part of C programming language. So, C compiler needs a declaration/prototype of it so that it can get to know about about number of arguments and their data types and return data type of the function. When it doesn't find it, it creates an Implicit Declaration of that function.
In Linux, sleep has a prototype in <unistd.h> and in windows, there is another function Sleep which has a prototype in <windows.h> or <synchapi.h>.
You can always get away with including header, if you explicitly supply the prototype of the function before using it. It is useful when you need only few functions from a header file.
The prototype of Sleep function in C on windows is:
VOID WINAPI Sleep(_In_ DWORD dwMilliseconds);
Remember, it is always a good practice to supply the prototype of the function being used either by including the appropriate header file or by explicitly writing it. Even, if you don't supply it, compiler will just throw a warning most of the time and it will make an assumption which in most cases will be something that you don't want. It is better to include the header file as API might change in future versions of the Library.
Windows doesn't have the sleep function. Instead, it has Sleep, which takes the number of milliseconds to sleep:
VOID WINAPI Sleep(
_In_ DWORD dwMilliseconds
);
You'll need to either #include <windows.h> or #include <synchapi.h>, depending on the version of Windows you're running. See MSDN for more details.
Update in 2022:
As it is stated on the Linux man page here we need to include unistd.h and should do fine for all OS.
#include <stdio.h>
#include <unistd.h>
int main()
{
sleep(1); /* sleep for 1 second*/
printf("END\n");
return 0;
}
To make it more cross-platform, try this:
#ifdef _WIN32
#include <Windows.h>
#else
#include <unistd.h>
#endif

Initializer is not a constant error

sorry if this has been asked over and over but i just don't get what's wrong with this C code, since it was compiling with no problems until someday it started complaining about "C2009: Initializer is not a constant" in lines 9 and 10 of this header:
// CONIO2.H
#ifndef CONIO2_H_INCLUDED
#define CONIO2_H_INCLUDED
#ifndef _WINDOWS_
#include <windows.h>
#endif
void clrscr(void) {
int Written = 0;
COORD ord;
ord.X = 0;
ord.Y = 0;
FillConsoleOutputCharacter(GetStdHandle(STD_OUTPUT_HANDLE), 32, 80 * 25, ord, &Written);
}
//MAIN.C
#include <stdio.h>
#include <conio2.h>
Edit: I found the error. I was using the Eclipse CDT plugin for developing C applications but it wasn't setting up the path correctly. I had to play with the configs to make it work, but thanks anyways!
COORD ord;
You are missing the definition of the COORD type alias. My guess is you are not including the right header: Wincon.h
See here for the requirements (the header to include) to use the COORD type alias:
http://msdn.microsoft.com/en-us/library/windows/desktop/ms682119(v=vs.85).aspx
EDIT: moreover you seems to have an issue with your #ifndef directives: there are two #ifndef but only one #endif in your header. For each #ifndef you need an #endif. And are you sure you want to include windows.h only when _WINDOWS_ is not defined?
This link describes the error along-with some examples. It may help you. As per the link, the compiler initializes non-automatic variables at the start of the program and the values they are initialized with must be constant. http://msdn.microsoft.com/en-us/library/t801az8a(v=vs.80).aspx
Your code has several pre-processor oddities. The below code works well on a standard C compiler for Windows. Please note that VC++ is not a standard C compiler, so it could toss all kinds of strange errors at you.
// CONIO2.H
#ifndef CONIO2_H_INCLUDED
#define CONIO2_H_INCLUDED
#include <windows.h>
void clrscr (void)
{
DWORD Written = 0;
COORD ord;
ord.X = 0;
ord.Y = 0;
FillConsoleOutputCharacter (GetStdHandle(STD_OUTPUT_HANDLE),
32,
80 * 25,
ord,
&Written);
}
#endif /* CONIO2_H_INCLUDED */
//MAIN.C
#include <stdio.h>
#include "conio2.h"

Resources