Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

global variables/functions across different projects

Status
Not open for further replies.

deb9

Programmer
Sep 21, 2005
8
US
Hi,

I would like to share information between different projects in VC++ using common (global) variables. How do I declare a variable or function to be global across different projects? (not just within files of the same project)?

Thank you so much

Deb
 
Not sure what you are asking. But if you want the same variables in different projects then put the global variables in a header file and include it in each project that you want those variables in.

However, if you want different running processes to use the same variables, you would have to use something like memory mapped files with mutexes. Or you could use pipes, depending on what you are seeking.


HyperEngineer
If it ain't broke, it probably needs improvement.
 
Yes I would like to use the same int variable in two different running processes: one is a GUI were I set the variable to a given value and the other is another process running that reads the variable. I have to use pipes? How do I do it? (Dont know anything about it)

Thanks HyperEngineer

deb
 
You can use shared memory. It is a bit painful as you have to lock it every time you wish to access it and unless you unlock it, nothing else can access it.
 
Deb,

I don't know too much about pipes. I could not find alot in MSDN. But I've used memory mapped files.

The main process will create the memory mapped file. All other processes will open the mapped file.

Create a mapped file:

Code:
  HANDLE MemoryMapHandle;
  LPVOID MapPointer;
  char* OriginData;
  
  // Create the mapped file
  MemoryMapHandle = CreatFileMapping((HANDLE) 0xFFFFFFFF, NULL, PAGE_READWRITE, 0, 2048, "SharedData");
  // Get a pointer to the mapped file
  MapPointer = MapViewOfFile(MemoryMapHandle, FILE_MAP_WRITE, 0, 0, 0);
  // Send data to the mapped file
  ::lstrcpy((char*) MapPointer, OriginData);

When you are done with the file:
Code:
  UnMapViewOfFile(MapPointer);
  CloseHandle(MemoryMapHandle);

Your other processes would use:

Code:
  HANDLE SharedMap;
  LPVOID MapPointer;
  char* DestinData;

  // Open the mapped file
  SharedMap = OpenFileMapping(FILE_MAP_READ, FALSE, "SharedData");
  // Get a pointer to the mapped file
  MapPointer = MapViewOfFile(SharedMap, File_MAP_READ | FILE_MAP_WRITE, 0, 0, 0);
  // Read data to the mapped file
  DestinData = (char*) MapPointer;

Once again, when your process is finished with the mapped file:

Code:
  UnMapViewOfFile(MapPointer);
  CloseHandle(SharedMap);

If you are doing a DLL, then xwb had the solution. Use shared memory.

In the DLL:

Code:
  #pragma data_seg("SharedMemory")
     //  Your shared variable
     int data = 0;
  #pragma data_seg()

In your .def file put under SECTIONS:

Code:
  SECTIONS
    SharedMemeory  READ WRITE SHARED

Hope this helps



HyperEngineer
If it ain't broke, it probably needs improvement.
 
Thanks to all for the very useful info.

Deb
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top