I take NO RESPONSIBILITY for what damage this may cause.
I needed an application that could help identify what was using all the bandwidth of our internet connection, so I decided to write the application myself. The code came from all over the place as using WMI is insane confusing.
Well this was a massive pain to get working as I have never used WMI but having found and awesome application from Microsoft called “WMI Code Creator v1.0″ made it possible.
When the application is run it minimise’s to the task menu. To use just Select the network adapter you want to monitor and click “Start Now”. All the network adapters installed should show up in the listbox. The total downloads and upload amounts are the Adapters Total data transfer since the last boot.
ok Now for some code…..
Declare some stuff and generate list of Network Adapters and add to listbox called lstAdapters
Dim strComputer, objWMIService, total_sent, total_received, total_obj 'create list of adapters Try Dim searcher As New ManagementObjectSearcher( _ "root\CIMV2", _ "SELECT * FROM Win32_NetworkAdapter WHERE PhysicalAdapter = True") For Each queryObj As ManagementObject In searcher.Get() lstAdapters.Items.Add(queryObj("Name")) Next Catch err As ManagementException MessageBox.Show("An error occurred while querying for WMI data: " & err.Message) End Try
Now the Meat of the code
Sub Object_OnScriptEnter() total_sent = "" total_received = "" ' get total bytes received and sent strComputer = "." objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\WMI") ' get total received total_obj = objWMIService.ExecQuery("SELECT * FROM MSNdis_ReceivesOk") For Each objItem In total_obj 'If objItem.InstanceName = "Realtek PCIe GBE Family Controller" Then If objItem.InstanceName = lstAdapters.SelectedItem.ToString Then total_received = objItem.NdisReceivesOk * 1.35 / 1000 '& " received " & vbNewLine End If Next ' get total sent total_obj = objWMIService.ExecQuery("SELECT * FROM MSNdis_TransmitsOk") For Each objItem In total_obj If objItem.InstanceName = lstAdapters.SelectedItem.ToString Then total_sent = objItem.NdisTransmitsOk * 1.35 / 1000 End If Next
Now to Call OnScriptEnter() using a timer
Private Sub tmrNetworkStat_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tmrNetworkStat.Tick Try Object_OnScriptEnter() Catch End Try End Sub