1
votes

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!

2
See that space the IDE inserted after "ListenForReports" above? It is saying to you: "There aren't supposed to be parentheses here, do you know what you're doing?"Bob77
The correct name should resemble LocationApiLib.CivicAddressReportFactoryHans Passant

2 Answers

1
votes

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.

0
votes

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)