1

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:

enter image description here

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:

enter image description here

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.

2
  • The point of programming in any .NET-compatible language is that no matter which you can reference the compiled assembly from every other assembly also produced by .NET. Thus you can reference an assembly which was written using C# in one written on VB.NET and vice versa. Just add a reference to your project/assembly. Commented Aug 30, 2018 at 9:35
  • Re the new error: it needs to be non-instance, then; in C# that is called static - in VB it is called Shared. So: mark those methods as Shared Commented Aug 31, 2018 at 8:30

2 Answers 2

2

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:

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




        }

    }
}

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

enter image description here Any idea what is this?

3
  • Another issue occurs.When i tried applying it in c#,i got an error it cannot load DLL E5KDAQ.dll.I thought this dll is included in the vb itself when compiled into a class library?Added on my Answer
    – Thomas Koh
    Commented Aug 31, 2018 at 7:50
  • "I thought this dll is included in the vb itself" - nope, these are external imports - you still need the actual dll to be shipped. It needs to end up in the execution location, usually bin/debug or bin/release for testing. Usually you'd include the dll in the project and tell the build system to copy it to the output location. Commented Aug 31, 2018 at 8:33
  • I have already included the dll in the output debug directory but still getting the same error
    – Thomas Koh
    Commented Aug 31, 2018 at 16:06
1

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#
3
  • May you explain what do you mean by VB wrapper?Do you mean the E5KDAQ.vb file?Do you mean i create a VB project containing the E5KDAQ.vb and E5KDAQ.dll and compile it.Sorry i am new to vb.net and c#
    – Thomas Koh
    Commented Aug 30, 2018 at 9:39
  • @ThomasKoh the basic deployment unit for .NET assemblies is the dll; when you compile a VB or C# project, the output is usually a dll (unless you've told it to create an executable). That dll can be referenced by other projects. Note: if they're in the same Visual Studio solution, even this is overkill - you can add a "project reference" from your C# project to your VB project, and it'll manage the details (dlls, nupkgs, etc) automatically. You cannot uss a VB file inside a C# project, but you can have a VB project and a C# project in the same solution, and communicate between them. Commented Aug 30, 2018 at 10:02
  • I tried to add a reference to the dll file straight in my C# project but got the following error.Have updated it on my question.
    – Thomas Koh
    Commented Aug 31, 2018 at 1:12

Not the answer you're looking for? Browse other questions tagged or ask your own question.