Solved

Global variable to few allocated objects.

Posted on 2000-04-03
20
128 Views
Last Modified: 2010-05-02
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
Comment
Question by:Vad_sh
[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
  • 12
  • 4
  • 4
20 Comments
 
LVL 10

Expert Comment

by:caraf_g
ID: 2680224
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
 
LVL 10

Expert Comment

by:caraf_g
ID: 2680229
PS - Global data MUST be declared as global in a .bas module in your project.

E.g.

Global gstrSomeGlobalStringVariable As String
0
 
LVL 10

Accepted Solution

by:
caraf_g earned 150 total points
ID: 2680318
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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 

Author Comment

by:Vad_sh
ID: 2680384
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
 
LVL 10

Expert Comment

by:caraf_g
ID: 2680460
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
 

Author Comment

by:Vad_sh
ID: 2680680
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
 
LVL 10

Expert Comment

by:caraf_g
ID: 2680738
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
 

Author Comment

by:Vad_sh
ID: 2680784
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
 
LVL 10

Expert Comment

by:caraf_g
ID: 2681773
That's correct. Your main application should still be a Standard .exe. But change your ActiveX .DLL to an ActiveX .EXE
0
 
LVL 5

Expert Comment

by:KDivad
ID: 2682760
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
 
LVL 10

Expert Comment

by:caraf_g
ID: 2682779
Okeedokee... I'll write up an example!

This will take a few minutes. Hang on...
0
 
LVL 10

Expert Comment

by:caraf_g
ID: 2682785
PS - I'm assuming you are both using VB6. VB5's threading model is quite different.
0
 
LVL 5

Expert Comment

by:KDivad
ID: 2682829
Ah, well in that case, never mind. I'm using VB5.
0
 
LVL 10

Expert Comment

by:caraf_g
ID: 2682834
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
 
LVL 5

Expert Comment

by:KDivad
ID: 2683319
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
 

Author Comment

by:Vad_sh
ID: 2683519
caraf_q,

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

Thank you very mach.
I belive that it help in future.
0
 
LVL 10

Expert Comment

by:caraf_g
ID: 2683580
KDivad, of course ;-) That'd be nice

(Point-horny <g>)
0
 
LVL 5

Expert Comment

by:KDivad
ID: 2686561
caraf_g, ;-) Look for the post!
0
 
LVL 10

Expert Comment

by:caraf_g
ID: 2686584
Can't find it.... What's the URL?
0
 
LVL 10

Expert Comment

by:caraf_g
ID: 2686585
<doh> It just appeared...
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

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

Title # Comments Views Activity
JSON Response and request in VB6 application 11 817
Spell Check in VB6 13 137
Advice in Xamarin 21 109
Formula problem with Excel attachment 6 43
Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

733 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