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