Board index » delphi » best way to share global data between dll and apps

best way to share global data between dll and apps

Could someone recommend the best way to share data variables between
an app and a dll? 32 bit windows, borland 5.02 C++

I wanted to use a singleton pattern object, however I believe that the
"static" instance of the variable would not be shared between the dll
and the application, as, for example, if I have a global in one and an
extern global in the other of the same name, they are not the same.
i.e. if I change the dll one, the app one stays the same.

I believe I could use memory mapped files.

Isn't there some sort of pragma (yes I am trying to fix a mess I made)
I could use for the variables to ensure they are in the same space?

Any comments?

Thanks for your assistance,
Jeff Kish
jeff.k...@mro.com

 

Re:best way to share global data between dll and apps


Jeff,

Once a DLL is loaded it can access any memory address which the EXE
can access. From the CPU's point of view there is no difference
between EXE code and DLL code.

The easiest way to do what you want is decide whether the DLL or the
EXE are to hold the data then pass the other one its address. Eg:

// EXE code -----------------
struct TGlobalVars
{
  WORD fred;
  int  binkle;

Quote
};

TGlobalVars GlobalVars;

// During DLL loading:
  DLLFunctionPassGlobalAddress( &GlobalVars );

// DLL code -----------------
TGlobalVars * GlobalVars;
void DLLFunctionPassGlobalAddress( TGlobalVars * EXEsGlobalVarsPtr )
{
  GlobalVars=EXEsGlobalVarsPtr;

Quote
}

// From now on the DLL can access the global variables by
dereferencing its GlobalVars pointer.

Although this would achieve want you want I'd have to say that I'm
not sure that using global variables this way is a good idea. Global
variables offer a great way of screwing things up and should be used
with caution.

Andrue Cope
[Bicester, UK]

Re:best way to share global data between dll and apps


Thanks for your reply.

Yes this is initially how I started out.

Due to an oversight, it is not quite working out the way I had hoped
(the dll links to a library the app links to also.. the library
accesses a variable as int global_int, and the dll was given a global
int * global_int.. but parts of the static library the dll links to
still reference int global_int... yes I screwed up a bit here.

Trying to figure a good way out of my incompentance. There is at least
one global that varies at run-time so "everyone" needs to see it no
matter who changes it. Oh, by the way.. a mostly single threaded
application.

Any more suggestions?

Thanks,
Jeff Kish

On Thu, 21 Jun 2001 09:45:02 +0100, Andrue Cope

Quote
<not.a.va...@email.address.sorry> wrote:
>Jeff,

>Once a DLL is loaded it can access any memory address which the EXE
>can access. From the CPU's point of view there is no difference
>between EXE code and DLL code.

>The easiest way to do what you want is decide whether the DLL or the
>EXE are to hold the data then pass the other one its address. Eg:

>// EXE code -----------------
>struct TGlobalVars
>{
>  WORD fred;
>  int  binkle;
>};

>TGlobalVars GlobalVars;

>// During DLL loading:
>  DLLFunctionPassGlobalAddress( &GlobalVars );

>// DLL code -----------------
>TGlobalVars * GlobalVars;
>void DLLFunctionPassGlobalAddress( TGlobalVars * EXEsGlobalVarsPtr )
>{
>  GlobalVars=EXEsGlobalVarsPtr;
>}

>// From now on the DLL can access the global variables by
>dereferencing its GlobalVars pointer.

>Although this would achieve want you want I'd have to say that I'm
>not sure that using global variables this way is a good idea. Global
>variables offer a great way of screwing things up and should be used
>with caution.

>Andrue Cope
>[Bicester, UK]

Re:best way to share global data between dll and apps


Jeff,

Quote
> Any more suggestions?

Make sure you use this as a learning exercise :)

Seriously though the only other solution that springs to mind would
be to use another DLL to hold/manipulate the globals. Unfortunately
this would still mean a lot of work.

Andrue Cope
[Bicester, UK]

Other Threads