Compile C#.Net Dll for Vb6

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):

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

  2. Register ActiveX dll

2.1 Run CMD as Administrator

2.2 Run - regsvr32.exe C:\path_to_old.dll


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



Leave a reply



Submit