如何将dll用于VB.net到C#

时间:2018-08-30 09:32:09

标签: c# vb.net-2010

我有dll和使用VB.net.的示例代码 但是,我需要使用C#上的E5KDAQ.vb文件在E5KDAQ.dll中使用函数。我可以知道如何实现吗?

任何人都可以提供有关如何使用它的良好示例吗?

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

我试图直接在我的C#项目中引用dll文件,但是无法出现以下错误:

enter image description here

我也试图将VB项目编译为类库 具有以下内容:

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

我遇到以下错误:

enter image description here

那么我如何导入E5KDAQ.dll,以便我可以成功地将其编译为供C#使用的类库?

如果有人愿意提供帮助,我可以向您发送示例vb项目文件,因为公众可以访问它。

任何帮助将不胜感激。

2 个答案:

答案 0 :(得分:2)

已解决。 要将dll动态链接库包含在类库文件中,必须使用 Shared Functione.g

公共共享函数COMM_Close(ByVal ComPortNumber短)短 结束功能

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

也可以找到另一种解决方案尝试此方法。 将整个vb.net转换为csharp visual studio扩展

How Convert VB Project to C# Project

在c#上运行类库时出错。 在C#项目中,我可以看到所有功能,但是在构建项目时,出现以下错误:

enter image description here

enter image description here

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());
            }




        }

    }
}

还将dll放入创建可执行文件的调试文件夹中,仍然出现相同错误:

enter image description here 知道这是什么吗?

答案 1 :(得分:1)

2位选项:

  1. (最简单,并且可能是首选),将您现有的VB包装器编译为dll,并只需从C#项目中引用该dll-只需在C#中使用Select a,b from tab where a='pmtr[1,1]':'A':'pmtr[2,1]' 等即可
  2. 将VB(手动或通过工具)转换为C#;如果VB所做的只是宣传P / Invoke目标,那么可能并不是很棘手
  3. (“有点”)将您现有的VB包装器编译为dll,然后使用“ reflector”之类的工具将编译后的IL重写为C#