Extern Variable and/or #define in VC++

Posted on 2004-11-03
Last Modified: 2013-11-20
My application has about 30 classes.

I want to add a Global constant used by multiple programs called SWAP_ALLOWED which will be set to 1 or 2 based on queried value from a database.

I'd like this to be a #define variable OR extern.

I do know that #define cant be changed at runtime - OR can it be? If so, how?

But extern "C" int SWAP_ALLOWED is producing a LNK2001 error!

What do I do?

Question by:ramchi22
    LVL 8

    Accepted Solution

    #define cannot be changed at runtime because it is preproccesor definition, hence the pre prefix.

    If you extern a variable, you can change that at runtime.  However by specifying extern you are saying that the variable is defined somewhere else.

    so if you have in all your files where you need to use globalVar.

    extern int globalVar;

    you need to have

    int globalVar;

    in one file where the variable actually resides.

    the LNK2001 is saying the linker can't find the,

    int globalVar;

    hope that helps.


    LVL 12

    Assisted Solution

    How do I share data in my DLL with an application or with other DLLs?

    if that variable is defined in a.exe and you want different instance of a.exe share that variable,
    you can define it in a shared date segment like this(just as decribed in MSDN):
    // shared data
    #pragma data_seg(".SHARED")
    int g_somedata=0;
    #pragma data_seg()
    #pragma comment(linker, "/section:.SHARED,RWS")

    welcome to
    LVL 8

    Expert Comment

    uh, I didn't realize you were trying to use a variable from separate executables?  

    If that is what you're trying to do, the response I posted will not work.  It only works from within the same executable.
    LVL 43

    Assisted Solution

    I want to add a Global constant used by multiple programs called SWAP_ALLOWED which will be set to 1 or 2 based on queried value from a database.

    Why not just let each app query the database for the value?
    LVL 1

    Assisted Solution

    You can use Memory-Mapped files to share the memory between different executables.You can allocate a pagefile as shared memory in one app and access the same pagefile from other files.

    From Master Application:Use below code
    void CreateMemoryMappedFile()
             int*  pSharedVars;
             HANDLE hMapFile = CreateFileMapping(INVALID_HANDLE_VALUE,// Use swap file
                             NULL,// default security
                             PAGE_READWRITE,// read/write permission
                             0,// max. object size
                             1024,// size of hFile
                             "SampleMappingObject");// name of mapping object
             if (hMapFile == NULL)
          return ;
          pSharedVars = (int*)MapViewOfFile(hMapFile,// handle to mapping object
                       FILE_MAP_ALL_ACCESS,// read/write permission
                       0,// max. object size
                      0,// size of hFile
                                 0);// map entire file
          if (pSharedVars == NULL)
                            pSharedVars[0] = 12;
                            pSharedVars[1] = 13;


    From Other Applcations follow below code:
    void CreateMemoryMappedFile()
             int*  pSharedVars = NULL;
             hFMapFile = OpenFileMapping(FILE_MAP_ALL_ACCESS, FALSE, "SampleMappingObject");

            if (hFMapFile != NULL)
                // Obtaining the base address of the mapping
                lpFMapAddress = MapViewOfFile(hFMapFile, FILE_MAP_ALL_ACCESS, 0, 0, 0);
               if (lpFMapAddress != NULL)
            pFSharedVars = (SHAREDVARS*)lpFMapAddress;

    I am already using the same in my programs and it is working fine.With this your problem wiil be solved.


    Featured Post

    Top 6 Sources for Identifying Threat Actor TTPs

    Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

    Join & Write a Comment

    Suggested Solutions

    Title # Comments Views Activity
    Fingerprint solution to register in a website system 3 78
    if loop in java 3 99
    either24  challenge 19 73
    Window placement 17 56
    Here is how to use MFC's automatic Radio Button handling in your dialog boxes and forms.  Beginner programmers usually start with a OnClick handler for each radio button and that's just not the right way to go.  MFC has a very cool system for handli…
    Introduction: Dynamic window placements and drawing on a form, simple usage of windows registry as a storage place for information. Continuing from the first article about sudoku.  There we have designed the application and put a lot of user int…
    This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
    Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

    733 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    22 Experts available now in Live!

    Get 1:1 Help Now