How to call a DLL written by C# from VB codes?

I wrote a class library (compiled as DLL) using C#, with source codes as the follows:

==============================================================
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Threading;
using System.Diagnostics;
using System.IO;
using System.Text;

namespace UTS_Convertor
{
 /// <summary>
 /// Summary description for Class1.
 /// </summary>
 public class Class1
 {
  public Class1()
  {
   //
   // TODO: Add constructor logic here
   //
  }
  public byte [] uts_convert( byte [] unicode_bytearray )
  {
   UnicodeEncoding Unicode = new UnicodeEncoding();
   UTF8Encoding UTF8 = new UTF8Encoding();

   String unicode_str = Unicode.GetString( unicode_bytearray );
   byte [] utf8_bytearray = UTF8.GetBytes( unicode_str );
   return( utf8_bytearray );
  }
 }
}
==============================================================

Can the DLL be called by a VB application If can, how to write the VB codes If can not, how to compose the DLL under Visual C#

Big Smile



Answer this question

How to call a DLL written by C# from VB codes?

  • cstackable

    I'm a little confused.

    Do you want this code in C# dll that can be referenced by a VB application

    or

    Do you want this code to be converted to and written in VB

  • W Hooper

    Hello,

    I am trying to do something similar and I am in a desperate need of help.

    I am trying to use C/C++ dlls in VBA. Those dlls are a third-party dlls and I have no control on them. I just want to be able to use them.
    as an example of what I am trying to do is:

    I need to use the function : getDirectory() which is a member of DFCLib.IDfFile

    At the top (outside my sub) I declared the function :
    Declare Function getDirectory Lib "C:\Program Files\Documentum\Shared\DcNewComp.dll" () As String

    but at :

    Set fx = New DFCLib.myIDfFile

    I get the error : User-defined type not defined.
    from the object browser I can see that IDfFile (IDfFile seems to be a class) is a member of DFCLib.



    Thanks in advance.


  • Christian Jensen

    Thanks Johan.

    And to note: I like Sub New, it's completely self explanitory. Big Smile

    Randy

  • Waitcursor

    Randy's suggestion should work - but you may have to use the namespace qualified name when you create the object:



    Public MyClass as New UTS_Convert.Class1

     


    Randy: what I suspect you find weird about the C# code is the constructor syntax in C#. In C# (and C++) you use the name of the class where you would use Sub New in Visual Basic.

    Best regards,
    Johan Stenberg



  • grikgal

    Moonriver,

    Please do not post multiple threads of the same post. I have removed the other duplicates.

  • Scott S. Waschitz

     David M. Kean wrote:
    I'm a little confused.

    Do you want this code in C# dll that can be referenced by a VB application

    or

    Do you want this code to be converted to and written in VB


    He wants to know if the code written in C# and made into a DLL can be used in a VB application. Then he wants to know how to do that. And if it cannot, then explain how to change the C# code so that VB can access it.

    My answer in part would be.

    If Yes to using in VB as is Then here's how to call. First make a referance to the DLL compiled file. In VB create an object from the class

    Public MyClass as New Class1

    Then within a method or event use the code as follows.

    Dim MyByteArr as Byte()

    MyByteArr()=MyClass.uts_convert (MyByteArr())

    But I can't say with certainty that it will work. The best way to find out is to try it. The code doesn't look right to me, but since I don't program in C# what do I know For those who do is it standard to define a class and then immediately referance it again as public Looks weird to me. Is that the equivelent of the Main() as in C++

    Randy

  • How to call a DLL written by C# from VB codes?