?
Solved

Extern Variable and/or #define in VC++

Posted on 2004-11-03
8
Medium Priority
?
942 Views
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?

Thx.
0
Comment
Question by:ramchi22
5 Comments
 
LVL 8

Accepted Solution

by:
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.

-A

0
 
LVL 12

Assisted Solution

by:OnegaZhang
OnegaZhang earned 500 total points
ID: 12488949
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vccore/html/_core_how_do_i_share_data_in_my_dll_with_an_application_or_with_other_dlls.3f.asp
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
0
 
LVL 8

Expert Comment

by:AaronReams
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.
0
 
LVL 45

Assisted Solution

by:AndyAinscow
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?
0
 
LVL 1

Assisted Solution

by:suryaxchange
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.

Example:
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)
            return;
      else
                {
                        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.

-Surya.
0

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