OWL NExt - Knowledge Base

[ Home | Contents | Search | Next | Previous | Up ]

Global Data in Win32

Date: 12/1/99
Time: 9:41:25 PM

Question:


I noticed that in Richter's "Advanced Programming 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 <iostream.h>

#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

Last changed: July 14, 2001