community.borland.com

Article #17047: Global Data in Win32

 Question and Answer Database

FAQ2047C.txt   Global Data in Win32 
Category   :Windows API
Platform    :All
Product    :C++Builder  ALL

Question:
I nototiced that in Richter's "Advanced Programing in Windows" 
he uses a #pragma statement to share global data, but this
does not work with your compiler, is there a way to do this 
with your compiler? 

Answer:
Yes of course there is.  It is quite easy.  Just create a .def 
file with your project and share the variable in there.  Here is
an example of the .def file you need:

SECTIONS
my_share_section READ WRITE SHARED

And here is an example C++ program to use the shared data:

#include 

#pragma data_seg ("my_share_section")
	long shared_long = 0;
   char shared_array[] = "xxxxxxxxxxx";
#pragma data_seg()

main (){
	int choose = 1;
	while (choose){
		cout << "(1) Change long\n";
      cout << "(2) Print  long\n";
      cout << "(3) Change array\n";
      cout << "(4) Print  array\n";
      cout << "(0) Quit:" << endl;
      cin >> choose;

      switch (choose){
      	case 1: cout << "Enter number: ";
         		  cin >> shared_long;
                 break;

         case 2: cout << "Shared long = " << shared_long << endl;
         		  break;

         case 3: cout << "Enter 10 char array: ";
         		  cin.getline (shared_array, 2);
                 cin.getline (shared_array, 10);
                 break;

         case 4: cout << "Shared array = " << shared_array << endl;
         	     break;

         default: cout << "Good Bye" << endl;
         		   break;
      }
   }
}

if you want you can make all the static and global data shared 
by having your def file as:

SECTIONS
   .data READ WRITE SHARED



7/2/98 10:32:32 AM
 

Last Modified: 01-SEP-99