Storing variables in DLLs

E

ewpatton

I'm writing a DLL and want to store information internally between
calls to the DLL functions.

For example:

static int i1,i2;

INT APIENTRY StoreInt1(int i) { return i1=i; }
INT APIENTRY StoreInt2(int i) { return i2=i; }
INT APIENTRY GetSum() { return i1+i2 }

I have all the definitions set up correctly... and the test program
compiles fine... but when I run the program:

StoreInt1(5);
StoreInt2(10);
int i = GetSum();

i != 15?

It seems like after the application leaves the DLL it losses all the
static information so when it calls it again, the variables are fresh.
I've also tried making the whole library a static library (.lib vs
..dll) but the same thing happens.

Any ideas?

Evan
 
J

Jack Klein

I'm writing a DLL and want to store information internally between
calls to the DLL functions.

[snip]

Then you need to ask in a group like
or one of Microsoft's groups
in the family. This is specific to your
platform and its mechanisms, the C++ language does not define or
acknowledge the existence of DLLs.
 
S

Shark

I'm writing a DLL and want to store information internally between
calls to the DLL functions.

For example:

static int i1,i2;

INT APIENTRY StoreInt1(int i) { return i1=i; }
INT APIENTRY StoreInt2(int i) { return i2=i; }
INT APIENTRY GetSum() { return i1+i2 }

I have all the definitions set up correctly... and the test program
compiles fine... but when I run the program:

StoreInt1(5);
StoreInt2(10);
int i = GetSum();

i != 15?

It seems like after the application leaves the DLL it losses all the
static information so when it calls it again, the variables are fresh.
I've also tried making the whole library a static library (.lib vs
.dll) but the same thing happens.

Any ideas?

Evan

?? If your application stops refering the dll, the information is lost.
It should loose. Internally it maintains a reference count. once your
application stops refering it and no other applications are using it ,
even the dll gets unloaded. but even this doesn't matter. if you close
your application, the info would be lost. If you want to have them
stored for future usage, save them in a text file or use a db.
 
G

Gianni Mariani

It seems like after the application leaves the DLL it losses all the
static information so when it calls it again, the variables are fresh.
I've also tried making the whole library a static library (.lib vs
.dll) but the same thing happens.

Any ideas?

I'm not sure what semantics you want it to have but DLL's (or *nix
..so's) work just as if you statically link them in this case. i.e.

main.cpp
----------
static int i1,i2;

int StoreInt1(int i) { return i1=i; }
int StoreInt2(int i) { return i2=i; }
int GetSum() { return i1+i2 }

int main()
{
StoreInt1(5);
StoreInt2(10);
int i = GetSum();

// i had better be 15.
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,779
Messages
2,569,606
Members
45,239
Latest member
Alex Young

Latest Threads

Top