Solved

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

Posted on 2004-04-01
6
456 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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

Suggested Solutions

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

739 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