extern variable

V

vadnala

I used to have a program(A.EXE) using an extern variable as follows:

A.cpp
extern DWORD Flag;

B.cpp
DWORD Flag;

Now, I wish to do the following tasks:
1. Exclude B.cpp from the application(A.EXE) and place/compile it in a
DLL (B.DLL).
2. A.EXE to load the above B.DLL dynamically (calling LoadLibrary) and
use the same extern variable concept.

I am fine/done with 1st step but got stuck with the 2nd task (being a
newbie)!!
Can someone shed some light here? (hopefully with an example)

P.S. I tried exporting the variable (Flag) in the B.DEF file but
couldn't move further.

Thanks in advance.
- VV
 
R

red floyd

I used to have a program(A.EXE) using an extern variable as follows:

A.cpp
extern DWORD Flag;

B.cpp
DWORD Flag;

Now, I wish to do the following tasks:
1. Exclude B.cpp from the application(A.EXE) and place/compile it in a
DLL (B.DLL).
2. A.EXE to load the above B.DLL dynamically (calling LoadLibrary) and
use the same extern variable concept.

DLLs and linking are beyond the scope of the language. I recommend a
newsgroup dedicated to your platform and/or development system.

http://www.parashift.com/c++-faq-lite/how-to-post.html#faq-5.9
 
J

Jim Langston

I used to have a program(A.EXE) using an extern variable as follows:

A.cpp
extern DWORD Flag;

B.cpp
DWORD Flag;

Now, I wish to do the following tasks:
1. Exclude B.cpp from the application(A.EXE) and place/compile it in a
DLL (B.DLL).
2. A.EXE to load the above B.DLL dynamically (calling LoadLibrary) and
use the same extern variable concept.

I am fine/done with 1st step but got stuck with the 2nd task (being a
newbie)!!
Can someone shed some light here? (hopefully with an example)

P.S. I tried exporting the variable (Flag) in the B.DEF file but
couldn't move further.

Thanks in advance.
- VV

Your question is pretty much off topic, but on topic to a certain extent.
Basically, however, what you want to do is OS specfic. extern works at link
time. A.cpp will use the same Flag from B.cpp when the objects are linked.
What you are talking about using the same variable in a different program (a
dll at this point) is run time. Ask in a newsgroup specific to your OS
(probably windows) and you'll probably be pointed to shared memory. Try
comp.os.ms-windows-programmer-win32 or the like.
 
S

Steve Pope

Your question is pretty much off topic, but on topic to
a certain extent. Basically, however, what you want to do
is OS specfic. extern works at link time. A.cpp will use the
same Flag from B.cpp when the objects are linked. What you are
talking about using the same variable in a different program
(a dll at this point) is run time. Ask in a newsgroup specific
to your OS (probably windows) and you'll probably be pointed
to shared memory.

I would try to avoid creating a need for functions in libraries
(dynamic or otherwise) and functions in the main executable
from needing to access the same global variables. To do so
would be poor encapsulation. It's much better if the library function
can work completely from its arguments, and the calling function
gets everything it needs from the return value of the library
function.

If you need a persistent state between function calls, consider
using a static variable.

Just an opinion.

Steve
 
V

vadnala

Thank you all..

(dynamic or otherwise) and functions in the main executable
from needing to access the same global variables. To do so
would be poor encapsulation. It's much better if the library function
can work completely from its arguments, and the calling function
gets everything it needs from the return value of the library
function.

If you need a persistent state between function calls, consider
using a static variable.

Just an opinion.

Steve
 

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

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,534
Members
45,007
Latest member
obedient dusk

Latest Threads

Top