April 28, 2024, 05:52:11 PM

News:

Own IWBasic 2.x ? -----> Get your free upgrade to 3.x now.........


Variables inside DLL

Started by Barney, July 09, 2007, 09:23:29 AM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Barney

I am writing a DLL, which is supposed to be used by many other languages, but for the sake of discussion let's assume I am writing it in EBasic and it will be used with Aurora. I've done all the functions and that was the easy bit. All necessary functions are properly exported and I am happily using those functions with Aurora. So far, so good.

I have a number of variables inside the DLL source, which are global and I would like them to be available to my Aurora program. I know I can write a simple set and get functions to access those variables inside DLL but I'd like to have more direct access i.e. I'd like to change them and use them directly in my main Aurora (or other language) program. I've tried various combinations of EXPORT and GLOBAL and EXTERN keywords but I must admit it was without success. Most of the time nothing happens and in the worst case my program was unceremoniously terminated.

Here's basically what I want to do. Part of the DLL source:


EXPORT SpriteGraphics3D
DECLARE CDECL SpriteGraphics3D(INT gw, INT gh, OPT INT d=0, OPT INT m=0, OPT FLOAT pivotdist=1.0f)
EXPORT GetVars3D
DECLARE CDECL GetVars3D(INT iVar1 BYREF, INT iVar2 BYREF)
EXPORT SetVars3D
DECLARE CDECL SetVars3D(INT iVar1, INT iVar2)


INT spritepivot,spritecamera
GLOBAL spritepivot
GLOBAL spritecamera

SUB GetVars3D(INT iVar1 BYREF, INT iVar2 BYREF)
iVar1=spritecamera
iVar2=spritepivot
ENDSUB

SUB SetVars3D(INT iVar1, INT iVar2)
spritecamera=iVar1
spritepivot=iVar2
ENDSUB

' Create a complete sprite display with camera/pivot
SUB SpriteGraphics3D(INT gw, INT gh, OPT INT d=0, OPT INT m=0, OPT FLOAT pivotdist=1.0f)
bbGraphics3D(gw,gh,d,m)
bbSetBuffer(bbBackBuffer())
spritecamera=bbCreateCamera()
spritepivot=CreateSpritePivot(spritecamera,pivotdist)
ENDSUB
.
.


So, my question is simple. How should I setup the variables so that I can access them directly i.e spritepivot=whatever inside the main program? Or is it simply not possible?

Barney

Ionic Wind Support Team

There are so many reasons you shouldn't do that ;)

DLL's are dynamic,  hence their name, and may be mapped into memory anywhere.  DLL's may be unloaded by the OS when memory gets short and an address you had previously is no longer valid when the DLL gets reloaded back into memory.

In any event there is no direct way for a linker to attach an executable to the address of a DLL global variable.  The address of the variable in a DLL is never known until it has been loaded by the OS.

Last, not as important anymore, is on 9x systems the global address tables of DLL's are shared. Meaning there is only one copy of the DLL in memory so another process using your DLL can trounce on that global variable.

Paul.
Ionic Wind Support Team

Barney

Yeah, I thought so, but it did not hurt to ask. ;)

I already have a solution to what I want to do. It's not as elegant but it works.

Thanks for prompt answer, Paul. I hope this marks the end of my little flurry of questions regarding DLL's.  ;D

Barney