简体   繁体   中英

How to use dll for VB.net to C#

I have dll and sample code using VB.net. However I need to use functions in E5KDAQ.vb using the E5KDAQ.dll file on C#. May I know how to achieve that?

Can anyone give any good examples on how to use it?

E5KDAQ.vb

 Imports System.Runtime.InteropServices
Imports System.Threading
Imports System.text
Imports System.IO
Imports System.Environment
Module E5KDAQ

  'Public Cm As New MODULE_IOCHANNELS
  'TCP/IP Port Declaration
  Const TCP_MODBUS_PORT = 502                    'MODBUS/TCP
  Public Const UDP_ASC_PORT = 1025               'UDP ASCII Command
  Public Const UDP_ALARM_PORT = 5168             'UDP alarm port
  Public Const UDP_STREAM_PORT = 5148            'UDP stream port
  Public Const BROADCAST_IP = "255.255.255.255"


  '########## E5KDAQ.DLL Export Functions ###############################################//

  <DllImport("E5KDAQ.dll")> _
  Public Function E5K_GetRunTimeOS() As Integer
  End Function

  '----Open/Close module functions -----

  <DllImport("E5KDAQ.dll")> _
  Public Function E5K_OpenModuleUSB(ByVal devid As Integer) As Short
  End Function

  <DllImport("E5KDAQ.dll")> _
  Public Function E5K_GetLocalIP(ByRef ip0 As Byte,<[In](),Out()>Byref ip1 As Byte,<[In](),Out()> ByRef ip2 As Byte,<[In](),Out()>ByRef ip3 As Byte) As Short
  End Function
  <DllImport("E5KDAQ.dll")> _
  Public Function E5K_DebugPrint(ByVal str As String)
  End Function

End Module

I tried to Reference the dll file directly in my C# project but cannot with the following error:

在此输入图像描述

I also tried to compile the VB project as class library with the following:

Class1.vb

Imports System.Runtime.InteropServices
Imports System.Threading
Imports System.Text
Imports System.IO
Imports System.Environment
Public Class Class1
    'Public Cm As New MODULE_IOCHANNELS
    'TCP/IP Port Declaration
    Const TCP_MODBUS_PORT = 502                    'MODBUS/TCP
    Public Const UDP_ASC_PORT = 1025               'UDP ASCII Command
    Public Const UDP_ALARM_PORT = 5168             'UDP alarm port
    Public Const UDP_STREAM_PORT = 5148            'UDP stream port
    Public Const BROADCAST_IP = "255.255.255.255"
    'Type of Event
    Public Const ALARM_EVENT_TYPE = 0
    Public Const STREAM_EVENT_TYPE = 1

    Public Const HIGH_ALARM_EVENT = 0
    Public Const LOW_ALARM_EVENT = 1

    Public Const AD_ALARM_EVENT = 1
    Public Const DI_ALARM_EVENT = 0

  <DllImport("Kernel32.Dll")>
    Public Function CloseHandle(ByRef hObject As Integer) As Long
    End Function

    <DllImport("Kernel32.Dll")>
    Public Function CreateMutex(ByVal Attr As Integer, ByVal bInitial As Integer, ByVal lpName As Integer) As Integer
    End Function

    <DllImport("Kernel32.Dll")>
    Public Function ReleaseMutex(ByVal hdnl As Integer) As Boolean
    End Function

    Public Function GetVarPtr(ByVal e As Object) As Integer
        Dim GC As GCHandle = GCHandle.Alloc(e, GCHandleType.Pinned)
        Dim GC2 As Integer = GC.AddrOfPinnedObject.ToInt32
        GC.Free()
        Return GC2
    End Function

    '########## E5KDAQ.DLL Export Functions ###############################################//

    <DllImport("E5KDAQ.dll")>
    Public Function E5K_GetRunTimeOS() As Integer
    End Function

    '----Open/Close module functions -----

    <DllImport("E5KDAQ.dll")>
    Public Function E5K_OpenModuleUSB(ByVal devid As Integer) As Short
    End Function

    <DllImport("E5KDAQ.dll")>
    Public Function E5K_OpenModuleIP(ByVal IP As String, ByVal ConnectTimeout As Integer, ByVal TxTimeout As Integer, ByVal rxTimeout As Integer) As Short
    End Function
End Class

I got the following error:

在此输入图像描述

So how do i import the E5KDAQ.dll so that i can compile it successfully as a class library for C# to use?

If anyone willing to help,i can send you the example vb project file since it is accessible to the public.

Any help will greatly be appreciated.

Resolved. To include the dll dynamic linked library in your class library file,you have to use Shared Functione.g

Public Shared Function COMM_Close(ByVal ComPortNumber As Short) As Short End Function

https://docs.microsoft.com/en-us/dotnet/visual-basic/misc/bc31529

Also find another solution trying this out too. Converting entire vb.net to csharp visual studio extensions

How Convert VB Project to C# Project

Error when running the class library on c#. In the c# project i can see all the function,however when i build the project ,i got the following error:

在此输入图像描述

在此输入图像描述

Form1.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using E5KDAQCSHARP32;

namespace TestE5KDA
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            int dival=0;
            int[] counterval = new int[23];
            // Test test1= new Test();
            Class1.E5K_ReadDIStatus(Module1.mid, ref dival);

            //read DI counter value
            Class1.E5K_ReadMultiDICounter(Module1.mid, 0, 2, counterval);
    for (int  i = 0; i<= Module1.mDIChannels -1; i++)
            {
                //Module1. mDICounterlist(i).Text = counterval(i);
                //Console.WriteLine(i);
                MessageBox.Show(counterval[i].ToString());
            }




        }

    }
}

Also put the dlls in the debug folder where the executable files are created,still getting the same error:

在此输入图像描述 Any idea what is this?

2-and-a-bit options:

  1. (simplest and probably preferred) compile your existing VB wrapper as a dll, and simply reference that dll from your C# project - and just use var i = E5KDAQ.E5K_GetRunTimeOS(); etc from your C#
  2. translate (manually or via a tool) the VB to C#; if all the VB does is advertise P/Invoke targets, this probably isn't very tricky
  3. (the "and a bit") compile your existing VB wrapper as a dll, then use a tool like "reflector" to rewrite the compiled IL as C#

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM