Solved

Implementing remote dll callbacks as events in VB.Net?

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

930 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

13 Experts available now in Live!

Get 1:1 Help Now