Click to See Complete Forum and Search --> : I hope I word this correctly... [Resolved]
Dave Sell
Sep 13th, 2004, 04:19 PM
Please read what I am trying to do, maybe you can offer me some guidance - all I really need is a book for reference and help:
I want MyProject.MyClass, an ActiveX EXE to load when the OS loads, like a service that runs all day.
Then in VB6APP1 I create an Object reference:
Set Object = Server.GetObject(MyProject.MyClass)
Lets say I change a Property:
Object.State = 12
Then VBAPP1 goes away:
Set Object = Nothing
Then an hour later in VB6APP2, a totally different app, wants to do the same:
Set Object = Server.GetObject(MyProject.MyClass)
Lets say I test the same Property:
intState = Object.State ' I excpect intState to be 12
Then VB6APP2 goes away:
Set Object = Nothing
Is such a thing possible in the realm of COM and VB6?
I 'm pretty sure this can be done with C++, but I would very much like to stay working with either VB6 or VB.NET. In other words I need to make a persistent ActiveX EXE server. or service (i think!)
Thanks for any advice!
Dave Sell
< Edit: Changed CreateObject to GetObject >
shunt
Sep 22nd, 2004, 09:59 AM
Dave, I have tried to do the exact same thing a few years ago.
I have tried making an ActiveX EXE as a windows service. As I can remember, I got it working using Visual Basic 6 and the NT Service control. But, it only seems to work on Windows 2000. When I installed it on NT4, the app would spin into a loop and memory would go through the roof. Seems like a bug in the NT Service control. I just feel thats a little dicey! It works 100% with standard exe, but then you wont be able to connect to it. Well... Unless you develop a winsock with your own protocol.
http://www.vbforums.com/showthread.php?s=&threadid=151900&highlight=ntservice+control
Another option is to write your ActiveX EXE and start in on OS startup using the Windows Scheduler. The scheduler has options to execute applications on OS startup. You will then be able to connect to the classes using GetObject.
And last, but by NO means least... There's .NET. You can develop a fully fledged Windows Service using VB.NET. It is stable and you will be able to connect to the same instance using remoting.
Dave Sell
Oct 30th, 2004, 11:04 AM
I got this working using the Singleton approach.
shunt
Nov 1st, 2004, 03:24 AM
The Singleton is the best design!
Have you ever managed to get the GetObject function working with your own classes in VB6? I have tried it before, but apparently you can only use GetObject on classes written in C++....???
What implementation did you use to get it running as a "service"?
Dave Sell
Nov 1st, 2004, 09:17 AM
Originally posted by shunt
The Singleton is the best design!
Have you ever managed to get the GetObject function working with your own classes in VB6?
Yes!
I have tried it before, but apparently you can only use GetObject on classes written in C++....???
Not only do I have local VB6 COM clients calling GetObject to instantiate the same instance of a Singleton, but I also have REMOTE VB6 DCOM clients calling GetObject to instantiate the same instance of the same Singleton!
What implementation did you use to get it running as a "service"?
Technically I don't have it running as a service. I was going to research that avenue, but as it turns out, the first client to call upon my Singleton makes the SIngleton get loaded up into memory. So making it a service seems a bit redundant. Although, in another part of my design I do have need to make a normal VB6 app into a service, so I will be researching that also.
shunt
Nov 1st, 2004, 11:05 AM
Could you post a code example of the GetObject function in action, with the class that it is consuming... For some reason, I cant get the GetObject function to work.... :(
Dave Sell
Nov 1st, 2004, 11:13 AM
I can post some of the code, but not all of the code. A certain part of the code I got from buying a book. I do not have permission to distribute this source without the author's permission.
If you buy the book, then you can have all the source.
Dave Sell
Nov 1st, 2004, 02:21 PM
Originally posted by shunt
Could you post a code example of the GetObject function in action, with the class that it is consuming... For some reason, I cant get the GetObject function to work.... :(
Are you going to get the book? I am really looking forward to sharing what I have made with someone else. It is quite astonishing to me how little code this took of mine (~100 lines) and how fully functional this has become.
Currently, I can have multiple clients "tune in" to events happenning on the server, all Events Raised by the same object. All listening clients (Observers) will catch all events they want to listen to at the same time.
Also, any client can initiate an Event for the server to re-throw for all the other clients. Basically its a "webbing" of event chains.
Dave Sell
Nov 1st, 2004, 02:41 PM
Looks like you can also get the source by becoming a premier member here:
http://www.devx.com/
Jaiboy
Nov 2nd, 2004, 05:48 AM
I have implimented this logic well in my company to create a monitoring application. There is no need to make the Active Exe as a service. All you need to do is a wrapper class hat has to be loaded when windows OS loads.
I will explain.
Create and compile the following
MyProject.MyClass 'Public Not Creatable Class
MyProject.WrapperClass
In this wrapper class you have to instanciate the MyClass Object and maintain. All MyApp1 and MyApp2 should request WrapperClass to provide the MyClassObject.
Means a function should be written inside the wrapper like this.
'Pub Var
dim MyObj as MyClass1
Function GiveMyClass () as MyClass
If MyObj is nothing then Set MyObj = New MyClass1
GiveMyClass = MyObj
End Function
shunt
Nov 2nd, 2004, 10:19 AM
And does this work with the GetObject function?
Dave Sell
Nov 2nd, 2004, 10:23 AM
Originally posted by shunt
And does this work with the GetObject function?
Only if it regeisters itself with the ROT.
shunt
Nov 2nd, 2004, 10:33 AM
Only if it regeisters itself with the ROT
And in that lies the problem! You cant do this using standard VB6 created COM objects... You have to use some really nasty Windows API's as far as I know... Right? Or is there some way of compiling your component so the class registers itself in the ROT?
Dave Sell
Nov 2nd, 2004, 10:39 AM
You can. I did. It works. I used some source code called VBoost to get a great deal of robustness. Earlier last week someone posted some very straightforward code to do the same thing, albiet less robust.
The VBoost objects just get added to your project and you basically never look at the code. You just instantiate an ROTHook object and use it in your project which amounts to 3 lines of code.
Dave Sell
Nov 2nd, 2004, 10:40 AM
Originally posted by shunt
Or is there some way of compiling your component so the class registers itself in the ROT? Nope. Not built into VB6 natively, but neither are Winsocks, Webbrowser control, or myriad more 3rd party controls.
You add them, you use them, they just work.
shunt
Nov 3rd, 2004, 02:09 AM
Ok. I see....
You would think that VB6 would provide functionality to use the ROT since they provide the GetObject function which requires it...???
Anyway, no big deal. Using the singleton pattern with a server and client class, you can basically get the same thing. You just wont be able to use GetObject....
Dave Sell
Nov 3rd, 2004, 02:28 AM
Originally posted by shunt
Ok. I see....
You would think that VB6 would provide functionality to use the ROT since they provide the GetObject function which requires it...???
Anyway, no big deal. Using the singleton pattern with a server and client class, you can basically get the same thing. You just wont be able to use GetObject....
Repeat after me: You can use GetObject to access a Singleton created with VB6 from a local or a remote client.
Dave Sell
Nov 3rd, 2004, 02:31 AM
Originally posted by Jaiboy
I have implimented this logic well in my company to create a monitoring application. There is no need to make the Active Exe as a service. All you need to do is a wrapper class hat has to be loaded when windows OS loads.
I will explain.
Create and compile the following
MyProject.MyClass 'Public Not Creatable Class
MyProject.WrapperClass
In this wrapper class you have to instanciate the MyClass Object and maintain. All MyApp1 and MyApp2 should request WrapperClass to provide the MyClassObject.
Means a function should be written inside the wrapper like this.
'Pub Var
dim MyObj as MyClass1
Function GiveMyClass () as MyClass
If MyObj is nothing then Set MyObj = New MyClass1
GiveMyClass = MyObj
End Function
So do you have to take any extra steps in the Control Panel Services area to Add your new class as a service once you do the above code?
shunt
Nov 4th, 2004, 02:49 AM
In the code from Jaiboy's post, a client trying to get an instance of the MyClass will have to instanciate a WrapperClass using the New keyword or CreateObject and then run the GiveMyClass function to return the "Singleton" MyClass.... No GetObject here...!
You can use GetObject to get "Singleton" MyClass directly, but you have to write some really nasty ROT API management code into MyClass first. So, unless you are going to buy that book for the code... you are not going to be using GetObject.
Dave Sell
Nov 4th, 2004, 08:32 AM
YOu can do it, with and without that book. I'll put some things together to show you once and for all. :bigyello:
Dave Sell
Nov 4th, 2004, 01:18 PM
Originally posted by shunt
In the code from Jaiboy's post, a client trying to get an instance of the MyClass will have to instanciate a WrapperClass using the New keyword or CreateObject and then run the GiveMyClass function to return the "Singleton" MyClass.... No GetObject here...!
You can use GetObject to get "Singleton" MyClass directly, but you have to write some really nasty ROT API management code into MyClass first. So, unless you are going to buy that book for the code... you are not going to be using GetObject.
Here, dude. Here is a Singleton Server progect that loads itself into the ROT so that clients can use GetObject to isntantiate the same instance of it. This also works across DCOM to a degree.
Just make sure to compile the Singleton with Binary compatibility.
I have included 2 different kinds of clients which use it. Using that book I mentioned is not necessary here.
Not a service yet, but I believe it can be done. I will have to look into that some more.
Dave Sell
Nov 9th, 2004, 04:24 PM
Well, Shunt, do you believe me now?
shunt
Nov 10th, 2004, 02:24 AM
Yes Dave. Thanks for that example! I have been looking for that for a while now.
vbforums.com
Copyright Internet.com Inc., All Rights Reserved.