Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2004-04-01
6
Medium Priority
?
478 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 2000 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

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: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying 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

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

885 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