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. COM
  4. Calling .NET component from VB. [modified]

Calling .NET component from VB. [modified]

Scheduled Pinned Locked Moved COM
csharpcomhelpdebuggingtutorial
2 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.

    modified on Monday, March 24, 2008 3:49 AM

    V 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.

      modified on Monday, March 24, 2008 3:49 AM

      V Offline
      V Offline
      Vi2
      wrote on last edited by
      #2

      Statement "Private Declare Function method Lib..." is unwanted and should be removed. I.1. Add typelib into VB project by menu "Project" -> "References...". Search your DLL or TLB there (something like "interop") and select it. I.2. Create new instance with New and Set operator Dim obj As sample Set obj = New sample II. Create new instance with CreateObject and Set operator Dim obj As Object Set obj = CreateObject("interop.sample")

      With best wishes, Vita

      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