Solved

Global variable to few allocated objects.

Posted on 2000-04-03
20
125 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
  • 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
 

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
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
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…

707 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now