Solved

Implementing remote dll callbacks as events in VB.Net?

Posted on 2007-04-05
5
620 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
  • 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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

758 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

24 Experts available now in Live!

Get 1:1 Help Now