Главная страница | назад





Article #20132: How to create a shared segment in a dll.

Question:

How do I build a dll that contains a shared data segment? Books I've been reading mention the #pragma data_seg() directive, but this doesn't seem to be supported by Builder.

Answer:

Builder 4 does not support the #pragma data_seg() directive, but there are compiler switches that will, along with the proper .def file, allow you to do this.

You will have to put the data you want shared in it's own source file and use the -zR and -zT compiler switches. These switches change the data segment name and class name for whatever source file they are used in. You will have to initialize the data you want to share, as uninitialized data is automatically moved to the default data segment. You must also create a .def file for your dll that tells the linker to give that segment the shared property.

Example:

There are four files in this example:

  • dll.h // foreward declarations for the dll
  • dllmain.cpp // the CPP file that contains the dll entrypoint
  • dllshared.cpp // contains the shared data
  • dlldef.def // module definition file for the dll

#pragma hdrstop
#include 
#define __dll__
#include "dll.h"

#pragma argsused
int WINAPI DllEntryPoint(HINSTANCE hinst, unsigned long reason, void*)
\{
        return 1;
\}

extern int SharedInteger;

int GetInteger(void)
\{
return SharedInteger;
\}

void SetInteger(int arg)
\{
SharedInteger = arg;
\}

//---- end file dllmain.cpp ----//




//---- begin file dllshared.cpp ----//

#pragma option -zRSHARESEG	// sets segment name for this source file to SHARESEG
#pragma option -zTSHARECLS	// sets class name for segment to SHARECLS

int SharedInteger = 0;

//---- end file dllshared.cpp ----//



//---- begin file dlldef.def ----//

LIBRARY DLLMAIN

SEGMENTS
	SHARESEG CLASS "SHARECLS" SHARED

//---- end file dlldef.def ----//

Last Modified: 10-MAY-00