Question Exposing .NET COM component to VB6

DannyvdKraan

New member
Joined
May 12, 2009
Messages
2
Programming Experience
1-3
Hello my saviors (I hope),

I have a little problem getting my .NET component to work in VB6. I have created in Visual Studio 2008 with VB.NET using the COM Class template a assembly which I want to call in VB6. This is the code:

<ComClass(TestTemplateClass.ClassId, TestTemplateClass.InterfaceId, TestTemplateClass.EventsId)> _
Public Class TestTemplateClass

#Region "COM GUIDs"
' These GUIDs provide the COM identity for this class
' and its COM interfaces. If you change them, existing
' clients will no longer be able to access the class.
Public Const ClassId As String = "6a22b94d-74e7-4bb7-ad90-2398faa72ef5"
Public Const InterfaceId As String = "c78aed76-d3d3-4af7-bd5f-d7e8775669d1"
Public Const EventsId As String = "f2fd0f05-acbe-4e4d-a5be-e19a0d259bcf"
#End Region

' A creatable COM class must have a Public Sub New()
' with no parameters, otherwise, the class will not be
' registered in the COM registry and cannot be created
' via CreateObject.
Public Sub New()
MyBase.New()
End Sub

Public Function SayHello() As String
Return "Hello!"
End Function
End Class

As you can see it's pretty straight forward. It does all the appropiate decorating/registering and whatnot for yah using the template. It only has one method called 'SayHello' which I'm calling in VB6. I clicked 'build' and presto... it created the .dll/.tlb/.pdb

Then in VB6 I referenced the .tlb (can't reference the .dll directly) and created a form with one button. In the click event I have the following code:

Private Sub cmdOK_Click()
Dim o As TestTemplateClass
Set o = New TestTemplateClass
MsgBox o.SayHello
End Sub

This works! No problem. So what's the problem you might ask? Well the executable doesn't work. When I simply make the executable in the studio and dubble click it I get the run time error: "File or assembly name 'TestTemplate', or one of it's dependencies, was not found." I've tried to:
- move the dll + tlb to the same directory the .exe is in;
- move these files to the same directory vb.exe is in;
- move these files to c:\windows\system32

This all didn't help. Then I created a package using the Package & Deployment Wizard. It complains indeed about not knowing the dependancies of 'TestTemplate' assembly, but it goes ahead and create my package anyways. I install my tiny test VB6 application by running setup and it apparantly copies TestTemplate.tlb to my c:\windows\system32 directory. But when I run this installed application I get: "Class does not support automation or does not support expected interface".

To cut this long story short. Can someone please explain to me why it works in my IDE but not in an executable? It's all done on the same development machine (mine) with Win XP and Framework 3.5 installed. I'm at my wits end after 3 days digging, so I really need some help.

Thnx in advance for the replies.
 
Exposing .NET COM to VB6?

Ok, imagine the simplest solution in Visual Studio 2008 with framework 3.5 (OS = Win XP) where I have added a class library. Then I've added a COM Class. This COM Class is so simple, it only exposes 1 function:

Public Function SayHello() As String
Return " Hello."
End Function

I've build this class library, which produced the .dll and the .tlb. I see the .tlb in VB6 app's references box (which is on the same comp), so I reference it. I call SayHello, run it, and... 'automation error'.

I found an article somewhere how to make a SetUp Project and then install the .dll. After I seperatly installed the .dll using this setup project the COM component works like it should in my VB6 app.

Ok, now for my question... WHY?! Can someone please explain to me what the setup project does that I apparantly didn't.

P.S.: Just to make it clear, it worked fine with VB2005 and Framework 2.0 ... But now I can't make this simplest example project to work.
 
Back
Top