Solved

Implementing remote dll callbacks as events in VB.Net?

Posted on 2007-04-05
5
637 Views
Last Modified: 2013-11-26
This is a VB.Net Question
OK, so I've figured (with some help) out how to receive an asynchronous callback from a remote dll written in C++ using delegates.  But I've also just figured out that a) these are returned as separate thread and b) I don't -need- a separate thread.  What I'd like to do is receive the callback as an event (if possible).
So this is a four part question:
1)  Is it possible to convert a callback from a remote dll (using a delegate of course) as an event?
2) If  the answer to 1 is yes, does this mean that I can enable that event to act as other events in VB.Net, that is to interrupt the single thread to process the event -and- have full access to the UI so that returns from the dll can be written to form controls directly?
3) If this isn't possible, is there any value in considering events, that is should I simply resign myself to handling the callbacks via separate threads?
4) Any code examples of the 2 above (if 1 is yes) would be great (I have examples of 3 thanks)
pgb
0
Comment
Question by:brownpeterg
[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
  • 4
5 Comments
 
LVL 27

Expert Comment

by:VBRocks
ID: 18857885
1.  The answer to your first question, as I understand it, is no.  In VB.NET you have to use a Delegate.
2.  I believe the answer is no.
3.  To the third question, I believe you can handle it on the same thread.
4.  I'll post you an example that I hope will be helpful for you.
0
 
LVL 27

Expert Comment

by:VBRocks
ID: 18857897
First off, here is a link that I found to be very helpful.  The example is in C#, but I made it work in VB.NET.

Remoting example:
http://www.codeproject.com/csharp/RemotingChatSample.asp

0
 
LVL 27

Accepted Solution

by:
VBRocks earned 500 total points
ID: 18857963
Here is an example:

First off, in VB.NET you have to add a reference to the System.Runtime.Remoting dll.

'This is an example of the Child:
Imports Remote
Imports System.Runtime.Remoting
Imports System.Runtime.Remoting.Channels
Imports System.Runtime.Remoting.Channels.Tcp
Imports System.Runtime.Remoting.Services

Public Class Form1
    Private RTransfer As Remote.RemoteTransfer = Nothing
    Private Service As Runtime.Remoting.ObjRef = Nothing
    Private Channel As TcpChannel = Nothing
    Private tmr As System.Windows.Forms.Timer

    Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        StopListening()

    End Sub


    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        StartListening()

        tmr = New Timer()
        AddHandler tmr.Tick, AddressOf OnTick
        tmr.Interval = 1

    End Sub

    Private Sub StartListening()
        StopListening()

        Try
            Channel = New TcpChannel(1500)
            ChannelServices.RegisterChannel(Channel, False)

            RTransfer = New Remote.RemoteTransfer()
            Service = RemotingServices.Marshal(RTransfer, "DALCTransferService")

            RTransfer.SlaveCall = New Remote.RemoteTransfer.delSlaveCall(AddressOf OnRequest)


        Catch ex As Exception
            MessageBox.Show("Error Listening: " & ex.InnerException.ToString(), "Child")

        End Try

    End Sub

    Private Sub StopListening()
        If Service IsNot Nothing Then
            RemotingServices.Unmarshal(Service)

        End If

        If RTransfer IsNot Nothing Then
            RemotingServices.Disconnect(RTransfer)

        End If

        If Channel IsNot Nothing Then
            ChannelServices.UnregisterChannel(Channel)

        End If

        Service = Nothing
        RTransfer = Nothing
        Channel = Nothing
    End Sub

    Private Sub OnRequest()

        'tmr.Start()
        MessageBox.Show("Request Received", "Child")

        'tmr.Stop()
    End Sub

    Private Sub OnTick(ByVal sender As Object, ByVal e As System.EventArgs)
        'Not Needed?
        Me.Label1.Text = "Request Received"
    End Sub

End Class


'Here is an example of a Remote.dll that I made:
Imports System
Imports System.Runtime.Remoting


Public Class RemoteTransfer
    Inherits MarshalByRefObject

    Public Delegate Sub delSlaveCall()
    Public SlaveCall As delSlaveCall

    Public Sub New()
    End Sub


    Public Sub CallSlave()
        Try
            SlaveCall.Invoke()
        Catch ex As Exception

        End Try

    End Sub

    Public Overrides Function InitializeLifetimeService() As Object
        Return Nothing

    End Function

End Class

Public Class Request

    Private RT As RemoteTransfer = Nothing

    Public Sub Process()

        Dim sURL As String = String.Format("tcp://{0}:{1}/DALCTransferService", "DataPrint2", "1500")

        Try
            RT = CType(Activator.GetObject(GetType(RemoteTransfer), sURL), RemoteTransfer)

            RT.CallSlave()
            'RT.SlaveCall.Invoke()

        Catch ex As Exception
            MsgBox(ex.ToString())

        End Try
    End Sub
End Class


'Here is an example of the Master:
Imports System.Runtime.Remoting
Imports Remote

Public Class Master

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim req As New Remote.Request()

        req.Process()

    End Sub
End Class


Hope that helps!   I'll let you know if I think of anything else.

0
 

Author Comment

by:brownpeterg
ID: 18861892
VBRocks,
    Thanks.  Honestly, I asked the question improperly.  I used the word 'remote' in my question errantly.  The dll isn't remote, its just not included (or "includable") in my solution.  So when I quickly the question in a rush this morning before work, I goofed and described a "remote dll."
    But I'll accept your answer anyway, you put a good bit of work into it (I haven't checked it and don't know enough about remoting anyway!).
pgb
0
 
LVL 27

Expert Comment

by:VBRocks
ID: 18862223
Thanks!  
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

739 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