How can I call a vb.net dll from vb.net?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Thom Hehl

    How can I call a vb.net dll from vb.net?

    I have created the following com class in vb.net using VS 2010:

    Code:
    <ComClass(AgileApi.ClassId, AgileApi.InterfaceId, AgileApi.EventsId)> _
    Public Class AgileApi
    
    #Region "COM GUIDs"
        ' These  GUIDs provide the COM identity for this class 
        ' and its COM interfaces. If you change them, existing 
        ' clients will no longer be able to access the class.
        Public Const ClassId As String = "38a4d038-7e0b-40f7-9299-4d7b18d5d25f"
        Public Const InterfaceId As String = "aae5fd64-b9b2-497e-882c-006618d1679f"
        Public Const EventsId As String = "fb928add-9053-46a7-b124-49183b1cea18"
    #End Region
    
        ' A creatable COM class must have a Public Sub New() 
        ' with no parameters, otherwise, the class will not be 
        ' registered in the COM registry and cannot be created 
        ' via CreateObject.
        Public Sub New()
            MyBase.New()
        End Sub
    
        Public Function callAgile(ByVal protocol As String, ByVal host As String, ByVal port As Integer, ByVal serviceName As String, ByVal methodName As String, ByVal parms As String()) As String
            Dim xml As String
    
            Return xml
        End Function
    
    End Class
    Then I created a new project with a form to test it. Here's the form code:

    Code:
    Imports System.Runtime.InteropServices
    Public Class Form1
    
        <DllImportAttribute("AgileApi.dll", EntryPoint:="callAgile", SetLastError:=True, CharSet:=CharSet.Unicode, _
             ExactSpelling:=True, CallingConvention:=CallingConvention.StdCall)> _
        Public Shared Function callAgile(ByVal protocol As String, ByVal host As String, ByVal port As Integer, ByVal serviceName As String, ByVal methodName As String, ByVal parms As String()) As String
            'This should use the DLL
        End Function
    
        Private Sub btnGo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGo.Click
            Dim protocol As String
            protocol = "http"
            Dim host As String
            host = txtHostName.Text
            Dim port As Integer
            port = Val(txtPortNumber.Text)
    
            Dim serviceName As String
            serviceName = txtServiceName.Text
            Dim methodName As String
            methodName = txtMethodName.Text
            Dim parms(5) As String
            parms(0) = txtParm0.Text
            parms(1) = txtParm1.Text
            parms(2) = txtParm2.Text
            parms(3) = txtParm3.Text
            parms(4) = txtParm4.Text
    
            Dim last As Integer
            For ix As Integer = 4 To 0 Step -1
                If parms(ix) = "" Then
                    last = ix
                End If
            Next
    
            ReDim parms(last + 1)
    
            Dim xml As String
            xml = callAgile(protocol, host, port, serviceName, methodName, parms)
            txtXml.Text = xml
        End Sub
    End Class
    But when I call my function, I get System.EntryPoi ntNotFoundExcep tion

    I've been googling for days with no results. I can't seem to find any kind of guide to telling me how to create a vb.net dll and call it from vb.net.

    Thanks for your help.
  • Joseph Martell
    Recognized Expert New Member
    • Jan 2010
    • 198

    #2
    DLLs in .net are treated differently than in C++ and other programming languages.

    When you create a class library project in VS (for either VB or C#) you are creating a DLL. In order to call/use that DLL in another project you have to add a reference to your new program.

    Go to Project > Add Reference..., or right-click on the project that needs to use your DLL, and click "Add Reference...".

    After a short wait you should see an Add Reference dialog box.

    There are several tabs across the top but each of the components that is listed in a tab represents a DLL. If you expand the "Path" column in the .Net tab you will see the DLL location.

    If your two projects are in the same solution, you should probably go to the Projects tab and add the project that you need.

    If your dll project is NOT in the same solution as your project that needs to use the dll, then go to the Browse tab and find your compiled DLL from your DLL project. This will add the DLL as a reference to your solution.

    Comment

    • Thom Hehl
      New Member
      • Nov 2010
      • 3

      #3
      I added the reference and am getting the same error.

      If I delete the DLL, the error changes to something like dll not found, so the entry point error indicates that if found the DLL, but can't find the entry point.

      Is there something i need to do when I create the DLL to define that method as the entry point?

      Comment

      • Joseph Martell
        Recognized Expert New Member
        • Jan 2010
        • 198

        #4
        What types of projects are you dealing with? From your code it looks like your second project is a Form application. You might check your solution to make sure you have your form application set up as your start up project. Your dll has no entry point defined so the .Net runtime would throw that error if you set the dll as the start up project.

        Comment

        • Thom Hehl
          New Member
          • Nov 2010
          • 3

          #5
          I'm running the form in it's own project. The form runs fine. It's merely a tester to fill in the fields for my dll. I fill in the fields and then click the button and the call fails because it can't find the entry point of my dll.

          Thanks for the help.

          Comment

          • Thom Hehl
            New Member
            • Nov 2010
            • 3

            #6
            I found the following command. When I ran it against my DLL, I got:
            Code:
            C:\Documents and Settings\40860205\My Documents\Visual Studio 2010\Projects\Agil
            eApi\AgileApi\bin\Debug>"C:\Program Files\Microsoft Visual Studio 10.0\VC\bin\du
            mpbin" -exports AgileApi.dll
            Microsoft (R) COFF/PE Dumper Version 10.00.30319.01
            Copyright (C) Microsoft Corporation.  All rights reserved.
            
            
            Dump of file AgileApi.dll
            
            File Type: DLL
            
              Summary
            
                    2000 .reloc
                    2000 .rsrc
                    2000 .sdata
                    4000 .text
            Does this indicate there are no entry points? If so, how do I get an entry point generated into my dll?

            Comment

            • Joseph Martell
              Recognized Expert New Member
              • Jan 2010
              • 198

              #7
              I think that you did too thorough of a job of trying to user your dll in your application.

              When I remove the following lines from your form project:

              Code:
                 <DllImportAttribute("AgileApi.dll", EntryPoint:="callAgile", SetLastError:=True, CharSet:=CharSet.Unicode, _
                       ExactSpelling:=True, CallingConvention:=CallingConvention.StdCall)> _
                  Public Shared Function callAgile(ByVal protocol As String, ByVal host As String, ByVal port As Integer, ByVal serviceName As String, ByVal methodName As String, ByVal parms As String()) As String
                      'This should use the DLL
                  End Function
              and change the following lines in the form application:

              Code:
                      Dim myAgile As New DLLTest.AgileApi()
                      xml = myAgile.callAgile(protocol, host, port, serviceName, methodName, parms)
              My code works.

              I named my test DLL application "DLLTest" which is why the namespace in the Dim statement is DLLTest. Yours will probably be AgileApi.

              I am not entirely sure why you are having the problems that you are having. I would hazard to guess that it has to do with improperly "interop"in g your DLL or improperly importing your COM DLL. Either way, the COM interop classes that you are trying to use are not something that is very familiar to me so I have plumbed the depths of my knowledge at this point.

              Comment

              Working...