I'm writing a program that uses a DLL. Is there any way for the DLL to access the variables declared in the main program? I am looking for some way to tell the DLL that those variables are defined in the main program. Can I use the externdef keyword for this or is there no way to do it directly?

To do it indirectly, I could write a function in the DLL that the main program calls to tell the DLL all the addresses of the various variables, but that would not be nice.
Posted on 2002-02-03 12:51:02 by Hel
Im just thinking theoretically, i dont *truely* have the correct answer.

However, since a DLL can be loaded once and called upon by multiple instances, i would say no. If the DLL were to directly know of a specific spot in memory, then the next instance would be false (as two programs cant reside in the same space).

For this reason i think you'd have to go the inderect route. But you could decair a section of Heap space and simply pass it one address. Then between your main program and the DLL you get to the data via a structure???

Just a thought...
Hope it helps..
:alright:
NaN
Posted on 2002-02-03 14:12:29 by NaN
Hel,

In the simple sense, no, a DLL encapsulates its own variables and is not within the scope of the calling process to access the callers variables.

There are a number of ways to do it depending on what you want to do. You could of course pass the required variables on the stack but it could become untidy if there are many you need access to.

Alternatively you could use a memory mapped file and store any variable you like in it as it can be accessed by a DLL if you open the memory mapped file.

I would be inclined to rethink the need for a DLL if you need to access a wide range of variables in the program as they are not really designed for that purpose. Basically if you need to do this type of code, it should go in thwe EXE file.

Regards,

hutch@movsd.com
Posted on 2002-02-03 15:40:35 by hutch--
It might be possible... but it would probably be messy. EXE files can
have an export section just like DLLs can. I tried this once with very
bad results (note that microsoft *does* write in the PlatformSDK
documentation that you should not use LoadLibrary on EXE files
to access anything but resources). On win2000 it "sort of" worked,
with some ugly hacking around (relocations were not applied even
though present in the EXE). On 9x it failed totally.

Also, if your DLL can be used by multiple processes, very funny
problems can arise. You must make sure that the functions only
try to access data in the context of the process that calls the functions.

It's a bit hard to give proper advise when I don't know exactly
what you're trying to do, but one of the better solutions (although
cumbersome) is to pass a struct with data/function pointers to the
DLL. It has worked reasonably, just look at the quake2 based games.
Posted on 2002-02-03 15:54:40 by f0dder
For my two cents, the way I would do it is the way you already rejected: Pass the address of the data in the exe to the dll.

I'd also make it some type of structure just to keep all the elements well organized.

This would be a single DWORD param to an exported method, not much overhead at all.
Posted on 2002-02-03 16:45:37 by Ernie