Solved

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

Posted on 2004-04-01
6
446 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
Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

 

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
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 be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

830 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