COM Interop works fairly well once you have tamed it.
1) Avoid static class members, as these are not supported by COM. Also avoid identifiers that just differ by casing.
2) Add the following attribute to the assembly (in
AssemblyInfo.cpp
):
[assembly:ClassInterfaceAttribute(ClassInterfaceType::AutoDual)];
Uses the
System::Runtime::InteropServices
namespace.
3) Create the corresponding Type Library file and register it using the
regasm
command as a post-build event (
project Properties -> Build Events -> Post-Build Events -> Command Line
):
regasm "$(TargetPath)" /tlb
Build the class library from VC++, let
MyLib.dll
. The file
MyLib.tlb
will also be built.
4) Under VB6, register the reference to the Type Library:
Project -> References...
, then browse to the
.tlb
file and check the checkbox next to the new entry in the list.
5) From then on, the Object Browser (F2) will show you the contents of the library, and autocompletion will be performed by the editor.
6) Copy
MyLib.dll
next to the
VB6.exe
executable to work from the IDE. You will also need to copy it next to the compiled application if you plan to
Make
one.
7) Instantiating a class is achieved with:
Dim MyInstance As MyLib.MyClass
Set MyInstance = New MyLib.MyClass
You're done !
You may experience malfunctions and error messages at some stages. These can be due to dlls (run-time libraires, .NET framework or
MyLib.dll
) not being found in the search path.
Datatypes unsupported by COM also raise issues.