Stupid windows...

This is a discussion on Stupid windows... within the C++ Programming forums, part of the General Programming Boards category; Somewhere within my code (i think maybe the SDL or opengl library) i'm including windows.h and this stupid file has ...

  1. #1
    Registered User
    Join Date
    Apr 2007
    Location
    Sydney, Australia
    Posts
    217

    Stupid windows...

    Somewhere within my code (i think maybe the SDL or opengl library) i'm including windows.h and this stupid file has a crap load of reserved words! I've already come across three function names i've had to change because they conflict with a "#define" within windows.h or some other windows header. I don't need to call ANY windows function, only the cross platform libraries do. What can I do? I wan't to be able to create class methods called "GetClassName()" without it being interpeted as "GetClassNameA()".

  2. #2
    Student legit's Avatar
    Join Date
    Aug 2008
    Location
    UK -> Newcastle
    Posts
    156
    Overload the windows functions so that the methods you want to use belong to your class. Your compiler should know which functions to call by the context of which they are used in.

  3. #3
    Registered User
    Join Date
    Apr 2007
    Location
    Sydney, Australia
    Posts
    217
    The precompiler changes GetClassName to GetClassNameA.

    Code:
    const std::string& className = items[i].GetItemClass()->GetClassName();
    \Player.cpp:282: error: 'const class ItemClass' has no member named 'GetClassNameA'
    Same with all the other words winuser.h defines

  4. #4
    Student legit's Avatar
    Join Date
    Aug 2008
    Location
    UK -> Newcastle
    Posts
    156
    Hm....... What compiler are you using? I don't know myself how to turn the pre-compiler off but i'm sure many of the programmers on this board know how.

  5. #5
    Registered User
    Join Date
    Apr 2007
    Location
    Sydney, Australia
    Posts
    217
    I'm pretty sure turning the precompiler off would break everything. It's pretty much essential.

  6. #6
    Student legit's Avatar
    Join Date
    Aug 2008
    Location
    UK -> Newcastle
    Posts
    156
    My bad, i'm pretty new myself

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Yes, and that's done by a macro, so one possibility is to do
    Code:
    #undef GetClassName
    in the file that declares that class.

    Naturally, there are other solutions, here is just a short list of suggestions:
    - Do not use that name (or any other Windows function name that deals with text strings, as all of those will have a Unicode/Wide declaration, and a ANSI/Ascii declaration - which is selected by the "UNICODE" macro.
    - Do not use windows.h in a header file that is included by other header files, or in conjunction with class defintions.
    - Edit windows.h (probably NOT a good idea).
    - Complain to Microsoft.

    None of these are entirely trivial to do, but I can't really come up with anything better.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  8. #8
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,296
    Either rename your functions, or move everything that has to include "Windows.h" into a seperate cpp file, so that you don't have to include it in your other code. The later may require a small amount of refactoring or creating of stub functions.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  9. #9
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by iMalc View Post
    Either rename your functions, or move everything that has to include "Windows.h" into a seperate cpp file, so that you don't have to include it in your other code. The later may require a small amount of refactoring or creating of stub functions.
    That was one of my suggestions too - and I think that's the best solution. As a side-effect, all code directly related to Windows will now be located in one or a few .cpp files, which makes it relatively easy to port to another OS, should that need to happen [or even port to different versions of Windows, if they are so different that they need special treatment].

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Just starting Windows Programming, School me!
    By Shamino in forum Windows Programming
    Replies: 17
    Last Post: 02-22-2008, 07:14 AM
  2. Question..
    By pode in forum Windows Programming
    Replies: 12
    Last Post: 12-19-2004, 06:05 PM
  3. How come this only works in Windows nt/2000?
    By Unregistered in forum Windows Programming
    Replies: 1
    Last Post: 08-30-2002, 06:54 PM
  4. OpenGL and Windows
    By sean345 in forum Game Programming
    Replies: 5
    Last Post: 06-24-2002, 10:14 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21