Calling .NET component from VB6.
-
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.
-
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.
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 -
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 -
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.
Exposing .NET Components to COM [^]
A guide to posting questions on CodeProject[^]
Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
2006, 2007