PRODUCT : Borland C++ NUMBER : 1391 VERSION : 1.0 OS : OS/2 DATE : October 25, 1993 PAGE : 1/4 TITLE : Implementing shared memory in DLLs CREATING AN OS/2 2.X DLL WITH A SHARED AND NONSHARED DATA SEGMENT Use the include files to build and test the DLL SHAREDLL.DLL. SHAREDLL.DLL creates a new segment named "SHAREDATA" and puts the segment in a group named "SHAREGROUP". It is important to note that you cannot create a seperate segment with new protection attributes without giving it a new group, since groups share segment protection attributes. In addition to renaming the group and segment, a new class must be given to our new segment; the name choosen in this example is 'SHARECLASS'. FILELIST: GLOBAL.C - Contains the variable client_number which is located in the new segment "SHAREDATA", grouped in the group "SHAREGROUP", and given the class "SHARECLASS". The '#pragma option -z' compiler options are used to rename the segment attributes in the sourcefile. SHAREDLL.C - Contains exported function QueryClientNumber() which increments the variable "client_number" and prints its current value. There is a non-shared variable declared in the source file called "calls". This value reflects the number of times a given executable has called the DLLs exported function. SHAREDLL.DEF - Contains the defination file for the DLL. This has the SEGMENT directive that allows our new segment "SHAREDATA" to be shared and have a common variable "client_number". This defination file also declares the default data segment to be MULTIPLE NONSHARED to allow a concurrent example of a non-shared or private variable in a DLL. MAIN.C - Contains the source code for the executable that will call the dll function to test our PRODUCT : Borland C++ NUMBER : 1391 VERSION : 1.0 OS : OS/2 DATE : October 25, 1993 PAGE : 2/4 TITLE : Implementing shared memory in DLLs example DLL. SHAREDLL.MAK - Makefile to create the DLL and the executable. In order to see the shared and nonshared segment of the DLL in use, run multiple copies of the MAIN.EXE program. MAIN uses the imported function from SHAREDLL.DLL. Use the OS/2 command START to run a few copies of MAIN in the background, then run a copy of MAIN as a foreground process. The START command is used by giving it a name of an executable: START MAIN.EXE. // ** START OF FILE SHAREDLL.C ** #include // Declare a extern prototype to a variable that is defined // in another segment besides the default DLL segment. extern int client_number; // This variable will have a new copy for each executable // that loads the DLL. int calls = 1; // **************************************************** // Function will be exported in the DLL and allow the * // a EXE to query and increment the shared variable. * // The variable "calls" will not be a shared variable * // and thus will have a copy of itself for each copy * // of the executable. * // **************************************************** void _export QueryClientNumber(void) { printf("the client number is: %d\n", client_number++); printf("executable called DLL: %d times\n", calls++); } // ** END OF FILE SHAREDLL.C ** // ** START OF FILE GLOBAL.C ** #pragma option -zR_SHAREDATA #pragma option -zS_SHAREGROUP PRODUCT : Borland C++ NUMBER : 1391 VERSION : 1.0 OS : OS/2 DATE : October 25, 1993 PAGE : 3/4 TITLE : Implementing shared memory in DLLs #pragma option -zT_SHARECLASS int client_number = 1; // ** END OF FILE GLOBAL.C ** // ** START OF FILE SHAREDLL.DEF ** LIBRARY SHAREDLL DATA MULTIPLE NONSHARED SEGMENTS _SHAREDATA CLASS '_SHARECLASS' SHARED EXPORTS _QueryClientNumber // ** END OF FILE SHAREDLL.DEF ** // ** START OF FILE MAIN.C ** #include #include // Exported function from the DLL that increments the variable // in a new shared data segment and prints its value. extern void QueryClientNumber(void); void main(void) { int i = 0; for(;i < 5;i++) QueryClientNumber(); while(!kbhit()); } // ** END OF FILE MAIN.C ** // ** START OF FILE SHAREDLL.MAK ** SHAREDLL.DMP : SHAREDLL.DLL SHAREDLL.LIB MAIN.EXE @ECHO BUILD COMPLETE! SHAREDLL.DLL : SHAREDLL.C GLOBAL.C SHAREDLL.DEF BCC -sd SHAREDLL.C GLOBAL.C PRODUCT : Borland C++ NUMBER : 1391 VERSION : 1.0 OS : OS/2 DATE : October 25, 1993 PAGE : 4/4 TITLE : Implementing shared memory in DLLs SHAREDLL.LIB : SHAREDLL.DLL IMPLIB SHAREDLL.LIB SHAREDLL.DLL MAIN.EXE : MAIN.C BCC MAIN.C SHAREDLL.LIB // ** END OF FILE SHAREDLL.MAK ** DISCLAIMER: You have the right to use this technical information subject to the terms of the No-Nonsense License Statement that you received with the Borland product to which this information pertains.