Solved

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

Posted on 2004-04-01
6
426 Views
Last Modified: 2008-02-01
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
0
Comment
Question by:joevm3
  • 3
  • 2
6 Comments
 
LVL 48

Expert Comment

by:AlexFM
ID: 10738712
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.
0
 
LVL 48

Accepted Solution

by:
AlexFM earned 500 total points
ID: 10738723
Actually, OCX is loaded twice from the same process, so global variables are shared. Use global variables only if you want them to be shared for all controls loaded to the same application, and use control class members if you want them to be unique in each control instance.
0
 
LVL 3

Expert Comment

by:akalmani
ID: 10739718
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.
0
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 

Author Comment

by:joevm3
ID: 10744119
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
0
 

Author Comment

by:joevm3
ID: 10744300
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
0
 
LVL 48

Expert Comment

by:AlexFM
ID: 10746692
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.
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Suggested Solutions

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now