Solved

Implementing remote dll callbacks as events in VB.Net?

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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Suggested Solutions

If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

770 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