Extern Variable and/or #define in VC++

Posted on 2004-11-03
Medium Priority
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

Accepted Solution

AaronReams earned 500 total points
ID: 12488916
#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

OnegaZhang earned 500 total points
ID: 12488949
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 www.fruitfruit.com

Expert Comment

ID: 12488971
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 45

Assisted Solution

AndyAinscow earned 500 total points
ID: 12490787
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?

Assisted Solution

suryaxchange earned 500 total points
ID: 12837855
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

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction: Database storage, where is the exe actually on the disc? Playing a game selected randomly (how to generate random numbers).  Error trapping with try..catch to help the code run even if something goes wrong. Continuing from the seve…
Introduction: The undo support, implementing a stack. Continuing from the eigth article about sudoku.   We need a mechanism to keep track of the digits entered so as to implement an undo mechanism.  This should be a ‘Last In First Out’ collec…
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.
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…
Suggested Courses
Course of the Month16 days, 7 hours left to enroll

850 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