none
How to send data from .dll to main program in VB.NET RRS feed

  • Question

  • Hi everybody!

    How may I send data from .dll to main program in VB.NET? I can only call functions or subs from main program to .dll files.

    Thank you!

    Monday, February 18, 2019 1:56 PM

All replies

  • High level,

    • Create a new class project.
    • Create method(s) that are public with a return type e.g. string, number, boolean list, DataTable etc.
    • Add a reference to your forms project to the new class project.
    • Add a Import statement for the class project.
    • Create an instance of the class in the class project in the form to get or send data.

    Bullet 2 is optional but usually when sending information to a class method we want to at least know it was successful in this case to return a Boolean.

    If the DLL is not your DLL and is not VB.NET more details are needed.

    Super simple example

    Public Class Class1
        Private Property FirstName() As String = ""
        Private Property LastName() As String = ""
        Public Sub SetNames(pFirstName As String, pLastName As String)
            FirstName = pFirstName
            LastName = pLastName
        End Sub
        Public ReadOnly Property FullName() As String
            Get
                Return $"{FirstName} {LastName}"
            End Get
        End Property
    End Class
    

    Form code

    Imports SomeLibrary
    
    Public Class Form1
        Private demoClass As Class1 = New Class1
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            demoClass.SetNames("Karen", "Payne")
        End Sub
    
        Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
            MessageBox.Show(demoClass.FullName)
        End Sub
    End Class
    


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Monday, February 18, 2019 2:54 PM
    Moderator
  • You can't becuase the DLL must be hosted by the main program that would be a standalone executable IMO, and the main program makes a request as a client  to a method in the DLL and gets a response back from the DLL method acting as a server for lack of better words. The DLL method can't initiate a request to a method on the program that is hosting DLL that I know about.


     
    • Edited by DA924x Monday, February 18, 2019 10:34 PM
    Monday, February 18, 2019 10:33 PM
  • Hi MVP! Thank you for your reply!

    And I have further questions that actually the .dll is registered in server on TCP channels that can be called on remote client. After client called .dll is there any way to get parameters of the .dll from the server side? For example to get the client username, etc.

    Thank you!

    Tuesday, February 19, 2019 2:09 AM
  • Hi MVP! Thank you for your reply!

    And I have further questions that actually the .dll is registered in server on TCP channels that can be called on remote client. After client called .dll is there any way to get parameters of the .dll from the server side? For example to get the client username, etc.

    Thank you!

    https://docs.microsoft.com/en-us/dotnet/framework/wcf/feature-details/how-to-create-a-duplex-contract

    <copied>

    This topic shows the basic steps to create methods that use a duplex (two-way) contract. A duplex contract allows clients and servers to communicate with each other independently so that either can initiate calls to the other.

    Use this kind of contract when your service must query the client for more information or explicitly raise events on the client.

    <end>

    In other words, the a DLL hosted by the selfhost WCF service can initiate the communications with  the client using duplex, which is a DLL itself being hosted by the Windows desktop solution that can host your DLL.

    https://www.c-sharpcorner.com/UploadFile/8a67c0/wcf-service-self-hosting-and-consuming-with-windows-applicat/

    http://tutorial.wmlcloud.com/windows_7/Visual-Basic-2010---Implementing-WCF-Services.aspx

    What I don't understand is that since you know that the client must instance an object provided by the DLL  acting as a server, why can't the client send all the data required as parameters in calling methods on objects hosted by the DLL?  Why is it that you must call back to the client from the DLL's object for anything?


    Tuesday, February 19, 2019 10:14 AM