Click here to Skip to main content
15,881,172 members
Please Sign up or sign in to vote.
2.00/5 (2 votes)
See more:
I have written a class library using Visual C++ 2005 (CLR, old syntax). It is working fine and can be used from all .NET IDE's, including VB, C# and C++, exposing classes with methods and properties, enumerations...

Now I want to use it from VB6 and I understand that I need to somehow make it COM compatible.

I have read different things, some about COM Interop project properties, others mentioning the ComVisibleAttribute, others Type Libraries...

So far my different attempts have lamentably failed (no suitable options in the IDE, no TLB file being generated, the ComVisibleAttribute syntax raises an "Attribute Not Found" error...)

I am lost in the jungle and I need some guidance here. Who can help me ?
Posted
Updated 8-Mar-13 3:08am
v3
Comments
CHill60 25-Feb-13 5:37am    
Have you seen this article http://support.microsoft.com/kb/817248
Chris Reynolds (UK) 25-Feb-13 5:39am    
That's the article I used when I did this recently and it did the trick for me.
YvesDaoust 25-Feb-13 5:46am    
Of course. None of it seems applicable to my case. Specifically: "5.Click Configuration Properties, and then click the Build node": there is no Build node in the configuration properties of VC2005; I coundn't find any equivalent (same about "Compile"); "Use the ComVisibleAttribute attribute": the syntax [CombisibleAttribute(true)] genrerates a compilation error; "The /tlb: switch generates and registers a type library": no, I don't see any TLB file generated.

This article makes me believe that what I want to achieve is possible and at the same time sticks me deeply.
CHill60 25-Feb-13 6:01am    
In that case have you seen this tool http://msdn.microsoft.com/en-us/library/h627s4zy.aspx
YvesDaoust 25-Feb-13 6:05am    
Just seen it and tried it. Things just a little more obscure now...

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):
C++
[assembly:ClassInterfaceAttribute(ClassInterfaceType::AutoDual)]; // This will let VB6 pre-load the interface definitions
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:
VB
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.
 
Share this answer
 
v10
You can find this article useful to solve your problem although it if of C++ instead of vb6

Calling Managed .NET C# COM Objects from Unmanaged C++ Code[^]
 
Share this answer
 
Comments
YvesDaoust 4-Mar-13 7:13am    
Thanks for the suggestion.

I have now completed a solution that does not require programming (well, a single line of code), and... works, so I am adopting it :)
MatsGF 14-Jan-14 6:21am    
Can you describe that solution that didn't involve programming?
YvesDaoust 14-Jan-14 6:31am    
This is Solution 1. All I had to change in my code was inserting the line
[assembly:ClassInterfaceAttribute(ClassInterfaceType::AutoDual)]; // This will let VB6 pre-load the interface définitions

But of course, all the class wrapping code needs to be there as well.
MatsGF 14-Jan-14 8:37am    
Thanks!
I was hoping to find a solution that didnt include COM-objects since i wanted to get rid of the registration.
YvesDaoust 14-Jan-14 9:00am    
This seems to be doable: http://msdn.microsoft.com/en-us/library/fh1h056h.aspx

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900