clandestine
clandestine

Reputation: 35

Getting the size of current font on console C++

I'm having some problems with a bit of code. Apparently this returns an error.

GetCurrentConsoleFont(hOut, TRUE, fontInfo);

Where hOut is the std output handle and fontInfo is a PCONSOLE_FONT_INFO. It stops the thread and then exits returning -1073741819.

Any ideas?

Here is some code that does the causes the exact same problem.

#include <iostream>
#include <Windows.h>

PCONSOLE_FONT_INFO fontInfo;
HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);

int main() {
    GetCurrentConsoleFont(hOut, FALSE, fontInfo);
    return 0;
}

Upvotes: 2

Views: 301

Answers (1)

Andre Kampling
Andre Kampling

Reputation: 5630

In your code your using the fontInfo variable which is just an uninitialized pointer. There are two types CONSOLE_FONT_INFO and with a capital P in front of it PCONSOLE_FONT_INFO. Look at the following code to understand how to do it correctly.

HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_FONT_INFO fontInfo;

if (!GetCurrentConsoleFont(hOut, TRUE, &fontInfo))
    std::cout << GetLastError() << std::endl;

In that example I use a local variable so that the memory is on the stack. To pass a pointer you have to take the address with the address operator &.

Another possibility is, to allocate the memory on your own by using the new operator. But in that case the memory would lie on the heap and you would have to free the memory by using delete. You would pass the pointer directly without taking the address.

Upvotes: 1

Related Questions