Static member variable of a class in a DLL

Discussion in 'OT Technology' started by uneek, Jan 21, 2006.

  1. uneek

    uneek OT Supporter

    Joined:
    Dec 14, 2003
    Messages:
    12,002
    Likes Received:
    0
    simple class definition:

    Code:
     
    class [SIZE=2][COLOR=#0000ff]__declspec[/COLOR][/SIZE][SIZE=2]([/SIZE][SIZE=2][COLOR=#0000ff]dllexport[/COLOR][/SIZE][SIZE=2]) A[/SIZE]
      {
      public:
        A([SIZE=2][COLOR=#0000ff]void[/COLOR][/SIZE]);
        ~A([SIZE=2][COLOR=#0000ff]void[/COLOR][/SIZE]);
    [SIZE=2][COLOR=#0000ff]
        static [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]unsigned [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]int[/COLOR][/SIZE][SIZE=2] gm_ulT;[/SIZE]
      };
    
    implementation:

    Code:
    [COLOR=#0000ff]unsigned [/COLOR][SIZE=2][COLOR=#0000ff]int [/COLOR][/SIZE]A::gm_ulT = 10l;
    
    this class is defined and implemented within a dll.

    my problem is that I want to be able to have multiple instances of this class but have the static member variable shared among them, but if multiple applications are running simultaneously, each use of the DLL uses a different variable.

    this is for a server application and I want this class (assume it's a client) to have certain static members, so say there are 10 clients connected at once, they share this var.... but if the user opens up a 2nd server application I want that instance of the app to be able to define a new value for the static variable.... but only for the 2nd app.

    how do I do this?

    [edit]
    well I found this:
    http://msdn.microsoft.com/library/en-us/dllproc/base/dynamic_link_library_data.asp

    and it looks like the behavior I want is the default for MSVC++ .... but the link says the following as well:

    'Your development tools may allow you to override the default scope of global and static variables. For more information, see the documentation included with your development tools.'

    So how would I do this for VC++?? I do not see any information from the link regarding this :(
     
    Last edited: Jan 21, 2006
  2. samm

    samm Next in Line

    Joined:
    Dec 22, 2000
    Messages:
    2,630
    Likes Received:
    0
    Location:
    San Jose, CA
    I don't understand your question. Static member variables are shared by all instances of a class by default.
     
  3. uneek

    uneek OT Supporter

    Joined:
    Dec 14, 2003
    Messages:
    12,002
    Likes Received:
    0
    But each process has its own context so static variables are switched same as the registers / switchframe.

    This is what MSDN is saying anyway, although I can accept that MSDN is wrong.

    What I am concerned about is not whether MSDN is accurate in this regard but how to change the behavior, period.

    For example, if MSDN is correct and context switching also switches any static variables, then how do I change this behavior to make static variables global across ALL processes?

    And if MSDN is incorrect and there is one copy of a static variable for the entire machine, how do I change this behavior to make it local to a specific context?

    MSDN says this can depend on the compiler - but they don't tell me in that article how to do this with visual c++
     

Share This Page