Материал взят с сайта.
C++ Builder
| Главная | Уроки | Статьи | FAQ | Форум | Downloads | Литература | Ссылки | RXLib | Диски |

 
Глобальные переменные в DLL
olegenty
Отправлено: 30.09.2004, 14:51


Ветеран

Группа: Модератор
Сообщений: 2412



Чё то я расстроился. объявляю глобальную переменную в DLL, искренне будучи уверенным, что она глобальная, а она, падла, имеет свою копию для каждого присоединения DLL. Депрессия быстро прошла, но как сделать переменную разделяемой для всех подключений к DLL, я так и не понял.
вернее, как сделать в VC++ — нашёл быстро, а вот для BCB это неверно, у него таких #pragma нет... грустно...
olegenty
Отправлено: 01.10.2004, 08:28


Ветеран

Группа: Модератор
Сообщений: 2412



инфы по этому поводу крупицы, но всё же одну ссылочку найти удалось. может кому пригодится:
QUOTE

community.borland.com

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
AVC
Отправлено: 01.10.2004, 10:19


Ветеран

Группа: Модератор
Сообщений: 1583



О, это интересно. Спасибо!
Хлор
Отправлено: 06.10.2004, 02:49


Дежурный стрелочник

Группа: Участник
Сообщений: 78



Познавательно smile.gif
Спасиб......

Вернуться в Вопросы программирования в C++Builder