Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

100+ errors from wingdi.h when building

Tags:

c++

So I am making a game, an ASCII dungeon explorer and I had a plan to get the window size and scale the display the inventory beside the dungeon. I went on google to find a function that would return the window size so I can print the inv on the side... The parts that are commented out is the code that I found and so I tried to put it in my main.cpp just to try it out. I planned to use other functions that I found to get the max size of the window and to set the size. This code that I pasted into my code was giving me loads of errors when I went to run the game, somewhere around 180 errors from a header file called wingdi.h. I googled a bit more and found people changing some definitions in the project properties which I tried and it gave me about 130 errors from different headers included in . Someone said that one of the variables is already declared in windows.h and the person that asked that question should change it to something else so I changed csbi to my_csbi, both didn't work. Some people also said it was a problem with the code so I decided to just leave it for now, and go back to my old code and do something else for now (I was getting frustrated, lol). I commented it all out, hoping to come back to it later. When I tried to run my game with all of that code gone, it gave me the same errors. Do I have to reinstall Visual Studio 2015? Starting my code from scratch or changing some definitions?

    #include <iostream>
    #include <conio.h>
    //#include <Windows.h>
    #include "GameSystem.h"

    using namespace std;

    int main()
    {
        //int x = 0;
        //int y = 0;

        //CONSOLE_SCREEN_BUFFER_INFO my_csbi;
        //GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &my_csbi);

        //x = my_csbi.srWindow.Right -my_csbi.srWindow.Left + 1;
        //y = my_csbi.srWindow.Bottom -my_csbi.srWindow.Top + 1;

        //printf("columns: %d\n", x);
        //printf("rows: %d\n", y);

        GameSystem gameSystem("level1.txt");

        gameSystem.playGame();

        printf("Enter any key to exit...\n");
        int tmp;
        cin >> tmp;
        return 0;
    }

I am fairly new to programming so I'm sorry if my question is stupid or "simple" and if that offended you in some shape or form. :)

Thanks, Bulky.

EDIT: All the "solutions" others got didn't work for me and I decided to ask this because after I commented out all that code, my old code didn't even run (it was perfect before).

like image 853
Bulky Avatar asked May 01 '26 19:05

Bulky


1 Answers

  1. You never include <WinGdi.h> directly. You always include <Windows.h>, which pulls in the required headers implicitly.

  2. The GDI functions (from <WinGdi.h>) aren't going to do you any good if you are writing a console application. The GetConsoleScreenBufferInfo function is probably what you want to be using, but it is not a GDI function. It is a kernel function. Again, although it is technically declared in <WinCon.h>, you are not supposed to include that header directly. Just include <Windows.h>.

The following code works fine for me
(other than the fact that std::cin doesn't actually work for "enter any key to exit..."):

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

using namespace std;

int main()
{
   CONSOLE_SCREEN_BUFFER_INFO csbi;
   GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi);

   int x = csbi.srWindow.Right  - csbi.srWindow.Left + 1;
   int y = csbi.srWindow.Bottom - csbi.srWindow.Top  + 1;

   printf("columns: %d\n", x);
   printf("rows: %d\n", y);


   printf("Enter any key to exit...\n");
   int tmp;
   cin >> tmp;
   return 0;
}

Output:

columns: 90
rows: 33
Enter any key to exit...

If it does not work for you, one of the following must be true:

1. The problem resides in "GameSystem.h", which you have not shown us.
(It is fine—in the future, please post code as text, not pictures.)

2. You have created the wrong type of project (recreate a new project using the Win32 Console Application template).

3. There is something wrong with your installation of Visual Studio (try reinstalling).

like image 72
Cody Gray Avatar answered May 03 '26 10:05

Cody Gray



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!