Solved

Defining a shared data segment in a dll using Borland C++ compiler

Posted on 2003-11-05
6
810 Views
Last Modified: 2013-12-14
Is there a way to define a shared data segment in a DLL using Borland C++5.5 compiler. Apparently it doesn't support the VC++ #pragma data_seg(). I would greatly appreciate an example.

Thank you in advance,
Alex
0
Comment
Question by:FireClown
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 86

Expert Comment

by:jkr
ID: 9688501
See the sample at http://community.borland.com/article/0,1410,20008,00.html 

To change the data segment and the class name, you need to add #pragma option -zR[SEGMENT NAME] and #pragma option -zT[CLASS NAME] to the file you want the data shared from. Below is the source file I am going to export the integer named 'data':

File: SharedData.cpp
//---------------------------------------------------------------------------
// Borland C++Builder
// Copyright (c) 1987, 1999 Inprise Corporation. All Rights Reserved.
//---------------------------------------------------------------------------

#pragma option -zRSHSEG      // change default data segment name
#pragma option -zTSHCLASS    // change default data class name

// Here is the initialized data that will be shared.
int data = 0;

Notice that the segment name for this file is: SHSEGSHCLASS. A .def file is required for the linker to create the shared segement. Below is what the .def file looks like:

File: Shared.def
LIBRARY SHAREDDLL

SEGMENTS
  SHSEG CLASS 'SHCLASS' SHARED

Below is the header and source file for the DLL which contains the entry point into the DLL, and the setters and getters for the 'data' variable in the shared memory space:

File: SharedDLL.h
//---------------------------------------------------------------------------
// Borland C++Builder
// Copyright (c) 1987, 1999 Inprise Corporation. All Rights Reserved.
//---------------------------------------------------------------------------

#ifdef __DLL__
#define DLL_EXPORT __declspec(dllexport)
#else
#define DLL_EXPORT __declspec(dllimport)
#endif

extern "C"
{
void DLL_EXPORT SetData(int x);
int DLL_EXPORT GetData(void);
}

File: SharedDLL.cpp
//---------------------------------------------------------------------------
// Borland C++Builder
// Copyright (c) 1987, 1999 Inprise Corporation. All Rights Reserved.
//---------------------------------------------------------------------------

//---------------------------------------------------------------------------
#include
#pragma hdrstop

#include "Shared.h"
//---------------------------------------------------------------------------
//   Important note about DLL memory management when your DLL uses the
//   static version of the RunTime Library:
//
//   If your DLL exports any functions that pass String objects (or structs/
//   classes containing nested Strings) as parameter or function results,
//   you will need to add the library MEMMGR.LIB to both the DLL project and
//   any other projects that use the DLL.  You will also need to use MEMMGR.LIB
//   if any other projects which use the DLL will be perfomring new or delete
//   operations on any non-TObject-derived classes which are exported from the
//   DLL. Adding MEMMGR.LIB to your project will change the DLL and its calling
//   EXE's to use the BORLNDMM.DLL as their memory manager.  In these cases,
//   the file BORLNDMM.DLL should be deployed along with your DLL.
//
//   To avoid using BORLNDMM.DLL, pass string information using "char *" or
//   ShortString parameters.
//
//   If your DLL uses the dynamic version of the RTL, you do not need to
//   explicitly add MEMMGR.LIB as this will be done implicitly for you
//---------------------------------------------------------------------------
USEUNIT("SharedData.cpp");
USEDEF("Shared.def");
//---------------------------------------------------------------------------
int WINAPI DllEntryPoint(HINSTANCE hinst, unsigned long reason, void*)
{
   return 1;
}
//---------------------------------------------------------------------------
extern int data;

//---------------------------------------------------------------------------
void DLL_EXPORT SetData(int x)
{
  data = x;
}
//---------------------------------------------------------------------------
int DLL_EXPORT GetData(void)
{
  return data;
}
//---------------------------------------------------------------------------

0
 

Author Comment

by:FireClown
ID: 9689170
Thank you for a quick response,
I saw that article, but it applies to Borland C++ 4.0. The new compiler does not accept the -zR and -zT options for some reason. I therefore assumed that they designed another way of doing this and forgot to document it.
0
 
LVL 30

Expert Comment

by:Axter
ID: 9691265
Try using MapView API functions instead.

MapView API functions work on any Win32 compiler.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 6

Accepted Solution

by:
GloomyFriar earned 125 total points
ID: 9693245
It's not an excellent way, but you can change section flags after compilation and linking with using program like "Lord PE":
http://scifi.pages.at/yoda9k/LordPE/info.htm
0
 

Author Comment

by:FireClown
ID: 9710074
Thank you Axter, but I really need to get the sections defined the way VC++ lets me do it.
Thank you GloomyFriar it appears to require some extra fiddling, but works
0
 
LVL 30

Expert Comment

by:Axter
ID: 9711724
>> I really need to get the sections defined the way VC++ lets me do it.

VC++ lets you use MapView API functions.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
installing and using WTP plugin eclipse MARS 3 149
convert char array to number in c 5 93
C++ Code Issue 4 48
Netbeans and org.apache.commons.lang3 issue 3 53
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The viewer will learn how to use and create new code templates in NetBeans IDE 8.0 for Windows.
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…

738 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question