Imports System.Runtime.InteropServices
Imports System.Text

Module SNFMultiDLLExample

#Region "DLL Imports"

    ' Location of SNFMulti.dll.
    Const SNFMULTI_DLL As String = "..\..\..\..\..\64bitDll\SNFMulti.dll" ' Set CPU type to "Any CPU"
    'Const SNFMULTI_DLL As String = "..\..\..\..\..\..\32bitDll\SNFMulti.dll" ' Set CPU type to "x86"

    'int setThrottle(int Threads);                                                   /* Set scan thread limit. */
    'int startupSNF(char* Path);                                                     /* Start SNF with configuration. */
    'int shutdownSNF();                                                              /* Shutdown SNF. */
    'int testIP(unsigned long int IPToCheck);                                        /* Test the IP for a GBUdb range. */
    'int scanBuffer(unsigned char* Bfr, int Length, char* Name, int Setup);          /* Scan msgBuffer, name, setup time. */
    'int scanFile(char* FilePath, int Setup);                                        /* Scan msgFile, setup time. */
    'int getScanXHeaders(int ScanHandle, char** Bfr, int* Length);                   /* Get result & XHeaders. */
    'int getScanXMLLog(int ScanHandle, char** Bfr, int* Length);                     /* Get result & XML Log. */
    'int getScanClassicLog(int ScanHandle, char** Bfr, int* Length);                 /* Get result & Classic Log. */
    'int getScanResult(int ScanHandle);                                              /* Get just the scan result. */
    'int closeScan(int ScanHandle);                                                  /* Close the scan result. */


    'Set scan thread limit
    <DllImport(SNFMULTI_DLL, EntryPoint:="setThrottle", CharSet:=CharSet.Ansi, CallingConvention:=CallingConvention.Winapi)> _
    Public Function setThrottle(ByVal Threads As Int32) As Int32
    End Function

    'Startup
    <DllImport(SNFMULTI_DLL, EntryPoint:="startupSNF", CharSet:=CharSet.Ansi, CallingConvention:=CallingConvention.Winapi)> _
    Public Function startupSNF(ByVal PathToConfigurationFile As String) As Int32
    End Function

    'Shutdown
    <DllImport(SNFMULTI_DLL, EntryPoint:="shutdownSNF", CharSet:=CharSet.Ansi, CallingConvention:=CallingConvention.Winapi)> _
    Public Function shutdownSNF() As Int32
    End Function

    'Test IP
    <DllImport(SNFMULTI_DLL, EntryPoint:="testIP", CharSet:=CharSet.Ansi, CallingConvention:=CallingConvention.Winapi)> _
    Public Function testIP(ByVal IPToCheck As UInt32) As Int32
    End Function

    'ScanBuffer - returns scannerhandle
    <DllImport(SNFMULTI_DLL, EntryPoint:="scanBuffer", CharSet:=CharSet.Ansi, CallingConvention:=CallingConvention.Winapi)> _
    Public Function scanBuffer(ByVal MsgBfr As StringBuilder, ByVal MsgBfrLen As Int32, ByVal MsgID As String, ByVal SetupTime As Int32) As Int32
    End Function

    'ScanFile
    <DllImport(SNFMULTI_DLL, EntryPoint:="scanFile", CharSet:=CharSet.Ansi, CallingConvention:=CallingConvention.Winapi)> _
    Public Function scanFile(ByVal FilePath As String, ByVal Setup As Int32) As Int32
    End Function

    'GetScanXHeaders - use scannerhandle returned from previous - returns ScanResult Code
    <DllImport(SNFMULTI_DLL, EntryPoint:="getScanXHeaders", CharSet:=CharSet.Ansi, CallingConvention:=CallingConvention.Winapi)> _
    Public Function getScanXHeaders(ByVal ScannerHandle As Int32, ByRef HeadersPtr As IntPtr, ByRef HeadersLength As Int32) As Int32
    End Function

    'GetScanXMLLog - Get result & XML Log
    <DllImport(SNFMULTI_DLL, EntryPoint:="getScanXMLLog", CharSet:=CharSet.Ansi, CallingConvention:=CallingConvention.Winapi)> _
    Public Function getScanXMLLog(ByVal ScannerHandle As Int32, ByRef LogPtr As IntPtr, ByRef LogLength As Int32) As Int32
    End Function

    'GetScanClassicLog - Get result & Classic Log
    <DllImport(SNFMULTI_DLL, EntryPoint:="getScanClassicLog", CharSet:=CharSet.Ansi, CallingConvention:=CallingConvention.Winapi)> _
    Public Function getScanClassicLog(ByVal ScannerHandle As Int32, ByRef LogPtr As IntPtr, ByRef LogLength As Int32) As Int32
    End Function

    'GetScanResult - Get just the scan result
    <DllImport(SNFMULTI_DLL, EntryPoint:="getScanResult", CharSet:=CharSet.Ansi, CallingConvention:=CallingConvention.Winapi)> _
    Public Function getScanResult(ByVal ScannerHandle As Int32) As Int32
    End Function

    'Close Scan
    <DllImport(SNFMULTI_DLL, EntryPoint:="closeScan", CharSet:=CharSet.Ansi, CallingConvention:=CallingConvention.Winapi)> _
    Public Function closeScan(ByVal ScannerHandle As Int32) As Int32
    End Function

#End Region

    Sub Main()
        Dim LastResult As Integer
        Dim StartupFile As String = "C:\SNFServer\SNFServer.xml"

        Console.WriteLine("Hello, world!")
        Console.Write("startupSNF(): ")
        LastResult = startupSNF(StartupFile)
        Console.WriteLine(LastResult.ToString("D"))
        Console.Write("setThrottle(4):  ")
        LastResult = setThrottle(4)
        Console.WriteLine(LastResult.ToString("D"))

        Console.Write("Scanning ugly text:  ")

        Dim UglyText1 As New StringBuilder
        UglyText1.Append( _
          "Received: from [213.93.213.84] (HELO uw-4b58d8528225.arnhem.chello.nl)\r\n" + _
          "   by inbound.appriver.com (CommuniGate Pro SMTP 5.0.6)\r\n" + _
          "From: ""somebody"" <somebody@someplace.com>\r\n" + _
          "Subject: Nothing to see here\r\n" + _
          "\r\n" + _
          "So, this is a message eh?\r\n" + _
          ".\r\n")

        Dim HandleOne As Integer
        HandleOne = scanBuffer(UglyText1, UglyText1.Length(), "", 0)

        Console.WriteLine("Handle from scanBuffer:  " + Format("D", HandleOne))

        Dim StringPtr As IntPtr = IntPtr.Zero
        Dim StringLength As Int32
        Dim XHeaders As String

        LastResult = getScanXHeaders(HandleOne, StringPtr, StringLength)
        XHeaders = System.Runtime.InteropServices.Marshal.PtrToStringAnsi(StringPtr, StringLength)
        Console.WriteLine("getScanXHeaders():  " + LastResult.ToString("D"))
        Console.WriteLine("Xheaders length:  " + StringLength.ToString("D"))
        Console.WriteLine("XHeaders:  " + XHeaders)

        StringPtr = IntPtr.Zero
        Dim XMLLog As String

        LastResult = getScanXMLLog(HandleOne, StringPtr, StringLength)
        XMLLog = System.Runtime.InteropServices.Marshal.PtrToStringAnsi(StringPtr, StringLength)

        Console.WriteLine("getScanXMLLog():  " + LastResult.ToString("D"))
        Console.WriteLine("XML Log length:  " + StringLength.ToString("D"))
        Console.WriteLine("XML Log:  '" + XMLLog + "'")

        StringPtr = IntPtr.Zero
        Dim ClassicLog As String

        LastResult = getScanClassicLog(HandleOne, StringPtr, StringLength)
        ClassicLog = System.Runtime.InteropServices.Marshal.PtrToStringAnsi(StringPtr, StringLength)

        Console.WriteLine("getScanClassicLog():  " + LastResult.ToString("D"))
        Console.WriteLine("Classic Log length:  " + StringLength.ToString("D"))
        Console.WriteLine("Classic Log:  '" + ClassicLog + "'")

        LastResult = getScanResult(HandleOne)

        Console.WriteLine("getScanResult():  " + LastResult.ToString("D"))

        LastResult = closeScan(HandleOne)
        Console.WriteLine("closeScan(): " + LastResult.ToString("D"))

        Dim FilePath As String = "..\\..\\..\\..\\junkmsg.txt"

        Dim HandleTwo As Integer
        HandleTwo = scanFile(FilePath, 0)

        Console.WriteLine("Handle from scanFile:  " + Format("D", HandleTwo))

        LastResult = closeScan(HandleTwo)
        Console.WriteLine("closeScan(): " + LastResult.ToString("D"))

        Dim IP As UInt32 = 32768
        LastResult = testIP(IP)
        Console.WriteLine("TestIP(" + IP.ToString("D") + "):  " + _
                          LastResult.ToString("D"))

        LastResult = shutdownSNF()
        Console.WriteLine("shutdownSNF():  " + LastResult.ToString("D"))
    End Sub

End Module
