Link to home
Start Free TrialLog in
Avatar of joevm3
joevm3Flag for United States of America

asked on

Multiple instances of ActiveX OCX share glabal variables by default; how do i make each instance unique

i have an OCX written in MS C++ 6 that i am using in VB6
it works fine but when i load multiple instances of the OCX, say 2 on a vb form the global variables are shared between the two of them. here is an example:

in the ControlNameCtl.cpp file if i have:

int hello=0;

int SomeFunction(short param1)
{
hello++;
return hello;
}

----------
now if i load two OCX's and i call SomeFunction on the first,it will return 1 [expected]
and then if i call SomeFunction on the second OCX it will return 2 [unwanted]

i tried using the static keyword but i had no luck with that.

Thank you for your help,
Joe
Avatar of AlexFM
AlexFM

I don't beleive that this happens, possibly there is something else in the code. In any case, make hello variable member of the control class, this should help.
ASKER CERTIFIED SOLUTION
Avatar of AlexFM
AlexFM

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
AlexFM is right.

To share variables across instances you need to specify Data segment as shared.
// Global and static member variables that are not shared.

#pragma data_seg("SHARED")  // Begin the shared data segment.
// Integers, char[] arrays and pointers. These variables declared here are shared
#pragma data_seg() // End the shared data segment and default back to
                              // the normal data segment behavior.

//This tells the linker to generate the shared data segment.  
#pragma comment(linker, "/section:SHARED,RWS")

I think your code pasted here is incomplete or your are missing something.
Avatar of joevm3

ASKER

I tried #pragma data_seg() before the declaritions.
It did not work.

AlexFM, how do i make the variable 'hello' a member of the control class?


btw the code is just an example as the actual code is quite large, i don't think there is anything in the code affecting the default compiler settings.

Thank you,
Joe
Avatar of joevm3

ASKER

Okay it almost works like this:
if i use the ide to make a member variable [public] it works (the variables are not shared) but i can only access the variable within that class (CActivePhotonCtrl)
i tried using  extern int hello;  but that failed an i also tried  CActivePhotonCtrl::hello++;  but that also failed with the error "error C2597: illegal reference to data member 'CActivePhotonCtrl::hello' in a static member function"

how may i access a public member variable belonging to CActivePhotonCtrl from another class?

Thank you,
Joe
Using pointer to this class. This is not specific to COM, just C++.

class A
{
    public:
       int x;
};

from any plase having A* pA pointer:
pA->x = 1;

Static class member will be also shared between all control instances, you need non-static member.