private IFROW_HELPER PrivToPub(MIB_IFROW pri) { IFROW_HELPER ifhelp = new IFROW_HELPER(); ifhelp.Name = pri.wszName.Trim(); ifhelp.Index = Convert.ToInt32(pri.dwIndex); ifhelp.Type = Convert.ToInt32(pri.dwType); ifhelp.Mtu = Convert.ToInt32(pri.dwMtu); ifhelp.Speed = Convert.ToInt64(pri.dwSpeed); // Changed from ToInt32 to ToInt64 for VISTA ifhelp.PhysAddrLen = Convert.ToInt32(pri.dwPhysAddrLen); ifhelp.PhysAddr = MAC2String(pri.bPhysAddr); ifhelp.AdminStatus = Convert.ToInt32(pri.dwAdminStatus); ifhelp.OperStatus = Convert.ToInt32(pri.dwOperStatus); ifhelp.LastChange = Convert.ToInt64(pri.dwLastChange); // Changed from ToInt32 to ToInt64 to make it work and to uncomment ifhelp.InOctets = Convert.ToInt32(pri.dwInOctets); ifhelp.InUcastPkts = Convert.ToInt32(pri.dwInUcastPkts); ifhelp.InNUcastPkts = Convert.ToInt32(pri.dwInNUcastPkts); ifhelp.InDiscards = Convert.ToInt32(pri.dwInDiscards); ifhelp.InErrors = Convert.ToInt32(pri.dwInErrors); ifhelp.InUnknownProtos = Convert.ToInt32(pri.dwInUnknownProtos); ifhelp.OutOctets = Convert.ToInt32(pri.dwOutOctets); ifhelp.OutUcastPkts = Convert.ToInt32(pri.dwOutUcastPkts); ifhelp.OutNUcastPkts = Convert.ToInt32(pri.dwOutNUcastPkts); ifhelp.OutDiscards = Convert.ToInt32(pri.dwOutDiscards); ifhelp.OutErrors = Convert.ToInt32(pri.dwOutErrors); ifhelp.OutQLen = Convert.ToInt32(pri.dwOutQLen); ifhelp.Description = System.Text.Encoding.ASCII.GetString(pri.bDescr, 0, Convert.ToInt32(pri.dwDescrLen)); ifhelp.InMegs = ToMegs(ifhelp.InOctets); ifhelp.OutMegs = ToMegs(ifhelp.OutOctets); return(ifhelp); }
//[DebuggerStepThrough] private IFROW_HELPER PrivToPub(MIB_IFROW pri) { IFROW_HELPER ifhelp = new IFROW_HELPER(); ifhelp.Name = pri.wszName.Trim(); ifhelp.Index = Convert.ToInt32(pri.dwIndex); ifhelp.Type = Convert.ToInt32(pri.dwType); ifhelp.Mtu = Convert.ToInt32(pri.dwMtu); ifhelp.Speed = Convert.ToInt32(pri.dwSpeed); ifhelp.PhysAddrLen = Convert.ToInt32(pri.dwPhysAddrLen); ifhelp.PhysAddr = Encoding.ASCII.GetString(pri.bPhysAddr); ifhelp.AdminStatus = Convert.ToInt32(pri.dwAdminStatus); ifhelp.OperStatus = Convert.ToInt32(pri.dwOperStatus); ifhelp.LastChange = Convert.ToInt32(pri.dwLastChange); ifhelp.InOctets = pri.dwInOctets; //Convert.ToInt32(pri.dwInOctets); ifhelp.InUcastPkts = Convert.ToInt32(pri.dwInUcastPkts); ifhelp.InNUcastPkts = Convert.ToInt32(pri.dwInNUcastPkts); ifhelp.InDiscards = Convert.ToInt32(pri.dwInDiscards); ifhelp.InErrors = Convert.ToInt32(pri.dwInErrors); ifhelp.InUnknownProtos = Convert.ToInt32(pri.dwInUnknownProtos); ifhelp.OutOctets = Convert.ToInt32(pri.dwOutOctets); ifhelp.OutUcastPkts = Convert.ToInt32(pri.dwOutUcastPkts); ifhelp.OutNUcastPkts = Convert.ToInt32(pri.dwOutNUcastPkts); ifhelp.OutDiscards = Convert.ToInt32(pri.dwOutDiscards); ifhelp.OutErrors = Convert.ToInt32(pri.dwOutErrors); ifhelp.OutQLen = Convert.ToInt32(pri.dwOutQLen); ifhelp.Description = Encoding.ASCII.GetString(pri.bDescr, 0, Convert.ToInt32(pri.dwDescrLen)); ifhelp.InMegs = this.ToMegs((long)ifhelp.InOctets); ifhelp.OutMegs = this.ToMegs((long)ifhelp.OutOctets); return(ifhelp); }
public clsNetworkStats(bool IgnoreLoopBack) { int lRetSize = 0; MIB_IFROW ifrow = new MIB_IFROW(); byte[] buff = new byte[1]; byte val = 0; long ret = GetIfTable(ref val, ref lRetSize, 0); buff = new byte[lRetSize]; ret = GetIfTable(ref buff[0], ref lRetSize, 0); int lRows = buff[0]; this.m_Adapters = new ArrayList(lRows); byte len = (byte)lRows; for (byte i = 1; i <= len; i++) { ifrow = new MIB_IFROW(); ifrow.dwIndex = Convert.ToUInt32(i); ret = GetIfEntry(ref ifrow); IFROW_HELPER ifhelp = this.PrivToPub(ifrow); if (IgnoreLoopBack) { if (ifhelp.Description.IndexOf("Loopback") < 0) { this.m_Adapters.Add(ifhelp); } } else { this.m_Adapters.Add(ifhelp); } } }
/// <summary> /// New - Undocumented! /// </summary> /// <param name="IgnoreLoopBack"></param> /// <remarks></remarks> public clsNetworkStats([System.Runtime.InteropServices.OptionalAttribute, System.Runtime.InteropServices.DefaultParameterValueAttribute(true)] // ERROR: Optional parameters aren't supported in C# bool IgnoreLoopBack) { long ret = 0; MIB_IFROW ifrow = new MIB_IFROW(); MIB_IFTABLE IfTable = new MIB_IFTABLE(); int tablesize = 0; IntPtr iBuf = IntPtr.Zero; //get tablesize ret = GetIfTable(iBuf, ref tablesize, true); //resize buffer on tablesize iBuf = Marshal.AllocHGlobal(tablesize); //load buffer ret = GetIfTable(iBuf, ref tablesize, true); //marshar from buffer into MIB_IFTABLE structure IfTable = (MIB_IFTABLE)Marshal.PtrToStructure(iBuf, typeof(MIB_IFTABLE)); //initialize adapter list m_Adapters = new ArrayList(); int noInterfaces = Convert.ToInt32(IfTable.dwNumEntries); //NIC Interfaces number int IFROWSize = Marshal.SizeOf(ifrow); MIB_IFROW[] mrows = new MIB_IFROW[(int)noInterfaces + 1]; byte[] mDest = null; mDest = new byte[IFROWSize * noInterfaces + 1]; for (byte i = 1; i <= Convert.ToByte(noInterfaces); i++) { mrows[i - 1] = (MIB_IFROW)Marshal.PtrToStructure(new IntPtr(iBuf.ToInt32() + 4 + (i - 1) * IFROWSize), typeof(MIB_IFROW)); IFROW_HELPER ifhelp = PrivToPub(mrows[i - 1]); if (IgnoreLoopBack == true) { if (ifhelp.Description.IndexOf("Loopback") < 0) { m_Adapters.Add(ifhelp); } } else { m_Adapters.Add(ifhelp); } } Marshal.FreeHGlobal(iBuf); }
/// <summary> /// GetAdapter - Undocumented! /// </summary> /// <returns></returns> /// <remarks></remarks> public IFROW_HELPER GetAdapter() { IFROW_HELPER functionReturnValue = default(IFROW_HELPER); short i = 0; for (i = 0; i <= Convert.ToInt16(m_Adapters.Count); i++) { functionReturnValue = (IFROW_HELPER)m_Adapters(i); if (functionReturnValue.PhysAddr.ToString != "00-00-00-00-00-00") { return; } } return(null); return(functionReturnValue); }