旧版 VB6 应用程序在 ActiveX 创建对象期间抛出类型不匹配错误

发布于 2024-10-18 20:06:13 字数 1639 浏览 6 评论 0原文

我的任务是对旧版 VB6 Winform 应用程序进行更改。我发现这个应用程序不必要地分成多个 DLL(其中一些 DLL 只是几个类)。因此,我正在努力将一些 DLL 合并到主程序中,但我遇到了一个需要帮助的问题。

其中一个 dll 包含一个名为 CTest(Test.cls) 的类。主程序在以下代码行中使用了它。 strProgId 是命名另一个 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

以下是 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

如果 CTest 仍然是 DLL 的一部分,并且我在主程序中引用了它,则它可以顺利通过 CreateTestObject 行。如果我将类引入主程序,它会引发类型不匹配错误。

如有任何帮助,我们将不胜感激,预先感谢您。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

三五鸿雁 2024-10-25 20:06:13

CreateObject 仅适用于公开可见的情况COM 类。因此,因为您已将 CTest 引入主程序,所以 CreateObject 将不再工作,并且会引发错误,就像您所描述的那样。

还是

  • 通过 Set obj = New CTest 创建对象
  • 将类保留在单独的 DLL 中?您确定将其放在单独的 DLL 中没有其他副作用吗?没有其他应用程序使用它吗?

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?
淡忘如思 2024-10-25 20:06:13

我花了一天半的时间才解决了这个问题。就我而言,我调用了 dll 两次。第一次成功,第二次就出现上面的错误。我打开了几个项目,每个项目都有自己的兼容性设置。由于某些无法解释的原因,对通用 dll 的第二次引用已关闭兼容性。通过在版本兼容性中设置正确的路径并将其设置为二进制兼容性,问题得到解决。

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.

公布 2024-10-25 20:06:13

如果您直接将 CTest 引入主程序,那么您不需要 CreateObject 调用 - 只需以正常方式实例化它,现在它是您程序的一部分,并且应该可以正常工作。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文