locked
How to use / Call a Function in a DLL file ? RRS feed

  • Question

  •  Dear Friends,

    I want to learn creating DLL file and using it in my application; so I opened "ClassLibrary" and made a Public Function and built to get a DLL file. Then, I tried to use this dll file in another simple application but I couldn't get that - may be I didn't know how to call the function. 

    Given below is the Function I used to make the DLL file.
    Named this file as "SImpleMultiplyDLL".

    Public Class Class1
        Public Function SimpleMultiply(ByVal Number1 As Double, ByVal Number2 As Double) As Double
            Dim Result As Double
            Result = (Number1 * Number2)
            SimpleMultiply = Result
        End Function
    End Class

    I was able to give "Reference" to this SimpleMultiplyDLL and is visible in the "Reference" page. The picture is given here for you to see.


    For trying this DLL File, I made an application with 3 Textboxes & a Button; the code for that is as follows - as I couldn't call the function I am not giving that here...

    Public Class Form1
    
        Dim number1, number2, result As Double
    
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    
            number1 = CDbl(TextBox1.Text)
            number2 = CDbl(TextBox2.Text)
    
    
        End Sub
    End Class

    I tried writing " Imports SimpleMultiplyDLL " above "Public Class Form1" also, but I couldn't get the Function available.

    Can you please teach me how to call the Function from the Dll file.

    Thanks

    Kind Regards

    VKSBK


    A Real Novice Programmer !


    • Edited by V.K.S.B.K Monday, September 9, 2019 8:08 AM
    Monday, May 16, 2016 11:54 AM

Answers

  • Public Class Form1
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            TextBox3.Text = CStr((New Class1).SimpleMultiply(CDbl(TextBox1.Text), CDbl(TextBox2.Text)))
            'This is the more experienced way, mostly this is done as
            Dim whatever = New Class1
            TextBox3.Text = CStr(whatever.SimpleMultiply(CDbl(TextBox1.Text), CDbl(TextBox2.Text)))
        End Sub
    End Class
    Public Class Class1
        Public Function SimpleMultiply(ByVal Number1 As Double, ByVal Number2 As Double) As Double
            Return Number1 * Number2
        End Function
    End Class


    Success
    Cor

    • Marked as answer by V.K.S.B.K Monday, May 23, 2016 8:15 AM
    Monday, May 16, 2016 12:03 PM

All replies

  • Public Class Form1
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            TextBox3.Text = CStr((New Class1).SimpleMultiply(CDbl(TextBox1.Text), CDbl(TextBox2.Text)))
            'This is the more experienced way, mostly this is done as
            Dim whatever = New Class1
            TextBox3.Text = CStr(whatever.SimpleMultiply(CDbl(TextBox1.Text), CDbl(TextBox2.Text)))
        End Sub
    End Class
    Public Class Class1
        Public Function SimpleMultiply(ByVal Number1 As Double, ByVal Number2 As Double) As Double
            Return Number1 * Number2
        End Function
    End Class


    Success
    Cor

    • Marked as answer by V.K.S.B.K Monday, May 23, 2016 8:15 AM
    Monday, May 16, 2016 12:03 PM
  • Hi Cor,

    Thanks for the reply.

    After posting the question, I figured out what I was missing. I didn't change "Class1" to another name in "Public Class Class1"; and I was puzzled when this "Class1" appeared when I tried to call the function......:-(

    I have been using a dll file for my application ( "Swiss Ephemeris" for an Astrology Application); this dll file is written in "C/C++; and for VB.Net programmers the functions are given - one eg. is given below....

    In that, I don't see the format we used here in this my "Simple Multiply".

    Public Declare Function swe_julday Lib "swedll32.dll" _
            Alias "_swe_julday@24" ( _
              ByVal Year As Long, _
              ByVal Month As Long, _
              ByVal Day As Long, _
              ByVal hour As Double, _
              ByVal gregflg As Long _
            ) As Double

    Is it possible to use my sample function in the dll file, like the way the Function "swe_julday" is used in the example given above.  I couldn't understand why "Alias " swe_julday@24" and " Lib "swell32.dll" are used.  The name of the dll file is "swedll32.dll".

    Please help.

    Thanks

    Kind Regards

    VKSBK


    A Real Novice Programmer !

    Monday, May 16, 2016 12:41 PM
  • VKSBK, 

    Don't confuse that a DLL is always a .Net Class library. It is not. Often it is a Com object which you declare but that is completely handled different from a Class library.

    Probably this link will help you.

    https://msdn.microsoft.com/en-us/library/aa719104(v=vs.71).aspx


    Success
    Cor

    • Proposed as answer by Albert_Zhang Sunday, May 22, 2016 4:02 PM
    Monday, May 16, 2016 12:50 PM
  • Hi Cor Ligthert,

    Thanks. I'll read the article you mentioned after finishing this post.... It looks like a bit difficult to understand :-)

    So, can I say that  "swedll32.dll" is a Com and that is why those Functions are given in a different format for using in VB.Net ?

    Suppose, I make few functions and make a DLL file ( using VB.net) for that and let others to use those function. Can it be used in a C++ or C # ? 

    If it can be used in C++ / C #, should it be modified some way?  Just curious to know.

    For an eg., this "swedll.32.dll" is used in C# and other languages; but I don't see the list of functions given in C# like given for VB.Net.  

    If I want to use C# to create an application using 
    "swedll32.dll", how should the sample function I gave above should be changed / used?

    The sample function is given here also for you.

    Public Declare Function swe_julday Lib "swedll32.dll" _
            Alias "_swe_julday@24" ( _
              ByVal Year As Long, _
              ByVal Month As Long, _
              ByVal Day As Long, _
              ByVal hour As Double, _
              ByVal gregflg As Long _
            ) As Double

    Please help.

    Thanks

    Kind Regards

    VKSBK


    A Real Novice Programmer !


    • Edited by V.K.S.B.K Monday, May 16, 2016 1:20 PM
    Monday, May 16, 2016 1:18 PM
  • If you have the code in C# than you simply can use the converter from Dave to set it to VB. 

    There are beside the used words almost no differences between C# and VB.

    http://www.tangiblesoftwaresolutions.com/Product_Details/Instant_VB.html


    Success
    Cor

    Monday, May 16, 2016 1:38 PM
  • In that, I don't see the format we used here in this my "Simple Multiply".

    Public Declare Function swe_julday Lib "swedll32.dll" _
            Alias "_swe_julday@24" ( _
              ByVal Year As Long, _
              ByVal Month As Long, _
              ByVal Day As Long, _
              ByVal hour As Double, _
              ByVal gregflg As Long _
            ) As Double

    Is it possible to use my sample function in the dll file, like the way the Function "swe_julday" is used in the example given above.  I couldn't understand why "Alias " swe_julday@24" and " Lib "swell32.dll" are used.  The name of the dll file is "swedll32.dll".

    Please help.

    Thanks

    Kind Regards

    VKSBK


    A Real Novice Programmer !

    The answer is no. This is an unmanaged code calling convention. You would need to use C++ or another language to export the functions from the DLL in order to use this calling convention.

    Paul ~~~~ Microsoft MVP (Visual Basic)

    • Proposed as answer by Albert_Zhang Sunday, May 22, 2016 4:02 PM
    Tuesday, May 17, 2016 12:27 PM