Compile C#.net dll for VB6
Yes, if you expose your DLL as a COM component.
How to use method from VB6 dll in .NET?
Found solution, mb someone else will find useful, (this worked in my case):
Create .Net wrapper of VB6 ActiveX dll
1.1 Run CMD as Administrator
1.2 Move to .NET SDK folder -
cd C:\Program Files\Microsoft SDKs\Windows\v10.0A\bin\NETFX 4.6.1 Tools\
(in my case).1.3 Run -
TlbImp.exe C:\path_to_old.dll /out: new.dll
Register ActiveX dll
2.1 Run CMD as Administrator
2.2 Run - regsvr32.exe C:\path_to_old.dll
- Add Reference to converted dll ("new.dll") in c# project
I used to add "new.dll" reference before registering "old.dll", and was getting the following exception
Retrieving the COM class factory for component with CLSID {F2D4F4E5-EEA1-46FF-A83B-A270C92DAE4B} failed due to the following error: 80040154 Class not registered (Exception from HRESULT: 0x80040154 (REGDB_E_CLASSNOTREG))
Cleaning solution, removing reference and following steps 2,3 - helped
You may also find useful this articles
C-Sharp-and-activex-dll
Error adding reference to dll: Solution tlbimp.exe
help: Call C# winforms dll from VB6 project?
You have to make your class COM-Visible. Here's how I would change your code:
namespace TestDll
{
[Guid("FB8AB9B9-6986-4130-BD74-4439776D1A3D")]
[InterfaceType(ComInterfaceType.InterfaceIsIDispatch)]
[ComVisible(true)]
public interface IClass1
{
[DispId(50)]
void DisplayMessage();
}
[Guid("74201338-6927-421d-A095-3BE4FD1EF0B4")]
[ClassInterface(ClassInterfaceType.None)]
[ComVisible(true)]
[ProgId("TestDll.Class1")]
public class Class1:IClass1
{
void IClass1.DisplayMessage()
{
MessageBox.Show ("Displyaing message");
}
}
}
Note the [DispId(50)]
. You want to specify the dispatch ID for your COM-visible methods, properties, and events. If you don't, the compiler will do it for you and you may end up breaking compatibility every time you compile. The number doesn't matter so much as it doesn't change between compiles.
You might want to check out Building COM Objects in C#. It's a pretty good getting started tutorial.
Some highlights:
Exposing the VC# objects to the COM
world requires the following …* The class must be public
* Properties, methods, and events must be public.
* Properties and methods must be declared on the class interface.
* Events must be declared in the event interface.
Every Interface needs a GUID property
set before the interface name. To
generate the unique Guid , use the
guidgen.exe utility and select the
Registry Format.
Consuming .NET dll in VB6 application
I would thought to give you more detail, in order .NET Assembly expose to COM
you need to generate the tbl - type library
using RegAsm /tlb: MyLIB.tlb MyLIB.dll
There are Guidelines to expose .NET Types to COM and make sure you cope with that.
such as declare ComVisibleAttribute , require a public default constructor to be visible to COM , such are in
you can refer that in
How to call a Visual Basic .NET or Visual Basic 2005 assembly from Visual Basic 6.0
Related Topics
Best Practice for Exception Handling in a Windows Forms Application
What's the Difference Between Utf8/Utf16 and Base64 in Terms of Encoding
Detecting Client Death in Wcf Duplex Contracts
Why Are Tolookup and Groupby Different
Dynamically Updating Tabcontrol Content at Runtime
Allow Access Permission to Write in Program Files of Windows 7
Syncing SQL Server 2008 Databases Over Http Using Wcf & Sync Framework
Curious Null-Coalescing Operator Custom Implicit Conversion Behaviour
How to Create an Audit Trail with Entity Framework 5 and MVC 4
Forms' Does Not Exist in the Namespace System.Windows
How to Render an ASP.NET MVC View in PDF Format
Error: "The Node to Be Inserted Is from a Different Document Context"
How to Delete a Row from Gridview
Multiple Where Clauses with Linq Extension Methods