[英]How to use dll for VB.net to C#
I have dll
and sample code using VB.net.
我有使用VB.net.
dll
和示例代码VB.net.
However I need to use functions in E5KDAQ.vb
using the E5KDAQ.dll
file on C#. 但是,我需要使用C#上的E5KDAQ.dll
文件在E5KDAQ.vb
使用函数。 May I know how to achieve that? 我可以知道如何实现这一目标吗?
Can anyone give any good examples on how to use it? 任何人都可以提供有关如何使用它的任何好例子吗?
E5KDAQ.vb 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: 我试图在我的C#项目中直接引用dll文件,但不能出现以下错误:
I also tried to compile the VB project as class library with the following: 我还尝试使用以下代码将VB项目编译为类库:
Class1.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
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? 那么我如何导入E5KDAQ.dll以便我可以成功编译它作为C#的类库使用?
If anyone willing to help,i can send you the example vb project file since it is accessible to the public. 如果有人愿意提供帮助,我可以向您发送示例vb项目文件,因为它可供公众访问。
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 要在您的类库文件中包含dll动态链接库,您必须使用Shared Functione.g
Public Shared Function COMM_Close(ByVal ComPortNumber As Short) As Short End Function 公共共享功能COMM_Close(ByVal ComPortNumber为短)作为短端功能
https://docs.microsoft.com/en-us/dotnet/visual-basic/misc/bc31529 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 将整个vb.net转换为csharp visual studio扩展
How Convert VB Project to C# Project 如何将VB项目转换为C#项目
Error when running the class library on c#. 在c#上运行类库时出错。 In the c# project i can see all the function,however when i build the project ,i got the following error: 在c#项目中我可以看到所有的功能,但是当我构建项目时,我收到以下错误:
Form1.cs 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: 还将dll放在创建可执行文件的debug文件夹中,仍然会出现相同的错误:
2-and-a-bit options: 2位和1位选项:
var i = E5KDAQ.E5K_GetRunTimeOS();
(最简单也可能是首选)将现有的VB包装器编译为dll,并简单地从C#项目中引用该dll - 并使用var i = E5KDAQ.E5K_GetRunTimeOS();
etc from your C# 你的C#等等
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.