VB 6 中的 COM 对象:未定义用户定义类型

发布于 2024-11-03 16:16:24 字数 853 浏览 1 评论 0原文

在 VB6 项目中,我添加了对 COM dll 的引用: c:\windows\system32\locationapi.dll

以下代码运行良好,并且可以正确访问对象的方法/属性:

Public civicfactory
Set civicfactory = CreateObject("LocationDisp.CivicAddressReportFactory")

但是,它的事件不起作用 - I无法抓住他们。我想我需要像这样声明对象:

Public WithEvents civicfactory As LocationDisp.CivicAddressReportFactory

但这给了我错误“用户定义的类型未定义”。

谁能告诉我为什么第一个代码运行得很好,为什么这么说?

由于我的目的是让事件处理程序正常工作,您能否指出我的代码中的任何错误或缺失的内容:

Public civicfactory
Private Sub civicfactory_NewCivicAddressReport(report)
    MsgBox "New civic address report arrived"
    DisplayCivic (report)
End Sub
Set civicfactory = CreateObject("LocationDisp.CivicAddressReportFactory")
civicfactory.ListenForReports (1000)

类似的代码可以在 VBScript 中工作,但我无法使其在 VB 6 中工作 - 事件处理程序永远不会被调用。

提前致谢!

In a VB6 project I have added reference to the COM dll: c:\windows\system32\locationapi.dll

The following code works perfectly well and the object's methods/properties can be accessed correctly:

Public civicfactory
Set civicfactory = CreateObject("LocationDisp.CivicAddressReportFactory")

However, its events don't work - I cannot catch them. I guess I need to declare the object like this:

Public WithEvents civicfactory As LocationDisp.CivicAddressReportFactory

But this gives me the error "User-defined type not defined" at this line.

Can anyone tell me why it says so while the first code works perfectly well?

And as my purpose is to get event handlers working, can you point out any error or anything missing in my code:

Public civicfactory
Private Sub civicfactory_NewCivicAddressReport(report)
    MsgBox "New civic address report arrived"
    DisplayCivic (report)
End Sub
Set civicfactory = CreateObject("LocationDisp.CivicAddressReportFactory")
civicfactory.ListenForReports (1000)

Similar code works in VBScript, but I cannot get it working in VB 6 - the event handler never gets called.

Thanks in advance!

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

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

发布评论

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

评论(2

┊风居住的梦幻卍 2024-11-10 16:16:25

此行将起作用(在“项目”菜单上,选择“引用”,然后添加对您的类型库的引用)

Public WithEvents civicfactory As LocationDisp.CivicAddressReportFactory 

如果您添加对 COM 类型库的引用,则

This line

Public WithEvents civicfactory As LocationDisp.CivicAddressReportFactory 

will work if you add a reference to the COM type library (On the Project menu, select References, and add a reference to your typelib)

雪若未夕 2024-11-10 16:16:24

我不在 Windows 7 上进行开发,因此无法测试它,但我认为您需要实现 ILocationEvents 接口,然后创建此类的实例并将其传递给 CivicAddressReportFactory 对象的 ILocation 接口的 RegisterForReport 方法。

换句话说,他们根本不费心去实现事件,而是获得 COM 回调,又名“脚本事件”。

这是可行的,并且需要使用许多新的 COM 库,因为它们将目光放在 VB6 和其他 ActiveX 主机上。例如,您必须做类似这样的愚蠢事情才能在 XP 及更高版本中使用 UPnP 库,使用 MSXML Helper 对象进行异步请求等。

确实应该受到谴责。但您对 Microsoft.Net 有何期望?

一旦你解决了这个问题,就可以将其视为竞争优势。这就是我所做的。

I don't develop on Windows 7 so I can't test it, but I think you need to implement the ILocationEvents interface and then create an instance of this class and pass it to the RegisterForReport method of the ILocation interface of your CivicAddressReportFactory object.

In other words they did not bother to implement events at all, but instead you get COM callbacks, a.k.a. "script events."

This is doable, and is required to use a lot of new COM libraries since they put the squint on VB6 and other ActiveX hosts. For example you have to do goofy things like this to use the UPnP libraries in XP and later, for async requests using MSXML Helper objects, etc.

Reprehensible, true enough. But what do you expect from Microsoft.Net?

Think of it as competitive advantage once you get it worked out. That's what I do.

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