By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,136 Members | 1,021 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,136 IT Pros & Developers. It's quick & easy.

extern variable

P: n/a
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

Dec 24 '06 #1
Share this Question
Share on Google+
4 Replies


P: n/a
va*****@gmail.com wrote:
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-lit...t.html#faq-5.9

Dec 24 '06 #2

P: n/a
<va*****@gmail.comwrote in message
news:11**********************@i12g2000cwa.googlegr oups.com...
>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.
Dec 25 '06 #3

P: n/a
Jim Langston <ta*******@rocketmail.comwrote:
><va*****@gmail.comwrote in message
>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)
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
Dec 25 '06 #4

P: n/a
Thank you all..

On Dec 25 2006, 1:20 am, spop...@speedymail.org (Steve Pope) wrote:
Jim Langston <tazmas...@rocketmail.comwrote:
<vadn...@gmail.comwrote in message
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)
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
Jan 30 '07 #5

This discussion thread is closed

Replies have been disabled for this discussion.