问题描述
限时送ChatGPT账号..我在 Visual Studio 中创建了一个 Windows 窗体应用程序.
I have a windows form application created in Visual Studio.
我引用了一个 COM API,我正在尝试运行这个
I have referenced a COM API and I am trying to run this
L_RESULT = Visualfiles.SystemScript("HIST-TEL", sampleVisualBasicColl, "")
第二个参数需要bee一个集合,所以我创建了这个
The second parameter needs to bee a collection, so I created this
Dim sampleVisualBasicColl As New Microsoft.VisualBasic.Collection()
Dim item1, item2, item3, item4 As String
item1 = "Items"
item2 = "In"
item3 = "A"
item4 = "Collection"
sampleVisualBasicColl.Add(item1, "firstkey")
sampleVisualBasicColl.Add(item2, "secondkey")
sampleVisualBasicColl.Add(item3, "thirdkey")
sampleVisualBasicColl.Add(item4, "fourthkey")
但我得到的错误是这个
无法将Microsoft.VisualBasic.Collection"类型的对象转换为VBA.Collection"类型.
Unable to cast object of type 'Microsoft.VisualBasic.Collection' to type 'VBA.Collection'.
我发现这篇文章我认为会有所帮助 - https://support.microsoft/en-gb/kb/323737
I have found this article which I think will help - https://support.microsoft/en-gb/kb/323737
但我如何完成第 1-4 步?
But how do I complete steps 1-4?
我有 Visual Studio 社区 2015,但不知道如何创建这个 dll?
I have visual studio community 2015 and I am not sure how to create this dll?
感谢您的建议!
推荐答案
Collection coclass 是一个长期的麻烦制造者,微软为它创建了太多的实现.而且,非常不明智地,给这些实现提供了相同的 CLSID.不太清楚这是如何出错的,可能是一组程序员没有与另一组交谈.他们想出的糟糕解决方案是强制每个人都应用 [noncreatable] 属性.
The Collection coclass is a chronic trouble-maker, Microsoft created too many implementations of it. And, quite unwisely, gave those implementations the same CLSID. Not that clear how this went so badly wrong, probably one group of programmers not talking to another group. The lousy solution they came up with was to force everybody to apply the [noncreatable] attribute.
这会阻止您添加对 vba7.dll 的引用,以便您可以创建自己的 Collection 对象.这就是知识库文章告诉您使用 VB6 创建对象实例的原因.嗯,呃,18 年是很多狗的生活,今天谁已经安装了 VB6.您需要 MSDN 许可证或在 Ebay 拍卖中好运.
Which stops you from adding a reference to, say, vba7.dll so you can create your own Collection object. Which is why the KB article tells you to use VB6 to create an instance of the object. Well, ugh, 18 years is a lot of dog lives and who has VB6 installed anymore today. You need an MSDN license or good luck at an Ebay auction.
是时候以正确的方式解决这个问题,而不是那种糟糕的 KB 方式了.您所要做的就是创建 Collection 对象默认接口的具体实例.根据 COM 规则,接口的实现从不重要.
Time to get this fixed the correct way instead of that lousy KB way. All you have to do is to create a concrete instance of the Collection object's default interface. By COM rules, the implementation of the interface never matters.
向您的项目添加一个新类并粘贴此代码:
Add a new class to your project and paste this code:
Imports System.Runtime.InteropServices
Namespace VBA
<ComVisible(True), Guid("A4C46780-499F-101B-BB78-00AA00383CBB")>
Public Interface _Collection
<DispId(0)> Function Item(<[In]> ByRef Index As Object) As Object
<DispId(1)> Sub Add(<[In]> ByRef Item As Object, ByRef Optional Key As Object = Nothing,
ByRef Optional Before As Object = Nothing,
ByRef Optional After As Object = Nothing)
<DispId(2)> Function Count() As Integer
<DispId(3)> Sub Remove(<[In]> ByRef Index As Object)
<DispId(-4)> Function _NewEnum() As IEnumerator
End Interface
'' <ComVisible(True)>
<ClassInterface(ClassInterfaceType.None), Guid("A4C4671C-499F-101B-BB78-00AA00383CBB")>
Public Class Collection
Implements _Collection
Private impl As New Microsoft.VisualBasic.Collection
Public Sub Add(ByRef Item As Object, ByRef Optional Key As Object = Nothing, ByRef Optional Before As Object = Nothing, ByRef Optional After As Object = Nothing) Implements _Collection.Add
impl.Add(Item, CStr(Key), Before, After)
End Sub
Public Sub Remove(ByRef Index As Object) Implements _Collection.Remove
If TypeOf Index Is String Then impl.Remove(CStr(Index)) Else impl.Remove(CInt(Index))
End Sub
Public Function Count() As Integer Implements _Collection.Count
Return impl.Count
End Function
Public Function _NewEnum() As IEnumerator Implements _Collection._NewEnum
Return impl.GetEnumerator()
End Function
Public Function Item(ByRef Index As Object) As Object Implements _Collection.Item
Return impl(Index)
End Function
End Class
End Namespace
因此,现在不是创建一个新的 Collection 对象,而是创建一个新的 VBA.Collection 对象以保持组件正常运行.我没有更好的方法来测试它了,希望它有效.
So instead of creating a new Collection object, now create a new VBA.Collection object to keep the component happy. I don't have a good way to test it anymore, hope it works.
这篇关于使用 Visual Studio 2015 创建 VB6 dll?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
更多推荐
[db:关键词]
发布评论