Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. Calling .NET component from VB6.

Calling .NET component from VB6.

Scheduled Pinned Locked Moved Visual Basic
csharpcomhelpdebugging
4 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    SPanicker
    wrote on last edited by
    #1

    I am trying to work out a simple COM/.NET interop code. In my project in VB, I have created a simple windows application to add two numbers. The functionality of adding is done through the help of a .NET DLL written in C#. I am quite confused by trying various ideas sugggested by people. Can anyone clarify this simply in a step by step fashion? This my VB code: Private Declare Function method Lib "C:\Documents and Settings\CSfiles\interop\interop\bin\Debug\interop.dll" Alias "interopdll" (ByVal X As Integer, ByVal X As Integer) Private Sub Command1_Click() Dim a As Integer Dim obj As Object obj = New sample ' I get a compile-time error for undefined user type here. a = obj.method(CInt(Text1.Text), CInt(Text2.Text)) Label1.Caption = CStr(a) End Sub My C#(.NET DLL) Code: using System; using System.Runtime.InteropServices; using System.Reflection; [assembly: ComVisible(true)] namespace interop { /// <summary> /// Summary description for Class1. /// </summary> [ClassInterface(ClassInterfaceType.AutoDual)] [GuidAttribute("6CE9C732-CD90-4042-A5F0-CF71DFAC2598")] class sample { public sample() { } public int c; public void method(int a, int b) { c = a+b; //return c; } } } In this I have registerd the DLL with /codebase switch while using regasm.exe too.

    Regards, Lenus.

    D 1 Reply Last reply
    0
    • S SPanicker

      I am trying to work out a simple COM/.NET interop code. In my project in VB, I have created a simple windows application to add two numbers. The functionality of adding is done through the help of a .NET DLL written in C#. I am quite confused by trying various ideas sugggested by people. Can anyone clarify this simply in a step by step fashion? This my VB code: Private Declare Function method Lib "C:\Documents and Settings\CSfiles\interop\interop\bin\Debug\interop.dll" Alias "interopdll" (ByVal X As Integer, ByVal X As Integer) Private Sub Command1_Click() Dim a As Integer Dim obj As Object obj = New sample ' I get a compile-time error for undefined user type here. a = obj.method(CInt(Text1.Text), CInt(Text2.Text)) Label1.Caption = CStr(a) End Sub My C#(.NET DLL) Code: using System; using System.Runtime.InteropServices; using System.Reflection; [assembly: ComVisible(true)] namespace interop { /// <summary> /// Summary description for Class1. /// </summary> [ClassInterface(ClassInterfaceType.AutoDual)] [GuidAttribute("6CE9C732-CD90-4042-A5F0-CF71DFAC2598")] class sample { public sample() { } public int c; public void method(int a, int b) { c = a+b; //return c; } } } In this I have registerd the DLL with /codebase switch while using regasm.exe too.

      Regards, Lenus.

      D Offline
      D Offline
      Dave Kreskowiak
      wrote on last edited by
      #2

      SPanicker* wrote:

      Private Declare Function method Lib "C:\Documents and Settings\CSfiles\interop\interop\bin\Debug\interop.dll" Alias "interopdll" (ByVal X As Integer, ByVal X As Integer) Private Sub Command1_Click()

      You cannot call a .NET dll from VB6 like this. The .NET compilers don't generate the exports needed for VB6 to call library functions like this. You'll have to partially scrap the VB6 code and rewrite it along with rewriting the C# .DLL. The .DLL has to be rewritten so that it's functionality it exposed through COM, if it's not already. Then you can add a reference to the COM-registered .DLL in your VB6 project and call the classes and methods through the COM interface.

      A guide to posting questions on CodeProject[^]
      Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
           2006, 2007

      S 1 Reply Last reply
      0
      • D Dave Kreskowiak

        SPanicker* wrote:

        Private Declare Function method Lib "C:\Documents and Settings\CSfiles\interop\interop\bin\Debug\interop.dll" Alias "interopdll" (ByVal X As Integer, ByVal X As Integer) Private Sub Command1_Click()

        You cannot call a .NET dll from VB6 like this. The .NET compilers don't generate the exports needed for VB6 to call library functions like this. You'll have to partially scrap the VB6 code and rewrite it along with rewriting the C# .DLL. The .DLL has to be rewritten so that it's functionality it exposed through COM, if it's not already. Then you can add a reference to the COM-registered .DLL in your VB6 project and call the classes and methods through the COM interface.

        A guide to posting questions on CodeProject[^]
        Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
             2006, 2007

        S Offline
        S Offline
        SPanicker
        wrote on last edited by
        #3

        Hi Dave, Thanks for the info. Can you just explain this a bit more? Tht is, can you just put the idea across through some demo? Or just provide me with some useful link where I can start up with such a basic tutorial or so?

        Regards, Lenus.

        D 1 Reply Last reply
        0
        • S SPanicker

          Hi Dave, Thanks for the info. Can you just explain this a bit more? Tht is, can you just put the idea across through some demo? Or just provide me with some useful link where I can start up with such a basic tutorial or so?

          Regards, Lenus.

          D Offline
          D Offline
          Dave Kreskowiak
          wrote on last edited by
          #4

          Exposing .NET Components to COM [^]

          A guide to posting questions on CodeProject[^]
          Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
               2006, 2007

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups