AutoHotkey Homepage AutoHotkey Community
Let's help each other out
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Network Download/Upload Meter
Goto page 1, 2, 3, 4  Next
 
Post new topic   Reply to topic    AutoHotkey Community Forum Index -> Scripts & Functions
View previous topic :: View next topic  
Author Message
Sean



Joined: 12 Feb 2007
Posts: 1776

PostPosted: Tue Apr 03, 2007 8:04 am    Post subject: Network Download/Upload Meter Reply with quote

It displays the network download/upload KB in progress bar.
I adapted Skan's excellent GUI for HDD Monitor for it:
http://www.autohotkey.com/forum/viewtopic.php?p=113890#113890

I assumed the index of the active network adapter's interface is 2.
And, I made it to truncate the rate at 100KB, which I think can be easily adjustable.

Code:
#SingleInstance, Force

Gui, -Caption +Border +AlwaysOnTop +ToolWindow
Gui, Color, EEAA99
Gui, +LastFound
WinSet, TransColor, EEAA99
Gui, Add, Progress,      w100 h10 cGreen -0x1 vDn
Gui, Add, Progress, x+10 w100 h10 cRed   -0x1 vUp
Gui, Show, x780 y3 , NetMeter                  ; Adjust X & Y to suit your screen res

If GetIfTable(tb)
   ExitApp

Loop, % DecodeInteger(&tb)
{
   If DecodeInteger(&tb + 4 + 860 * (A_Index - 1) + 544) < 4 || DecodeInteger(&tb + 4 + 860 * (A_Index - 1) + 516) = 24
      Continue
   ptr := &tb + 4 + 860 * (A_Index - 1)
      Break
}

If !ptr
   ExitApp

SetTimer, NetMeter, On, 1000
Return

NetMeter:
DllCall("iphlpapi\GetIfEntry", "Uint", ptr)

dnNew := DecodeInteger(ptr + 552)      ; Total Incoming Bytes
upNew := DecodeInteger(ptr + 576)      ; Total Outgoing Bytes

dnRate := Round((dnNew - dnOld) / 1024)
upRate := Round((upNew - upOld) / 1024)

GuiControl,, Dn, %dnRate%
GuiControl,, Up, %upRate%

dnOld := dnNew
upOld := upNew
Return


GetIfTable(ByRef tb, bOrder = False)
{
   nSize := 4 + 860 * GetNumberOfInterfaces() + 8
   VarSetCapacity(tb, nSize)
   Return DllCall("iphlpapi\GetIfTable", "Uint", &tb, "UintP", nSize, "int", bOrder)
}

GetIfEntry(ByRef tb, idx)
{
   VarSetCapacity(tb, 860)
   DllCall("ntdll\RtlFillMemoryUlong", "Uint", &tb + 512, "Uint", 4, "Uint", idx)
   Return DllCall("iphlpapi\GetIfEntry", "Uint", &tb)
}

GetNumberOfInterfaces()
{
   DllCall("iphlpapi\GetNumberOfInterfaces", "UintP", nIf)
   Return nIf
}

DecodeInteger(ptr)
{
   Return *ptr | *++ptr << 8 | *++ptr << 16 | *++ptr << 24
}


PS. I believe the following script mimics 'netstat -e', i.e., blindly add up the total downloaded/uploaded bytes of all interfaces.
I recommend it to the users for whom the first script doesn't work, as a temporary work-around.

Code:
#SingleInstance, Force

Gui, -Caption +Border +AlwaysOnTop +ToolWindow
Gui, Color, EEAA99
Gui, +LastFound
WinSet, TransColor, EEAA99
Gui, Add, Progress,      w100 h10 cGreen -0x1 vDn
Gui, Add, Progress, x+10 w100 h10 cRed   -0x1 vUp
Gui, Show, x780 y3 , NetMeter                  ; Adjust X & Y to suit your screen res

If GetIfTable(tb)
   ExitApp

SetTimer, NetMeter, On, 1000
Return

NetMeter:
dnNew := 0
upNew := 0

GetIfTable(tb)

Loop, % DecodeInteger(&tb)
{
/*  Include this codes to exclude the loopback interface.
   If DecodeInteger(&tb + 4 + 860 * (A_Index - 1) + 516) = 24
      Continue
*/
   dnNew += DecodeInteger(&tb + 4 + 860 * (A_Index - 1) + 552)      ; Total Incoming Octets
   upNew += DecodeInteger(&tb + 4 + 860 * (A_Index - 1) + 576)      ; Total Outgoing Octets
}

dnRate := Round((dnNew - dnOld) / 1024)
upRate := Round((upNew - upOld) / 1024)

GuiControl,, Dn, %dnRate%
GuiControl,, Up, %upRate%

dnOld := dnNew
upOld := upNew
Return


GetIfTable(ByRef tb, bOrder = True)
{
   nSize := 4 + 860 * GetNumberOfInterfaces() + 8
   VarSetCapacity(tb, nSize)
   Return DllCall("iphlpapi\GetIfTable", "Uint", &tb, "UintP", nSize, "int", bOrder)
}

GetIfEntry(ByRef tb, idx)
{
   VarSetCapacity(tb, 860)
   DllCall("ntdll\RtlFillMemoryUlong", "Uint", &tb + 512, "Uint", 4, "Uint", idx)
   Return DllCall("iphlpapi\GetIfEntry", "Uint", &tb)
}

GetNumberOfInterfaces()
{
   DllCall("iphlpapi\GetNumberOfInterfaces", "UintP", nIf)
   Return nIf
}

DecodeInteger(ptr)
{
   Return *ptr | *++ptr << 8 | *++ptr << 16 | *++ptr << 24
}


Last edited by Sean on Sat Apr 28, 2007 1:35 am; edited 4 times in total
Back to top
View user's profile Send private message
Timothy
Guest





PostPosted: Tue Apr 03, 2007 2:22 pm    Post subject: Reply with quote

It doesn't work with mine. I tried both wired and wireless connections and doesn't work with any of them. What can be adjusted to try and make it work with mine?
Back to top
Grumpy
Guest





PostPosted: Tue Apr 03, 2007 2:41 pm    Post subject: Reply with quote

Of course. Just send your computer to Sean, and he will see what is wrong...
Back to top
Sean



Joined: 12 Feb 2007
Posts: 1776

PostPosted: Tue Apr 03, 2007 3:34 pm    Post subject: Reply with quote

Timothy wrote:
It doesn't work with mine. I tried both wired and wireless connections and doesn't work with any of them. What can be adjusted to try and make it work with mine?

What does the following code display?

Code:
MsgBox % GetIfTable()


GetIfTable(bOrder = True)
{
   DllCall("iphlpapi\GetNumberOfInterfaces", "UintP", nIf)
   nSize := 4 + 860 * nIf + 8
   VarSetCapacity(tbl, nSize)
   DllCall("iphlpapi\GetIfTable", "Uint", &tbl, "UintP", nSize, "int", bOrder)

   Loop, %nIf%
         Interfaces .= DecodeInteger(&tbl + 4 + 512 + 860 * (A_Index - 1)) . " : " . DecodeInteger(&tbl + 4 + 516 + 860 * (A_Index - 1)) . " : " . DecodeInteger(&tbl + 4 + 544 + 860 * (A_Index - 1)) . "`n"

   Return Interfaces
}

DecodeInteger(ptr)
{
   Return *ptr | *++ptr << 8 | *++ptr << 16 | *++ptr << 24
}
Back to top
View user's profile Send private message
Timothy
Guest





PostPosted: Tue Apr 03, 2007 7:26 pm    Post subject: Reply with quote

1 : 24 : 5
65539 : 6 : 5
65540 : 6 : 0
65541 : 6 : 0
Back to top
bold



Joined: 20 Apr 2006
Posts: 16

PostPosted: Tue Apr 03, 2007 9:56 pm    Post subject: Reply with quote

It did not work for me also because I have all sorts of virtual network interfaces (for vpn and vmware).

I did the following:

Type the this command in a command prompt and check the output. The output will list all your network interfaces.

Code:
ipconfig /all


Check the position of the correct network interface. The first entry is your hostname, below that is the list of network interfaces. For me the sixth entry was the correct one so I replaced the 2 at the end of this line to 6.

Code:
DllCall("RtlFillMemory", "Uint", &tb + 512, "Uint", 1, "Uchar", 2)   ; 1-based index of the interface including loopback
Back to top
View user's profile Send private message
Sean



Joined: 12 Feb 2007
Posts: 1776

PostPosted: Tue Apr 03, 2007 11:30 pm    Post subject: Reply with quote

Timothy wrote:
1 : 24 : 5
65539 : 6 : 5
65540 : 6 : 0
65541 : 6 : 0

Is this correct? If it's correct, you have to replace

Code:
DllCall("RtlFillMemory", "Uint", &tb + 512, "Uint", 1, "Uchar", 2)

with

Code:
DllCall("ntdll\RtlFillMemoryUlong", "Uint", &tb + 512, "Uint", 4, "Uint", 65539)
Back to top
View user's profile Send private message
Sean



Joined: 12 Feb 2007
Posts: 1776

PostPosted: Tue Apr 03, 2007 11:47 pm    Post subject: Reply with quote

bold wrote:
It did not work for me also because I have all sorts of virtual network interfaces (for vpn and vmware).

You figured it out yourself very nicely in spite of my poor explanation. Thanks.
Back to top
View user's profile Send private message
majkinetor



Joined: 24 May 2006
Posts: 3716
Location: Belgrade

PostPosted: Wed Apr 04, 2007 9:03 am    Post subject: Reply with quote

Well, everybody have couple nowdays.
Sean, can you fix it not to depend on this ?

Take one as default and let user choose another one via menu or something ? Or, display all at start and let the user hide unwanted connections.
_________________
Back to top
View user's profile Send private message MSN Messenger
SKAN



Joined: 26 Dec 2005
Posts: 6841

PostPosted: Wed Apr 04, 2007 10:24 am    Post subject: Reply with quote

Very nice, Sean. Very Happy

BTW, do you know how to ascertain the bytes received & sent ( as shown in LAN status ) ?
Back to top
View user's profile Send private message
Sean



Joined: 12 Feb 2007
Posts: 1776

PostPosted: Wed Apr 04, 2007 1:39 pm    Post subject: Reply with quote

majkinetor wrote:
Well, everybody have couple nowdays.
Sean, can you fix it not to depend on this ?

I modified it as to detect the active one and then to start monitoring it.
I'm still not sure if it'll always pick out the correct one.
Back to top
View user's profile Send private message
majkinetor



Joined: 24 May 2006
Posts: 3716
Location: Belgrade

PostPosted: Wed Apr 04, 2007 1:41 pm    Post subject: Reply with quote

Hej, it works here now Very Happy
Can you close it in function, like:

Code:
  GetNetworkLoad( networkID="A" )

_________________
Back to top
View user's profile Send private message MSN Messenger
Sean



Joined: 12 Feb 2007
Posts: 1776

PostPosted: Wed Apr 04, 2007 1:45 pm    Post subject: Reply with quote

Skan wrote:
Very nice, Sean. Very Happy

Thanks! It's your GUI, BTW. Smile

Quote:
BTW, do you know how to ascertain the bytes received & sent ( as shown in LAN status ) ?

I'm not sure if I understand you correctly, but the dnNew/upNew is the total bytes (:which is mostly octets these days) received/sent up to now. Is this the proper answer?
Back to top
View user's profile Send private message
Sean



Joined: 12 Feb 2007
Posts: 1776

PostPosted: Wed Apr 04, 2007 2:10 pm    Post subject: Reply with quote

majkinetor wrote:
Hej, it works here now Very Happy

Good to know. Thanks.

Quote:
Can you close it in function, like:

Code:
  GetNetworkLoad( networkID="A" )

Do you mean like GetIfEntry(ByRef tb, idx)?
It's already there, however, I didn't use it as I couldn't decide whether incorporating offset is good or not.

PS. Oops, I forgot idx.
Back to top
View user's profile Send private message
SKAN



Joined: 26 Dec 2005
Posts: 6841

PostPosted: Wed Apr 04, 2007 3:55 pm    Post subject: Reply with quote

Sean wrote:

Skan wrote:
BTW, do you know how to ascertain the bytes received & sent ( as shown in LAN status ) ?

I'm not sure if I understand you correctly, but the dnNew/upNew is the total bytes (:which is mostly octets these days) received/sent up to now. Is this the proper answer?


It does not match the bytes sent/recvd shown in my Local Area Connection Status dialog!

I had been wanting to write a bandwidth usage solution for my customised needs, but have never been successful in fetching the exact Sent/Recvd bytes. Sad .. Related : Bandwidth Meter/Measurer

Sean wrote:
I made it to truncate the rate at 100KB, which I think can be easily adjustable


FYI:

I have a rather slow ADSL connection On Ethernet in W2K OS. The progress bar does not budge until I remove the "/ 1024"

The current version automatically selects.. Exclamation Very Happy For the previous version I had to alter the RtlFill... to 1 instead of 2.

Thanks .. Smile
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    AutoHotkey Community Forum Index -> Scripts & Functions All times are GMT
Goto page 1, 2, 3, 4  Next
Page 1 of 4

 
Jump to:  
You can post new topics in this forum
You can reply to topics in this forum


Powered by phpBB © 2001, 2005 phpBB Group