Thread: reducing global variables in windows programs

  1. #1
    Registered User
    Join Date
    Aug 2001
    Posts
    403

    reducing global variables in windows programs

    I'm fairly new to Win32 + MFC but it seems to me that using globals is virtually unavoidable.. with the WinProc and all.. or in MFC with different classes. Is there a solution to this problem I haven't come across yet in my learning? I never use global variables in my other programs, but it seems they must have to play a major part in Win32 or MFC programs.

  2. #2
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    In the wnd procs, just declare then static...they will then be there on the next call

    In MFC, declare your simple variables in in the class declaration.....they will stay in scope as long as the class

    If you want to access something in another class with MFC, there is a dirty way of using AfxGetApp () to get a pointer to your app....you might be able to access other classes through that depending on how you have built it......I dont like doing this much though
    Last edited by Fordy; 07-19-2002 at 09:31 PM.

  3. #3
    Registered User Dual-Catfish's Avatar
    Join Date
    Sep 2001
    Posts
    802
    I'm fairly new to Win32 + MFC but it seems to me that using globals is virtually unavoidable.. with the WinProc and all.. or in MFC with different classes. Is there a solution to this problem I haven't come across yet in my learning? I never use global variables in my other programs, but it seems they must have to play a major part in Win32 or MFC programs.
    Mmm, I know how you feel. When I started programming with the Win32 API, I was disgusted. Global variables, static variables... petzolds book is purely C. It scared me. I haven't touched it in awhile... I'm looking for a book, or a refrence which deals with the Win32 API and Object Oriented concepts (Basically a book that teaches the Win32 API with C++)

  4. #4
    Registered User The Dog's Avatar
    Join Date
    May 2002
    Location
    Cape Town
    Posts
    788
    Where can one find petzold's book?

  5. #5
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Where can one find petzold's book?
    Try Amazon.com or order from a ask your local bookstore for the phone numbers of sellers of "hard-to-find" books. I used to work at a bookstore and we had lists of suppliers.

    I'm fairly new to Win32 + MFC but it seems to me that using globals is virtually unavoidable..
    You too?

    Yeah MS really made a mess of their API, an accident, I'm sure

    Y'know, here I am, been programming for a year now, and at least 6 mos of that messing with Win32. What an occultish ritual THAT is! For one, there is little *solid* documentation of how functions are implemented, how they affect other areas of your app, etc. Worse yet, functions do not work as expected at times, as in: parameters that SHOULD be non-zero to work unexpectedly do, or vice versa! In fact, I just deleted my entire C Windows library. Too buggy. So I am rewriting it from scratch in - guess what? - C++ !!
    So that would be my first suggestion. Second, work slowly, mastering each little piece till you have a stable framework.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  6. #6
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    I don't know if this will help but here are some previous cprog->windows discussions:

    http://www.cprogramming.com/cboard/s...static+wndproc

    http://www.cprogramming.com/cboard/s...hlight=wndproc

  7. #7
    Registered User
    Join Date
    Jul 2002
    Posts
    273
    guys, I'm a windows developer and there are only three types of globals I EVER have in my code.

    WinMain() obviously

    WndProcs() because that's our messages

    and any Exported DLL functions. usually I only have one in my dll's to give me access to an object.

    The deal is this...

    1. Create an instance of your "Window" object in WinMain
    2. Call CreateWindow() width an LPARAM of the pointer to the object
    3. Handle WM_CREATE in the WndProc such that it does a SetWindowLong(hWnd, GWL_USERDATA, lParam); (of course the lParam means the item out of the CREATESTRUCT) This is your object
    4. Each time into the callback, GetWindowLong(hWnd,GWL_USERDATA) to get the object pointer.

    It's very simple, this provides a way of having NO GLOBAL DATA which is the way it should be.

  8. #8
    Registered User
    Join Date
    Jul 2002
    Posts
    273
    I realize that you may not want to use classes also. My method works just fine for structs also. Put all data associated with that window in a struct and pass the pointer into createwindow as above

  9. #9
    Registered User VBprogrammer's Avatar
    Join Date
    Mar 2002
    Posts
    175
    But - Why so against global varibles?
    VC++ 6

  10. #10
    Unregistered
    Guest
    Originally posted by Hershlag

    It's very simple, this provides a way of having NO GLOBAL DATA which is the way it should be. [/B]
    Sounds fascist.


    3. ...
    SetWindowLong(hWnd, GWL_USERDATA, lParam);
    and ugly.

  11. #11
    Registered User
    Join Date
    Jul 2002
    Posts
    273
    Global data is widely regarded as sloppy. I don't see that as something that makes me fascist. A neat freak maybe but hardly fascist. Besides, any windows programmer should write code that is fairly thread-safe and globals are the anti-thread.

    How is SetWindowLong ugly? it's a great way to associate data with a window in Win32. You have SetProp() also, but it requires a string search and is probably a bad alternative.

  12. #12
    Registered User
    Join Date
    Aug 2001
    Posts
    403
    Thanks for all the help, I now see several solutions to this problem, and I also am glad to realize I'm not the only one faced with this issue.


    But - Why so against global varibles?


    Of course this comes from a man w/ the ID of VBProgrammer :P.. Here in C++ world they are generally regarded to be on the evil side. (no offense, the VB thing was just funny to me)

  13. #13
    ¡Amo fútbol!
    Join Date
    Dec 2001
    Posts
    2,138
    coz, you don't need to baby VB. He works with asm and is even writing his own OS.

  14. #14
    Registered User
    Join Date
    Aug 2001
    Posts
    403

    alright :)

    ah ok, guess I just didn't expect that from the nick

  15. #15
    Registered User
    Join Date
    Jul 2002
    Posts
    273
    I work with a guy that is probably much like VBProgrammer. We used to debate things like global variables until we realized something about programming. There are different levels of abstraction that programmers work in. Basically, the lower level coder you are, the more often you are reinventing the wheel. The world NEEDS people who are willing to reinvent the wheel so that we can come up with better and better wheels. But it also needs people at higher levels of abstraction to build things with those wheels.

    The higher level of abstraction you work in, the more you should think about structure and design being readable. The lower you work in, the more you need to think about optimization for speed. Global variables are suitable to this low level but not higher levels. VBProgrammer has an ironic name in that he obviously thinks in this low level but you should understand that without us higher level thinkers we'd have great wheels but CRAPPY SOFTWARE!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Script errors - bool unrecognized and struct issues
    By ulillillia in forum Windows Programming
    Replies: 10
    Last Post: 12-18-2006, 04:44 AM
  2. General global bool variables?
    By Bajanine in forum Windows Programming
    Replies: 3
    Last Post: 02-27-2005, 09:16 PM
  3. Headers and Global Variables
    By ajoo in forum C++ Programming
    Replies: 1
    Last Post: 05-15-2004, 04:49 AM
  4. Replies: 6
    Last Post: 01-02-2004, 01:01 PM
  5. Global variables.. how bad?
    By punkrockguy318 in forum C++ Programming
    Replies: 19
    Last Post: 11-30-2003, 10:53 PM