Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Referencing .NET Assembly in VB6 won't work

Tags:

c#

com

vb6

I wrote a .net assembly using c# to perform functions that will be used by both managed and unmanaged code. I have a VB6 project that now needs to use the assembly via COM.

I created my .net assembly, made sure that ComVisible is set to true and that it is registered for COM interop via project properties.

public class MyClass

    [ComVisible(true)]
    public string GetResponse()
    {
        return "Testing Response"
    }

}

I build the assembly and copied the file into a folder. TestInterop.dll

I then run a batch file to register the assembly tool to register the object for COM.

cd C:\Windows\Microsoft.NET\Framework\v4.0.30319\
regasm "c:\Program Files\TestApp\TestInterop.dll" /tlb:TestInterop.tlb

I open a new VB6 application and reference TestInterop.dll

In VB6 I write the following code and it compiles.

Dim obj as TestInterop.MyClass
Set obj = new TestInterop.MyClass

Dim strTest as string

strTest = obj.GetRespose()

When I run the program it errors on the obj.GetResponse() line.

Run-time error' -2147024894 (80070002'):
Automation error
The system cannot find the file specified

Also, the intellesense does not work on obj. I had to type the GetResponse method. Is this normal?

Does anyone have any clue what could be wrong or what steps I missed. Thanks!

like image 228
dretzlaff17 Avatar asked Jun 08 '10 18:06

dretzlaff17


2 Answers

You'll want to put your .NET assembly in the GAC or else run RegAsm with the /codebase command line switch (it will complain, but this will at least work). No intellisense is normal, unfortunately.

like image 88
Mike Burton Avatar answered Nov 06 '22 19:11

Mike Burton


Last time I saw that I forgot to hard-code the GUIDs. So every time I recompiled VB would be unable to find my code. This is a template from VB.NET. (Don't use these GUIDs, make your own.)

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

#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 = "eaf83044-f0a7-417b-b333-e45aec398ca5"
    Public Const InterfaceId As String = "84e0fb8f-266d-40e6-9e8c-3d4eb37d3bf0"
    Public Const EventsId As String = "22ea2214-032f-4eb6-b2d4-c5dd213bab87"
#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

End Class
like image 41
Jonathan Allen Avatar answered Nov 06 '22 17:11

Jonathan Allen