Solved

DCOM and WithEvents

Posted on 2000-04-13
9
514 Views
Last Modified: 2012-06-21
Hi,

Imaging the scenario where a hotel has 5 reception desks. Each reception desk has a computer which displays the room availability. However, when one of the receptionists makes a booking, I want the other screens to update immediately (or as near as).

I've looked into DCOM and WithEvents etc.. but can't work out how to fit it all together.

I've created an ActiveX exe, containing the following code...



'-------  Class: clsTestEv  ---------
Option Explicit
Public Event Updated(Fred$)

Public Sub DoIt()
    RaiseEvent Updated("Do it")
End Sub

Public Sub EndIt()
    RaiseEvent Updated("End it")
End Sub
'----------------------------------

The class name would be 'testev.ClsTestEv'

I then created project 2, which is hopefully going to create a reference to the clsTestEv class, call the 'DoIt' procedure, and then receive an event...

what am I doing wrong ???

here's my code..

'-------Form : Form1---------
Private WithEvents blob As clsTestEV
Option Explicit

Private Sub Form_Load()
Dim x As Object
Set x = CreateObject("testev.clstestev")
x.DoIt
Set x = Nothing
End Sub

Private Sub blob_Updated(Fred As String)
    MsgBox Fred
End Sub
'----------------------------------


The object gets created, but when I call 'doit', the messagebox does not pop up. Surely it should ???

Once this is working, and I'm fairly confident I'll get this bit working ok, how do I declare the 'WithEvents' when the object I'm creating is on a remote machine ??, I can create the object ok using the  createobjects("testev.clstestev","machinname"), but how do I tell WithEvents what the object is ???
0
Comment
Question by:smegghead
  • 5
  • 3
9 Comments
 
LVL 6

Expert Comment

by:Marine
ID: 2712674
Hi can you try this? I created recently an app where i raise my own events ant it worked for me. Here is the code i used maybe it can work out for you.Good Luck

in Class.
--Class Code----
Public Event Change(sSql as string,lst as ListView)
Public Function SelCategory(Book As String, lstBooks As ListView)
Dim sSql As String
On Error GoTo err_book
Call Con 'opens connection
RaiseEvent Change(Book, lstBooks)
cnAcct.Close
Exit Function
err_book:
msgbox err.description
End Function

---Form Code---
Private WithEvents objBook As IBooks
Private Sub objBook_Change(sSql As String, lst As MSComctlLib.ListView)
Dim rs As New ADODB.Recordset, lstItem As ListItem, i As Integer
On Error GoTo err_Rec
rs.Open sSql, cnAcct, adOpenStatic, adLockPessimistic
If rs.RecordCount < 1 Then
    MsgBox "NO RECORDS HAVE BEEN FOUND ON THIS TOPIC" & Chr(13) & Chr(10) & _
           "            PLEASE SELECT ANOTHER TOPIC", vbInformation, "Search Found 0 Records"
    Exit Sub
End If
rs.MoveFirst
Do While Not rs.EOF
        Set lstItem = lst.ListItems. _
        Add(, , CStr(rs!Author))
        For i = 1 To lst.ColumnHeaders.Count - 1
            lstItem.SubItems(i) = IIf(Not IsNull(rs(i)), rs(i), "")
        Next
        rs.MoveNext
Loop
        rs.Close
Exit Sub
err_Rec:
MsgBox "ERROR OCCURED WHILE RETRIEVING RECORDS"
Err.Clear
End Sub
0
 
LVL 10

Author Comment

by:smegghead
ID: 2712781
Hmm, I can't see anything different in your code except that you seem to be running it within one project ?? I want to compile the ActiveX exe and use it remotely & locally.
0
 
LVL 3

Expert Comment

by:Joebob
ID: 2712963
The reason that your code is not working is because you declare a variable blob to recieve events, but you never assign it to anything.  You instead create another variable x that creates the object and calls the doit method.  If you are going to use x to call the doit method then x needs to be declared withevents.  Another way that might work is to set blob equal to x before calling the doit method.
0
 
LVL 3

Expert Comment

by:Joebob
ID: 2712976
Didn't see the rest of your question.  

You asked:
Once this is working, and I'm fairly confident I'll get this bit working ok, how do I declare the 'WithEvents' when the object I'm creating is on a remote machine ??, I can create the object ok using the  createobjects("testev.clstestev","machinname"), but how do I tell WithEvents what the object is ???

My Answer:
You can't use late binding.  You must declare your variable as the correct projectname.classname datatype.  The CreateObject function doesn't require that the variable it is returning the value to is of type object, it just requires that it is either a <font color="red">generic object</font> OR the <font color="red">correct object</font>.
0
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 10

Author Comment

by:smegghead
ID: 2713005
Thanks JoeBob

So, if I can't use late binding, how do I declare the object when it resides on another machine, do I have to register a local copy ?? surely not...

Is there another program that allows me to set up a reference to an object on a remote machine ??

PS. Does this approach sound like the most appropriate for what I am trying to achieve ??

PPS. I'm leaving work now, so I may go quiet for a few hrs.. (til I get home - sad eh ??)
0
 
LVL 3

Accepted Solution

by:
Joebob earned 75 total points
ID: 2713155
You have to have a copy on your development environment with a reference placed in your VB project, but you don't need a local copy at runtime.  (At least I am pretty sure of this).  When you run createobject with a remote PC listed, the interface definition will be sent from the remote computer to the local PC, therefore, you shouldn't need a local copy.

I foresee a big problem with your approach based on what you are trying to achieve.  With an ActiveX EXE, like all other classes, the event will be raised to only the owner or container of the class.  What this means is that PC1 and PC2 both will instantiate a copy of your class in the ActiveX EXE, but when PC1 tells the class to raise an event, PC1 is the only person that will receive the event.

I think I might have a way of working around this.  What I am going to decribe is sortof going against the rules of classes and object oriented design, but I am fairly certain that it will work.

You could do something along the lines of creating a global array that you add to everytime that a class is instantiated and, of course, remove from when a class is destroyed.  You could store a reference into this array by doing something like this in the Class_Initialize sub:
 Set Array(i) = Me.

You would then create a Friend method that could be called like Me.FireDoIt.  

Lastly you would create a function, likely in a standard module, that would do something like this:
 For i=lbound(Array) to ubound(Array)
   Array(i).FireDoIt
 Next i

I hope that I have been clear about this, but I have had a long day and my mind is a little on the fritz.

Another much more difficult way would be to send a IP message from the PC that makes the change to all of the other PCs.  You would likely want to use the UDT protocol for simplicity, but it still wouldn't be all that simple.
0
 
LVL 10

Author Comment

by:smegghead
ID: 2833236
I've now done this using Tcp/Ip, it works very well.
0
 
LVL 10

Author Comment

by:smegghead
ID: 2896926
Comment accepted as answer
0
 
LVL 10

Author Comment

by:smegghead
ID: 2896927
Hi

Thanks for your help joebob / marine.. I've done it a different way now, using TCPIP, but I'm sure your answers were correct.

Rich.

PS. I awarded the points to joebob, 'cos he mentioned using TCPIP, sorry marine !!
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

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…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

705 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

18 Experts available now in Live!

Get 1:1 Help Now