Documents & code‎ > ‎

NETMON 3 API part II /VB.NET/

NETMON 3 API part II /VB.NET/   
post id80 
post length5763 
post datetime4/18/2011 8:30:09 PM 
post ip10.10.10.254 

 




NETMON 3 API part II /VB.NET/





Option Strict Off : Option Explicit On 

#Region " Imports 

Imports System.Runtime.InteropServices _ 
      , System.Globalization _ 
      , System.Security _ 
      , System.IO 

#End Region 

<SuppressUnmanagedCodeSecurity()> _ 
    Public Class NetmonAPI 

    Public Delegate Sub CaptureCallbackDelegate(ByVal hCaptureEngine As [IntPtr] _ 
                                              , ByVal ulAdapterIndex As UInt32 _ 
                                              , ByVal pCallerContext As [IntPtr] _ 
                                              , ByVal hFrame As [IntPtr] _ 
                                              ) 

#Region " Structure`s " 

    <StructLayout(LayoutKind.Sequential, Pack:=1)> _ 
    Public Structure NM_NIC_ADAPTER_INFO 
        <MarshalAs(UnmanagedType.ByValArray, SizeConst:=1596)> _ 
            Public data() As Byte 
        <MarshalAs(UnmanagedType.FunctionPtr)> _ 
            Public CallBackFunction As CaptureCallbackDelegate 
    End Structure 

#End Region 

#Region " Enum`s " 

    Public Enum NmCaptureCallbackExitMode As UInt32 
        DiscardRemainFrames = 1 
        ReturnRemainFrames = 2 
    End Enum 

    Public Enum NmCaptureMode As UInt32 
        LocalOnly = 0 
        Promiscuous = 1 
    End Enum 

#End Region 

#Region " Function`s " 

    <DllImport("NmApi.Dll", CharSet:=CharSet.Ansi)> _ 
    Public Overloads Shared Function NmConfigAdapter(ByVal hCaptureEngine As [IntPtr], ByVal ulIndex AsUInt32, ByVal CallbackFunction As CaptureCallbackDelegate, ByVal pCallerContext As [IntPtr], ByVal ExitMode AsNmCaptureCallbackExitMode) As UInt32 
    End Function 

    <DllImport("NmApi.Dll", CharSet:=CharSet.Ansi)> _ 
    Public Overloads Shared Function NmGetAdapter(ByVal hCaptureEngine As [IntPtr], ByVal ulIndex As UInt32, ByRef pNMAdapterInfo As NM_NIC_ADAPTER_INFO) As UInt32 
    End Function 

    <DllImport("NmApi.Dll", CharSet:=CharSet.Ansi)> _ 
    Public Overloads Shared Function NmGetAdapterCount(ByVal hCaptureEngine As [IntPtr], ByRef ulCount AsUInt32) As UInt32 
    End Function 

    <DllImport("NmApi.Dll", CharSet:=CharSet.Ansi)> _ 
    Public Overloads Shared Sub NmGetApiVersion(ByRef Major As UInt16, ByRef Minor As UInt16, ByRef Build AsUInt16, ByRef Revision As UInt16) 
    End Sub 

    <DllImport("NmApi.Dll", CharSet:=CharSet.Ansi)> _ 
    Public Overloads Shared Function NmGetRawFrame(ByVal hFrame As [IntPtr], ByVal ulLength As UInt32, ByVal pFrameBuffer As [IntPtr], ByRef pulReturnLength As UInt32) As UInt32 
    End Function 

    <DllImport("NmApi.Dll", CharSet:=CharSet.Ansi)> _ 
    Public Overloads Shared Function NmOpenCaptureEngine(ByRef phCaptureEngine As [IntPtr]) As UInt32 
    End Function 

    <DllImport("NmApi.Dll", CharSet:=CharSet.Ansi)> _ 
    Public Overloads Shared Function NmPauseCapture(ByVal hCaptureEngine As [IntPtr], ByVal ulAdapterIndexAs UInt32) As UInt32 
    End Function 

    <DllImport("NmApi.Dll", CharSet:=CharSet.Ansi)> _ 
    Public Overloads Shared Function NmResumeCapture(ByVal hCaptureEngine As [IntPtr], ByVal ulAdapterIndexAs UInt32) As UInt32 
    End Function 

    <DllImport("NmApi.Dll", CharSet:=CharSet.Ansi)> _ 
    Public Overloads Shared Function NmStartCapture(ByVal hCaptureEngine As [IntPtr], ByVal ulAdapterIndex AsUInt32, ByVal CaptureMode As NmCaptureMode) As UInt32 
    End Function 

    <DllImport("NmApi.Dll", CharSet:=CharSet.Ansi)> _ 
    Public Overloads Shared Function NmStopCapture(ByVal hCaptureEngine As [IntPtr], ByVal ulAdapterIndex AsUInt32) As UInt32 
    End Function 

    <DllImport("NmApi.Dll", CharSet:=CharSet.Ansi)> _ 
    Public Overloads Shared Sub NmCloseHandle(ByVal hObjectHandle As [IntPtr]) 
    End Sub 

#End Region 

    Public Shared Sub ReceiveCallback(ByVal hCaptureEngine As [IntPtr], ByVal ulAdapterIndex As UInt32, ByVal pCallerContext As [IntPtr], ByVal hFrame As [IntPtr]) 
        ' 
        Dim Size As Int32 _ 
                = 10000 _ 
          , buff As IntPtr _ 
                = Marshal.AllocHGlobal(Size) 
        NmGetRawFrame(hFrame, Size, buff, Size) 
        ' 
        Console.WriteLine(String.Concat("Received data size: ", Size)) 
        '' 
    End Sub 

    Public Shared Sub Processing() 
        ' 
        Dim ICaptureEngine As [IntPtr] = Nothing _ 
          , BuildNumber As UInt16 = 0 
        NetmonAPI.NmGetApiVersion(NothingNothing, BuildNumber, Nothing
        ' 
        If Not IsNothing(BuildNumber) Then 
            ' 
            NmOpenCaptureEngine(ICaptureEngine) 
            ' 
            Dim AdaptersCount As UInt32 = Nothing _ 
                    : NmGetAdapterCount(ICaptureEngine, AdaptersCount) 
            For As Int32 = 0 To AdaptersCount 
                ' 
                If NetmonAPI.NmConfigAdapter(ICaptureEngine, 1, New CaptureCallbackDelegate(AddressOfReceiveCallback), Nothing, NmCaptureCallbackExitMode.DiscardRemainFrames) = 0 Then 
                    ' 
                    Dim AI As NM_NIC_ADAPTER_INFO = New NM_NIC_ADAPTER_INFO 
                    ' 
                    NetmonAPI.NmGetAdapter(ICaptureEngine, i, AI) 
                    ' 
                    NmStartCapture(ICaptureEngine, i, NmCaptureMode.Promiscuous) 
                    ' 
                End If 
                ' 
            Next 
            ' 
            Console.WriteLine(String.Concat(ControlChars.CrLf _ 
            , "Press ENTER to EXIT..." _ 
            )) 
            Console.ReadLine() 
            ' 
            NmStopCapture(ICaptureEngine, 0) 
            ' 
            NmCloseHandle(ICaptureEngine) 
            ' 
        End If 
        '' 
    End Sub 

End Class 
Comments