CTEC1638 Windows Programming I 2009W

"The Art (and Science) of Using Global Variables Without Using Global Variables"

Method 1:

Example:

        in globals.h:
        extern HINSTANCE G_hInst;

        in globals.cpp:
        #include <windows.h>
        #include "globals.h"
        HINSTANCE G_hInst = NULL;

        in program.cpp:
        #include <windows.h>
        #include "globals.h"
        ...
        
        WinMain( HINSTANCE hThisInst, ... )
        {
                G_hInst = hThisInst;
                ...
        }

        WndProc( ... )
        {
                ...
                ...  LoadBitmap( G_hInst, ... );
                ...
        }

Method 2:

Example:

        in globals.h:
        struct tagGlobal
        {
                HINSTANCE hInst;
        };

        extern struct tagGlobal G;

        in global.c:
        #include <windows.h>
        #include "globals.h"
        struct tagGlobal G =
        {
                NULL    // HINSTANCE hInst
        };

        in program.cpp:
        #include <windows.h>
        #include "globals.h"
        ...
        
        WinMain( HINSTANCE hThisInst, ... )
        {
                G.hInst = hThisInst;
                ...
        }

        WndProc( ... )
        {
                ...
                ...  LoadBitmap( G.hInst, ... );
                ...
        }

Method 3:

Example:

        in globals.h:
        struct tagGlobal
        {
                HINSTANCE hInst;
        };

        typedef struct tagGlobal GLOBAL, * LPGLOBAL;

        LPGLOBAL GetGlobalData( );

        in global.cpp:
        #include <windows.h>
        #include "globals.h"

        LPGLOBAL
        GetGlobalData( )
        {
                static GLOBAL data =
                {
                        NULL    // HINSTANCE hInst
                };
                return ( & data );
        }

        in program.cpp:
        #include <windows.h>
        #include "globals.h"
        ...
        
        WinMain( HINSTANCE hThisInst, ... )
        {
                LPGLOBAL lpG = GetGlobalData( );
                ...
                lpG->hInst = hThisInst;
                ...
        }

        WndProc( ... )
        {
                LPGLOBAL lpG = GetGlobalData( );
                ...
                ...
                ...  LoadBitmap( lpG->hInst, ... );
                ...
        }

Method 4 (C++ only):

Example:

        in Global.h:
        class CGlobal
        {
        private:
                static HINSTANCE m_hInst;

                CGlobal( ) { ; }        // pure static class

        public:
                static HINSTANCE & hInst( ) { return m_hInst; }

        };

        in Global.cpp:
        #include <windows.h>
        #include "Global.h"

        HINSTANCE CGlobal::m_hInst = NULL;


        in program.cpp:
        #include <windows.h>
        #include "Global.h"
        ...
        
        WinMain( HINSTANCE hThisInst, ... )
        {
                ...
                CGlobal::hInst( ) = hThisInst;
                ...
        }

        WndProc( ... )
        {
                ...
                ...  LoadBitmap( CGlobal::hInst( ), ... );
                ...
        }

Download example code (12K, zipped)


Back to CTEC1638