Error in using the GetCurrentConsoleFontEx

Aug 17, 2011 at 8:38am
I was trying to use the GetCurrentConsoleFontEx & the SetCurrentConsoleFontEx functions.
This is my code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27

HANDLE hStdin = GetStdHandle(STD_INPUT_HANDLE); 
HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);

CONSOLE_FONT_INFOEX orig, current;

current.cbSize = 16;

COORD FontSize;
	
FontSize.X = 10;
FontSize.Y = 10;

current.dwFontSize = FontSize;
current.FontWeight = 600;
current.FontFamily = FF_ROMAN;

	
if (GetCurrentConsoleFontEx(hStdin,FALSE,&orig))
	cout << "Got\n";
else
	cout << GetLastError();

if (SetCurrentConsoleFontEx(hStdout,FALSE,&current))
	cout << "\nSet";
else
	cout << endl << GetLastError();


Now when I run the program, I get the output as 87 for borh the conditions.
I checked the Error it signified, and found that it was for Incorrect Parameters.

But I can't understand how they could be wrong.
Any help??

EDIT:
I have tried using both hStdout and hStdin for the first parameter of the two Functions.

EDIT 2:
Got the problem.
The error was that I hadn't declared cbSize of orig and current

Though I have a new problem.
The program isn't effecting the Font of the console at all!
(BTW I am using Windows 7 ).
Last edited on Aug 18, 2011 at 6:48am
Aug 17, 2011 at 5:46pm
current.cbSize = 16;

Where does 16 come from?

This line should read

current.cbSize = sizeof(CONSOLE_FONT_INFOEX);

Andy

P.S. sizeof(CONSOLE_FONT_INFOEX) == 84
Aug 18, 2011 at 2:40am
@andywestken:
I had initially thought that it was the size of the characters (similar to font size, I mean).
I later saw a comment on the MSDN documentation saying that it has to be initialized as you specified.

That removed the errors the functions were giving.

I was now having another problem that the functions were returning TRUE, but no change was coming on the Text in the console.

Any help with that?
Aug 18, 2011 at 4:48pm
Please note that writing 84 in that code is a very bad idea, as it only has that value in win32, not in win64 or IA-64 architecture. Using sizeof operator is right thing to do.
Aug 18, 2011 at 8:56pm
I tried the values you used -- {10, 10}, 600, FF_ROMAN -- and it silently failed for me, too. But I got other value to work ok (see below)

SetCurrentConsoleFontEx's habit of silently failing does seem to be a common complaint about this function. And even worse, there is no official way (i.e. in MSDN) to get the list of supported fonts. But there are unsupported API calls: GetConsoleFontInfo and GetNumberOfConsoleFonts [1]

Note hat your posted code :
- is not initializing unused struct members to zero
- is not defining the font index or face name
- is using hStdin for the get call: both should use hStdOut

I got SetCurrentConsoleFontEx to work with

1
2
3
4
5
6
7
8
9
10
COORD FontSize = {16, 12};

CONSOLE_FONT_INFOEX current = {0}; // zero all members

current.cbSize = sizeof(CONSOLE_FONT_INFOEX);
current.nFont = 11; // apparently this is Lucida Console (no consts are provided [3])
current.dwFontSize = FontSize;
current.FontWeight = FW_NORMAL; // = 400, but should use predefined consts
current.FontFamily = FF_DONTCARE; // FF_ROMAN didn't work for me
// FaceName is unused 


If you just want to update the font size, you could reuse the info, just altering the size between the get and set calls (to hStdOut)

Andy

[1] Changing Console Fonts
http://blogs.microsoft.co.il/blogs/pavely/archive/2009/07/23/changing-console-fonts.aspx

[2] These two threads might also be of interest:

Console font size command
http://www.cplusplus.com/forum/beginner/28014/

Set font text in console application
http://social.msdn.microsoft.com/Forums/fi-FI/vclanguage/thread/2bffea84-e5a0-4fde-bd24-53cbcf1e3025
Last edited on Aug 18, 2011 at 8:57pm
Aug 19, 2011 at 10:39am
Thanks a lot.
I had corrected the last problem in my code already (After I corrected the cbSize problem,it Gave me the INVALID HANDLE error).
Topic archived. No new replies allowed.