Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Implementing remote dll callbacks as events in VB.Net?

Posted on 2007-04-05
5
Medium Priority
?
657 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 1500 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…

610 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