0
votes

I've been tasked with making a change to a legacy VB6 Winform app. What I found is that this app was unnecessarily split up into multiple DLLs (some of the DLL were simply a couple of classes). So, I'm working on consolidating some of the DLLs into the main program but I've run into a problem that I could use some help on.

One of the dlls contained a class called CTest(Test.cls). The main program used it in the following lines of code. strProgId is a string naming another DLL.

Dim objTest As CTest 

Set objTest = CreateTestObject(strProgId)

Public Function CreateTestObject(strProgId As String) As Object
10        On Error GoTo ErrorHandler
20        Set CreateTestObject = CreateObject(strProgId)
30        Exit Function
ErrorHandler:
40        UpdateErrorInfo "CreateTestObject", "Globals", strProgId
50        HandleError
End Function

Here are the contents of CTest

Option Explicit


Private m_strName As String
Private m_strDescription As String
Private m_cnnADO As ADODB.Connection

Public Property Get Name() As String
10        Name = m_strName
End Property

Public Property Let Name(strNewName As String)
10        m_strName = strNewName
End Property

Public Property Get Connection() As ADODB.Connection
10        Set Connection = m_cnnADO
End Property

Public Property Set Connection(cnnADO As ADODB.Connection)
10        Set m_cnnADO = cnnADO
End Property

Public Property Get Description() As String
10        Description = m_strDescription
End Property

Public Property Let Description(strNewDescription As String)
10        m_strDescription = strNewDescription
End Property

Public Function Run(ByVal strSTMType As String, _
                    instInstruments As CInstruments, objResults As CTestResults) As Boolean

End Function

If CTest is still part of a DLL and I have a reference to it in the Main Program, it gets through the CreateTestObject line without an error. If I bring in the class into the main program it throws a type mismatch error.

Any help is appreciated, thank you in advance.

3

3 Answers

2
votes

CreateObject will only work with publicly visible COM classes. Therefore, because you've brought CTest into your main program, CreateObject will no longer work and will raise errors just like you describe.

Either

  • Create the object via Set obj = New CTest
  • Or just leave the class in a separate DLL? Are you sure there's no other side effects of it being in a separate DLL? No other app using it?
2
votes

I just solved this one after a day and a half. In my case I invoke the dll twice. The first time it worked and the second time it threw the error above. I have several projects open and each has its' own compatibility setting. For some unexplained reason the second reference to the common dll had compatibility set off. By setting the correct path in the version compatability and setting it to binary compatibility the problem cleared up.

1
votes

If you're bringing CTest into your main program directly, then you don't need the CreateObject call - just instantiate it the normal way, now that it's part of your program, and it should work fine.