Reputation: 333
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.
Upvotes: 1
Views: 2855
Reputation: 333
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://learn.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:
Upvotes: 2
Reputation: 1062492
2-and-a-bit options:
var i = E5KDAQ.E5K_GetRunTimeOS();
etc from your C#Upvotes: 1