/// <summary> /// Get all adapters in the system. /// /// For the NDIS_MEDIUM enumeration: 0 - NdisMedium802_3; 16 - NdisMediumNative802_11 /// </summary> /// <returns></returns> private List <NM_NIC_ADAPTER_INFO> GetAdapters() { uint errno; uint adapterCnt; List <NM_NIC_ADAPTER_INFO> adapters = new List <NM_NIC_ADAPTER_INFO>(); if (this.captureEngineHandle != IntPtr.Zero) { errno = NetmonAPI.NmGetAdapterCount(this.captureEngineHandle, out adapterCnt); if (errno == 0) { for (uint i = 0; i < adapterCnt; i++) { NM_NIC_ADAPTER_INFO adapterInfo = new NM_NIC_ADAPTER_INFO(); adapterInfo.Size = (ushort)System.Runtime.InteropServices.Marshal.SizeOf(typeof(NM_NIC_ADAPTER_INFO)); errno = NetmonAPI.NmGetAdapter(this.captureEngineHandle, i, ref adapterInfo); if (errno == 0) { //Only take Ethernet or 802.11 wireless adapters. if (adapterInfo.MediaType == 0 || (uint)adapterInfo.MediaType == 16) { adapters.Add(adapterInfo); } } else { throw new Exception("NmGetAdapter() failed. Error=" + errno); } } } } return(adapters); }
public void Test() { bool isElevated; WindowsIdentity identity = WindowsIdentity.GetCurrent(); WindowsPrincipal principal = new WindowsPrincipal(identity); isElevated = principal.IsInRole(WindowsBuiltInRole.Administrator); Console.WriteLine($"Elevated permissions: {isElevated}"); capHandler = new CaptureCallbackDelegate(CapHandlerCallback); uint ret; IntPtr myCapEng; ret = NetmonAPI.NmOpenCaptureEngine(out myCapEng); if (ret != 0) { Console.WriteLine("Error {0}\n", ret); } else { uint AdptCount; ret = NetmonAPI.NmGetAdapterCount(myCapEng, out AdptCount); if (ret != 0) { NetmonAPI.NmCloseHandle(myCapEng); Console.WriteLine("Error {0}\n", ret); } else { // Creates a capture file which will store the last 10MB of traffic captured ret = NetmonAPI.NmCreateCaptureFile(this.filename, 10000000, NmCaptureFileFlag.WrapAround, out this.capFile, out this.size); if (ret != 0) { Console.Write("Error Creating File"); } Console.WriteLine($"Adapters avalable: {AdptCount}"); for (uint i = 0; i < AdptCount; i++) { ret = NetmonAPI.NmConfigAdapter(myCapEng, i, capHandler, IntPtr.Zero, NmCaptureCallbackExitMode.ReturnRemainFrames); if (ret != 0) { Console.WriteLine("Could not config {0}, error {1}", i, ret); } else { Console.WriteLine("Configured Adpt {0}", i); } ret = NetmonAPI.NmStartCapture(myCapEng, i, NmCaptureMode.Promiscuous); if (ret != 0) { Console.WriteLine("Could not Start Capture on {0}, error {1}", i, ret); } else { Console.WriteLine("Started Adpt {0}", i); } } System.Threading.Thread.Sleep(5000); for (uint i = 0; i < AdptCount; i++) { ret = NetmonAPI.NmConfigAdapter(myCapEng, i, capHandler, IntPtr.Zero, NmCaptureCallbackExitMode.ReturnRemainFrames); if (ret != 0) { Console.WriteLine("Could not config {0}, error {1}", i, ret); } else { Console.WriteLine("Configured Adpt {0}", i); } Console.WriteLine("Starting Adpt {0} again", i); ret = NetmonAPI.NmStartCapture(myCapEng, i, NmCaptureMode.Promiscuous); if (ret != 0) { Console.WriteLine("Could not Start Capture again on {0}, error {1}", i, ret); } else { Console.WriteLine("Started Adpt {0} again", i); } } for (uint i = 0; i < AdptCount; i++) { ret = NetmonAPI.NmStopCapture(myCapEng, i); if (ret != 0) { Console.WriteLine("Could not Stop Capture on {0}, error {1}", i, ret); } else { Console.WriteLine("Stopped Adpt {0}", i); } } } NetmonAPI.NmCloseHandle(myCapEng); } }