View Full Version : Getting an event from an ActiveX dll in VBA

11-22-2005, 07:18 AM
Hi Everyone,

Thanks for taking the time to look at this. I'm a self-taught VBA programmer (read: ugly, inefficient, seat-of-the-pants code) with a question. I'm trying to grab network packets directly into Excel. I've downloaded the PacketX dll, which is an ActiveX wapper for the WinPCap library. I tried to use it and am only getting about halfway to where I want to be.

I can call functions in PacketX with no problem, however the event that should be firing when a packet gets received (OnPacket) is never triggered. I'm wondering if there are some nuances or tricks to reading events that are different from just calling procedures in a .dll

(I know next to nothing about using .dlls from VBA, so much of what you see below was taken and modified from a working VBScript document example posted on the PacketX website. Therefore it may be a very obvious solution that I'm overlooking.)

Below is the code that I've modified, and below that the output. Note that the stats say that packets have arrived, but the event indicating their arrival never fired.

Thanks in advance for any help.

Public oPktX As Object

Public Sub CommandButton1_Click()
Set oPktX = New PacketX

'Pick adapter #2 and state that you have
oPktX.Adapter = oPktX.Adapters(2)
Debug.Print "Chosen Adapter = " & oPktX.Adapter.Description

oPktX.Adapter.BuffSize = 1 * 1024 '// 1 KB
oPktX.Adapter.BuffMinToCopy = 0

'// Hardware filter and capture mode
oPktX.Adapter.HWFilter = PktXLinkTypeLocalTalk
oPktX.Adapter.Mode = PktXModeCapture

Debug.Print "Is Good: " & oPktX.Adapter.IsGood
Debug.Print "Link Speed:" & oPktX.Adapter.LinkSpeed Debug.Print oPktX.Adapter.Device

'// Start capture
Debug.Print "Starting Cap At " + CStr(Now)
Application.Wait (Now + TimeValue("0:00:05"))

Debug.Print "------------------- Capture Statistics --------------------"
Debug.Print "Packets received " & oAdapter.PacketsRecv
Debug.Print "Packets lost " & oAdapter.PacketsLost
Debug.Print "--------------------------------------------------------" Debug.Print
End Sub

Public Sub PacketX_OnPacket(ByRef oPacket)
End Sub

(1) Generic NdisWan adapter
(2) Broadcom NetXtreme Gigabit Ethernet Driver (Microsoft's Packet Scheduler)
Chosen Adapter = Broadcom NetXtreme Gigabit Ethernet Driver (Microsoft's Packet Scheduler)
Is Good: 1
Link Speed:10000000
Starting Cap At 11/22/2005 8:30:09 AM
------------------- Capture Statistics --------------------
Packets received 7
Packets lost 0

11-22-2005, 02:26 PM
You would want to create a custom Class Module. This is a quick skeleton example off the top of my head. Your code would have to be incorporated. There's a bit more to it than this (property procedures, etc.), but it should at least lead you in the right direction.

Option Explicit
'The PacketObject Class
Private WithEvents oPktX As PacketX

Private Sub Class_Initialize()
Set oPktX = New PacketX
End Sub
Private Sub Class_Terminate()
Set oPktX = Nothing
End Sub

Private Sub oPktX_OnPacket(ByRef oPacket)
MsgBox "The oPktX_OnPacket Event Fired.", vbInformation
End Sub

Then in your regular module, declare an object of the class module type:

Public objPkt As PacketObject

Public Sub YourMethod()
Set objPkt As New PacketObject
Set objPkt = Nothing
End Sub