Click to See Complete Forum and Search --> : Connecting to an Instance.
mustafi
Oct 29th, 2001, 02:32 PM
Hi
Lets say you create an object and it runs in its own process space. It has the ability to provide certain services. Is it at all possible to connect to a running instance of that object. By connecting I mean not creating your own instance of that object but simply connecting to an already runing instance.
Mustafi *
jim mcnamara
Oct 29th, 2001, 04:04 PM
Yes. Use
Set myNewObj = GetObject("object name","servername")
Servername is only required when it is on another node.
mustafi
Oct 30th, 2001, 01:05 PM
Thanks Jim
However, GetObject only works if the componant you are trying to connect to wasnt written in VB.
jim mcnamara
Oct 30th, 2001, 01:26 PM
Sorry. That is not correct.
COM is absolutely not dependant on the source language. COM is purely binary. I can access a COM object written in C++ from any language that supports COM; one written in VB from VB, C++, J++, etc.
Believe me or not, makes no difference - but consider doing some reading. VB COM from Wrox Press is for VB development and deployment of COM objects. It's aimed completely at VB programmers. It also explains the limitations of GetObject vs. CreateObject. And how COM works.
Lethal
Oct 30th, 2001, 10:55 PM
Well put Jim. That's the beauty of COM, Seamless...;)
mustafi
Oct 31st, 2001, 12:48 PM
Hi Jim,
I hope you swallow :-)
Check it out:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vbenlr98/html/vamsgcantcreateobject.asp
Take special note of:
"You are trying to use the GetObject function to retrieve a reference to class created with Visual Basic.
GetObject can't be used to obtain a reference to a class created with Visual Basic."
If you find any info that says otherwise, please drop me a line!
DaMo
Oct 31st, 2001, 05:49 PM
Sorry Jim.
To be honest I was expecting the same, but we can't use the getobject function. See what the GetObject function says:
"If an object has registered itself as a single-instance object, only one instance of the object is created, no matter how many times CreateObject is executed. With a single-instance object, GetObject always returns the same instance when called with the zero-length string ("") syntax, and it causes an error if the pathname argument is omitted. You can't use GetObject to obtain a reference to a class created with Visual Basic."
jim mcnamara
Nov 1st, 2001, 08:00 AM
Yes, but that is because VB lets you create single instance objects. It's not because the code was written in VB per se.
If you create a single instance object in C++ you have the same problem.
If I read it right, Mustafi created an ActiveX server object. These are usually not single instance objects, because that defeats the purpose of an ActiveX server on a separate node. This would mean a new process for every connection.
What all this means is:
If you think you have an instance of an object running somewhere, you normally call GetObject. Why? because it is more efficient. CreateObject makes an all-new instance.
GetObject will fail in the special circumstance you're talking about.
I've got COM code all over the place written in VB. Some of it works with GetObject.
We're talking apples & oranges here. Like I said, it makes no difference if you want to see my point of view or not. Just go learn how it all really works, then tell me I'm full of it. That'll be fine. Meanwhile use New and CreateObject.
What most programmers want is to get past a problem. Just that.
mustafi
Nov 1st, 2001, 08:35 AM
Hiya Jim
I agree fully, all I need is a solution to the problemo. So
if I want a single-instance object and need to use this instance in multiple apps what are my options.
Much Appreciated.
Mustafi *
shunt
Nov 2nd, 2001, 12:46 AM
Hi Jim,
I would like to know how you got the GetObject function to work with any of your VB code? I have been researching this for weeks and come up with 1 answer each time; IT AINT POSSIBLE!
Further investigation showed that this is because the VB objects are not inserted into the Running Object Table. The GetObject function retrieves a reference to the running instance of an object by querying this table. The objects reference can be inserted and removed using these two functions:
RegisterActiveObject and RevokeActiveObject.
These are a part of the OLE api and not of the opperating system. I have looked for the VB declarations in MSDN and all over the internet but haven't come up with anything.
Yes we agree that COM is not language dependent, but its functionality is. This is purely because of the way in which VB lets us CreateObjects. We are not given the opportunity to register the objects in the ROT when using VB. It seems in many instances, VB simplifies a lot of what is needed to create objects and applications (Hence "Basic").
-----------------------------------------------------------------------
Looking again through MSDN does however provide an alternative. This would be to use a MultiUse ActiveX .exe. Within this component, there must be a minimum the following:
A public not creatable class (X)
A public multiuse class (Y)
A standard module (Z)
X is declared as public in Z. This makes it available to the entire component. A number variable(glngRefCount) is also declared as public in Z. This will be the reference counter (ie. How many applications are connected).
When an application creates the object, the first class to be created will be Y. In the initialization, a check should be placed to check the value of glngRefCount. If the count is 0 then this is the first application to connect, so create X and increment glngRefCount. If not, just increment glngrefcount since X is already created.
It is important to note at this point that a MultiUse ActiveX .exe component is only created once within a system. It is the classes within this component which have multiple instances. Hence, the value of a public variable within a standard module will be available to all instances of the classes within the component.
X will therefore be available to all instances of Y that are created. You can then manipulate X through Y from any one of the connected clients.
---------------------------------------------------------------------------
Sharing the CoffeMonitor (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vbcon98/html/vbconsharingcoffeemonitor.asp)
RegisterAvtiveObject in C++ (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/automat/htm_hh2/chap5_2k38.asp)
vbforums.com
Copyright Internet.com Inc., All Rights Reserved.