PDA

View Full Version : [SOLVED:] Event Handler for external DLL events not triggering



jpswart82
06-22-2019, 10:26 AM
I'm using Office 2010, and trying to use VBA to access the methods and events of a ActiveX DLL file. The DLL is supplied as part of the SDK of a fingerprint reader from ZKTECO - the reader is connected to the PC via ethernet. My goal is to use the methods and events available in the DLL in order to have a "check in" system where somebody who is registered can come and scan their fngerprint, and then on the excel side their "ID" will link with names from a database. My problem is that I'm struggling to make the event handler get executed when I use the fingerprint reader. I've included the DLL file via Tools -> References, and can view it using the object browser (screenshot below).

24472

The following code in Module1 successfully allows me to connect to the reader and use the methods from the DLL. The class is called CZKEM.


Option Explicit


Public Sub connect()
Dim IPAddress As String
Dim Ports As Long
Dim connected As Boolean
Dim tester As CZKEM
Dim vbool As Variant
Dim i As Integer
Dim success As Boolean
Dim UserID As Long
Dim fingerIndex As Long
Dim machineID As Long
Dim eventMask As Long

Dim iclass As New zkemkeeper.CZKEM

IPAddress = "192.168.1.201"
Ports = 4370
Set tester = New CZKEM

machineID = tester.MachineNumber

UserID = 10
fingerIndex = 0
eventMask = 65535


connected = tester.Connect_NET(IPAddress, Ports)
vbool = tester.RegEvent(machineID, eventMask)

End Sub



I've also managed to get access to the events seen in the object browser using the following code in Class1 Module:


Public WithEvents mApp As zkemkeeper.CZKEM




Public Sub mApp_OnFinger()
Worksheets(1).Range("A1") = "HETHOM"
End Sub


Public Sub mApp_OnKeyPress(ByVal Key As Long)
Worksheets(1).Range("A1") = "HETHOM"
End Sub


Public Sub mApp_onVerify(ByVal UserID As Long)
Worksheets(1).Range("A1") = "HETHOM"
End Sub




However, even though I connect to the reader successfully and run the RegEvent method, which according to the SDK documentation enables real-time events to be triggered, the event handlers never execute. I'm not sure if it is enough that I do the event handler in the Class1 module - I've tried creating an object of Class1, and I can call the event handlers manually, but they don't seem to be triggered when I use the fingerprint reader.

I'm sure there are probably many places where things can go wrong - but I guess my question is mainly if event handlers can be triggered in a class module or if I still have to do something with Class1 in the Module1 code.

Bob Phillips
06-22-2019, 04:06 PM
Where do you instantiate your class?

jpswart82
06-22-2019, 10:32 PM
Where do you instantiate your class?
I don't instantiate it in the code given, but when I tried to instantiate it, I did in the public sub connect(). It didn't help though, so I took it out. Do you have to instantiate it for the event handlers to work? I looked at an example where the person just had a user form and the custom event handlers in the class module, with no instantiation of the class, but perhaps I missed it in the code.

Bob Phillips
06-23-2019, 03:02 AM
I cannot speak of the example you saw, maybe it was because you had a userform that is an implicit class, but to get you event handling to effect, you need to instantiate your class. I am totally guessing here as I know nothing about ZKTECO, but looking at your code I would have thought that maybe the test variable should be type Class1 and then instantiate with

tester = New Class1

jpswart82
06-23-2019, 06:43 AM
I cannot speak of the example you saw, maybe it was because you had a userform that is an implicit class, but to get you event handling to effect, you need to instantiate your class. I am totally guessing here as I know nothing about ZKTECO, but looking at your code I would have thought that maybe the test variable should be type Class1 and then instantiate with

tester = New Class1

Okay thanks that's good to know. I've also come accross some other issues which might interfere (I.e. The connection terminated when the procedure finishes presumably because the object was only locally declared). But I will report back with further progress / questions, or hopefully some answers.

jpswart82
06-23-2019, 11:14 AM
Thank you xld, in the end it was your inputs that got me my answer.

The final code which is now working:


Option Explicit


Public tmpClass As New Class1




Public Sub connect()
Dim IPAddress As String
Dim Ports As Long
Dim connected As Boolean

Dim vbool As Variant
Dim i As Integer
Dim success As Boolean
Dim UserID As Long
Dim fingerIndex As Long
Dim machineID As Long
Dim eventMask As Long
Dim counter As Long



IPAddress = "192.168.1.201"
Ports = 4370



UserID = 10
fingerIndex = 0
eventMask = 2


connected = tmpClass.mApp.Connect_NET(IPAddress, Ports)
machineID = 1
vbool = tmpClass.mApp.RegEvent(machineID, eventMask)


End Sub




and the Class module:


Public WithEvents mApp As CZKEM






Private Sub Class_Initialize()
Set mApp = New CZKEM
Worksheets(1).Range("A1") = "HETHOMNOGNIE"
End Sub


Private Sub mApp_OnConnected()
Worksheets(1).Range("A1") = "HETHOM"
End Sub




Private Sub mApp_OnFinger()
Worksheets(1).Range("A1") = "HETHOM"
End Sub


Private Sub mApp_OnKeyPress(ByVal Key As Long)
Worksheets(1).Range("A1") = "HETHOM"
End Sub


Private Sub mApp_onVerify(ByVal UserID As Long)
Worksheets(1).Range("A1") = "HETHOM"
End Sub




Thanks for your help.

Bob Phillips
06-23-2019, 12:15 PM
Glad to see you got it working mate.

Just one comment on your code, I would rename your class to something more aligned to the product ZKTECO, and your variable along the same lines, such as Public instZKTECO As ZKTECO, as that would be more code self-documenting in style.

jpswart82
06-23-2019, 09:53 PM
Glad to see you got it working mate.

Just one comment on your code, I would rename your class to something more aligned to the product ZKTECO, and your variable along the same lines, such as Public instZKTECO As ZKTECO, as that would be more code self-documenting in style.

Yes thanks, will definitely do. The code provided was just for me to try and figure out how to get it working before I start writing my program. But generally speaking I probably don’t write the most legible code (my choice of variable names typically not so “readable”) so i rely on comments more for readability.

Bob Phillips
06-24-2019, 03:54 AM
But generally speaking I probably don’t write the most legible code (my choice of variable names typically not so “readable”) so i rely on comments more for readability.

Lol, I am quite the opposite. I rarely do code comments, so I try and get the code as self-documenting as possible.