extern variable

Discussion in 'C++' started by vadnala@gmail.com, Dec 24, 2006.

  1. Guest

    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, 2006
    #1
    1. Advertising

  2. red floyd Guest

    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-lite/how-to-post.html#faq-5.9
     
    red floyd, Dec 24, 2006
    #2
    1. Advertising

  3. Jim Langston Guest

    <> wrote in message
    news:...
    >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.
     
    Jim Langston, Dec 25, 2006
    #3
  4. Steve Pope Guest

    Jim Langston <> wrote:

    ><> wrote 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
     
    Steve Pope, Dec 25, 2006
    #4
  5. Guest

    Thank you all..

    On Dec 25 2006, 1:20 am, (Steve Pope) wrote:
    > Jim Langston <> wrote:
    > ><> wrote 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, 2007
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Thomas Matthews
    Replies:
    5
    Views:
    2,434
    tom_usenet
    Aug 2, 2004
  2. Dan Elliott
    Replies:
    4
    Views:
    13,262
    Tom Widmer
    Dec 8, 2004
  3. smackdab

    extern access to variable

    smackdab, Dec 8, 2003, in forum: C Programming
    Replies:
    1
    Views:
    449
    Jack Klein
    Dec 8, 2003
  4. Bangalore

    extern struct variable

    Bangalore, Aug 2, 2005, in forum: C++
    Replies:
    5
    Views:
    445
    Srini
    Sep 6, 2005
  5. Andre
    Replies:
    5
    Views:
    542
    Keith Thompson
    Jul 17, 2012
Loading...

Share This Page