• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 141
  • Last Modified:

Global variable to few allocated objects.

I have created an object (ActiveX dll) and I want all instances of the object to share the same global variable (if one instance changes the value of this variable then all other instances recognize the new value).

I want to be able to do that without using any methods or properties for this matter.

How do I do that?

0
Vad_sh
Asked:
Vad_sh
  • 12
  • 4
  • 4
1 Solution
 
caraf_gCommented:
Depends on what type of ActiveX component you're using. OK - you're using a DLL...

- Make it single threaded.

Now all instances of all classes defined in your project will share the same global data.

If it's an ActiveX Executable, make the threading model Thread Pool and create a single thread pool. Then your ActiveX.exe will behave just like your ActiveX dll with regards to global data.
0
 
caraf_gCommented:
PS - Global data MUST be declared as global in a .bas module in your project.

E.g.

Global gstrSomeGlobalStringVariable As String
0
 
caraf_gCommented:
In the light of the CS attitude towards point-stealing expressed in http://www.experts-exchange.com/jsp/qShow.jsp?ta=commspt2&qid=10320626  I feel I have no other option but to lock this question.

If you feel my answer is in any way unsatisfactory please ask for further information or reject this question. I would prefer it if you would reject the answer rather than grade me with a bad grade.
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!

 
Vad_shAuthor Commented:
caraf_q,

I declared a global variable with GLOBAL keyword in a .bas module before asking the question. It doesn't work!

Please explain to me about your words "single threaded".
0
 
caraf_gCommented:
If you look at the project properties for your ActiveX DLL, you'll see a frame titled "Threading Model"

The default value for ActiveX DLLs is "Apartment Threaded".

As a result, objects are created in thread "apartments". It's a bit too much to explain all this in detail here, and to be perfectly honest, I can't say I understand it well enough to give a good explanation to someone else....

Suffice to say that each of these "Apartments" has it's own set of global data, so that objects created in different "Apartments" cannot share global data.

By setting the Threading Model to Single Threaded, all objects in the DLL will run in the same "Apartment" and therefore will be able to share their global data. Problem solved ;-)
0
 
Vad_shAuthor Commented:
caraf_q,

It was really "Apartment Threaded" and I changed it to "Single Threaded", but...

It still doesn't work !!!

Here is the code I use in the class:

Public Sub SetValue(p_val As Integer)
  g_GlobalVariable = p_val
End Sub

Public Function GetValue() As Integer
  GetValue = g_GlobalVariable
End Function

Here is the code I use in the module:

Global g_GlobalVariable As Integer

I use those methods in two separate test applications and the value of g_GlobalVariable in the first test application does NOT change after I update it in the second test application.

Do you have any suggestions?
0
 
caraf_gCommented:
Aha... if you're running this from two separate *applications* then your best bet is to compile it as an ActiveX .exe

Change your project Type to ActiveX .exe

For activeX .exes the threading model is quite different. You can choose between Thread per Object and Thread Pool. Choose Thread Pool, and 1 Threads.

That should do the trick for you. Your code looks fine!
0
 
Vad_shAuthor Commented:
caraf_q,

I not use ActiveX exe , i am compaile
application as Standart Exe, in
this application I create inctanse of ActiveX.dll (part of the code is marked).

You mind that i mast change type of activeX dll to activeX exe and compaile?
0
 
caraf_gCommented:
That's correct. Your main application should still be a Standard .exe. But change your ActiveX .DLL to an ActiveX .EXE
0
 
KDivadCommented:
Interesting. I've tried that before (activex exe, thread pool, 1 thread, global variable in .bas module) and just tried it again and I still can't get it to work.
0
 
caraf_gCommented:
Okeedokee... I'll write up an example!

This will take a few minutes. Hang on...
0
 
caraf_gCommented:
PS - I'm assuming you are both using VB6. VB5's threading model is quite different.
0
 
KDivadCommented:
Ah, well in that case, never mind. I'm using VB5.
0
 
caraf_gCommented:
http://homepage.eircom.net/~carafa/TEMP/ShareGlobals.zip

KDivad, don't worry, it should work for VB5 too, except perhaps your project properties might be different. With a bit of experimentation you should be able to make it work...
0
 
KDivadCommented:
It took a couple minutes work to get it to compile (the compiled versions worked fine, but that didn't mean it would compile under VB5 and still work), but the project does work like it's supposed to! Many, many thanks!
Would you like a few extra points for your trouble? 50 and an "A"?
0
 
Vad_shAuthor Commented:
caraf_q,

I was down load example.
Examle very nice!!!!

Thank you very mach.
I belive that it help in future.
0
 
caraf_gCommented:
KDivad, of course ;-) That'd be nice

(Point-horny <g>)
0
 
KDivadCommented:
caraf_g, ;-) Look for the post!
0
 
caraf_gCommented:
Can't find it.... What's the URL?
0
 
caraf_gCommented:
<doh> It just appeared...
0

Featured Post

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!

  • 12
  • 4
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now