'//////////////////////////////////////////////////////////////////////////////

' Author: SY

' Demo ScriptEZ.API based application which uses facilities from RAD API:

' . Named Pipe usage

' . Threadpool usage

' . Echo() method usage which displays always messages into a console window

'   instead of WScript.Echo() with 2 behaviours for cscript.exe or wscript.exe

' . ScriptEZ.API Remoting Control Interface usage by NPTelnet.exe program

'   (Connect to ScriptEZ.API process with <processid (PID)> as connection name)

'

' Purpose: Display API Coverage from a ScriptEZ.API based process

'//////////////////////////////////////////////////////////////////////////////

 

const SCRIPTEZ_API = "ScriptEZ.API" ' Component name

 

Dim ScriptEZ

Dim ScriptEZMain

Dim hNamedPipeClient

Dim Err_num

Dim hThreadPool

Dim onExit

Dim InputString

Dim bstrData

Dim toClear

Dim toDisplay

Dim hSocketSvr

Dim APICoverage

' start the script

'/////////////////

Main()

 

' Main Subroutine

'////////////////

Sub Main()

 

  Dim HostAndPort

 

  toDisplay= 1

  toClear  = 0

  onExit   = 0

  APICoverage = ""

 

  Set WshShell = CreateObject("WScript.Shell")

  Set fso      = CreateObject("Scripting.FileSystemObject")

 

  Dim Windir

  Dim FullQualifiedEngineName

  ' if Win64, restart with WScript.exe/CScript.exe 32 bits

  On Error Resume Next

  Windir = WshShell.ExpandEnvironmentStrings("%WinDir%")

  FullQualifiedEngineName = Windir+"\SysWOW64" + Mid(WScript.FullName,InStrRev(WScript.FullName,"\"),Len(WScript.FullName))

  If fso.FolderExists(Windir+"\SysWOW64") And _

     LCase(WScript.FullName) <>  LCase(FullQualifiedEngineName) And _

     fso.FileExists(FullQualifiedEngineName) = True Then

    WshShell.Run FullQualifiedEngineName + " " + Chr(34) + CStr(WScript.ScriptFullName) + Chr(34)

    Exit Sub

  End If

 

  ' instantiate component object with callback interface

  Set ScriptEZMain = WScript.CreateObject(SCRIPTEZ_API,"ScriptEZMain_")

  Set ScriptEZ = WScript.CreateObject(SCRIPTEZ_API,"ScriptEZ_")

  'ScriptEZMain.SetCPUCoreAffinity "0"

 

  ' create client NamedPipe object + xtra encryption

  hNamedPipeClient = ScriptEZMain.CreateNamedPipeClientObject

  'ScriptEZMain.EnableNamedPipeObjectDataEncryption hNamedPipeClient,true,-1

 

  ' connect to server

  InputString = InputBox("Enter your hostname and named pipe, such like localhost:12346", _

                         "Choose a hostname", _

                         "localhost:12346")

  If Len(InputString) <= 0 Then Exit Sub

  HostAndPort = Split(InputString,":")

 

  Err_num  = ScriptEZMain.ConnectNamedPipeServerObject(HostAndPort(0), HostAndPort(1), hNamedPipeClient)

  If Err_num <> 0 Then Exit Sub

 

  ' create server socket object +  extra  encryption

  hSocketSvr = ScriptEZ.CreatePoolSocketServerObject(12349,vbNullString,100,8,0,0)

 

  ' Register App for receiving AppEvent(s) with onAppEvent(EventID)

  ScriptEZMain.AppRegister

 

  If hSocketSvr > 0 Then

    ScriptEZMain.SetConsoleTitle "API Coverage for process " + InputString + " | listener@12349"

  Else

    ScriptEZMain.SetConsoleTitle "API Coverage for process " + InputString

  End If

  ' creating thread pool with 4 threads for polling info

  hThreadPool = ScriptEZMain.CreateTaskQueueObject(4)

  r = ScriptEZMain.AddTask( hThreadPool, hNamedPipeClient )   

 

  ' block until NamedPipe break or exit event

  Do Until Err_num <> 0  Or onExit = 3

      WScript.Sleep 1000

  Loop

 

  ' empty queued msg for sending

  r = ScriptEZMain.SendNamedPipeObjectData(hNamedPipeClient,vbNullString,0)

 

  ' App unregister before ending

  ScriptEZMain.AppUnregister

  WScript.Sleep 4000

  Set ScriptEZMain = Nothing

 

End Sub

 

' Callbacks for App Events notification and fetching help text

'/////////////////////////////////////////////////////////////

Sub ScriptEZMain_onAppEvent(EventID)

 ScriptEZMain.Echo " -> EventID:" + CStr(EventID)

 Select Case EventID

   Case 3  onExit= 3

 End Select

End Sub

 

Function ScriptEZMain_AppEventHelp()

 ScriptEZMain_AppEventHelp = vbLF + _

  CStr("Filename: " + Chr(34) + CStr(WScript.ScriptFullName) + Chr(34) + vbLF + _

        "AppEventID = 3" + vbTab + "to exit")

End Function

 

 

' Callbacks for socket object

' ( server side )

'///////////////////////////

Function ScriptEZ_onConnectionRequest(hSocketClientObjectProxy)

 ScriptEZ_onConnectionRequest = CLng(1)

 r = ScriptEZ.EnableSocketObjectNonBlockingModeUsage(hSocketClientObjectProxy,1)

End Function

 

' ( client/proxy side)

'/////////////////////

Function ScriptEZ_onConnectionState(hOwnerSocket,state_desc,error_number)

 ScriptEZ.Echo state_desc

 ScriptEZ_onConnectionState = CLng(0)

End Function

 

' Callbacks for NamedPipe object

' ( client/proxy side)

'///////////////////////////

Function ScriptEZMain_onConnectionStateNamedPipe(hOwnerNamedPipe,state_desc,error_number)

  ScriptEZMain.Echo state_desc

  Err_num =  error_number

  ScriptEZMain_onConnectionStateNamedPipe = CLng(0)

End Function

 

' ( client/proxy side)

'//////////////////////////

Function ScriptEZMain_onDataNamedPipe(hOwnerNamedPipe,raw_data,data_size)

  If toClear = 1 Then

    ScriptEZMain.ClearConsole

    toClear = 0

    toDisplay = 1

  End If

  ' converting string to BSTR

  bstrData   = ScriptEZMain.ConvertStringToBSTR(raw_data)

  ScriptEZMain_onDataNamedPipe = CLng(-1) ' raw_data will be free-ed by the component

  If InStr(bstrData,"ready>") Then

    toClear = 1

    toDisplay = 1

  End If

 

  If InStr(bstrData," ---") Then toDisplay = 0

  If toDisplay = 1 And bstrData <> "?" Then

     ScriptEZMain.Echo bstrData

     APICoverage = APICoverage + bstrData

  ElseIf toDisplay = 1 And bstrData = "?" Then

     Do While 1

     hSocketClient = ScriptEZ.EnumActiveSocketClientObject(hSocketSvr)

     If hSocketClient <= 0 Then

        Exit Do

     End If

       If Len(APICoverage) > 0 Then r = ScriptEZ.SendSocketObjectData( hSocketClient,CStr(Date) + " | " + _

                                                                       CStr(Time)+vbLF + APICoverage, _

                                                                       Len(CStr(Date) + " | " + _

                                                                       CStr(Time)+vbLF+APICoverage) )  

     Loop

     APICoverage = ""

  End If

  ScriptEZMain.FreeBSTR bstrData

End Function

 

' Notification on Task aborting

'//////////////////////////////

Sub ScriptEZMain_onAbort( lpString )

End Sub

 

' Callback for thread pool object (TaskQueueObject)

'//////////////////////////////////////////////////

Function ScriptEZMain_onRun( localStorage )

  If Err_num <> 0  Or onExit = 3 Then

   ScriptEZMain_onRun = CLng(0) ' stop this task

   Exit Function

  Else

   ScriptEZMain_onRun = CLng(1) ' run again this task

  End If

 

  WScript.Sleep 10000 ' Every 10 seconds

  r = ScriptEZMain.SendNamedPipeObjectData(localStorage,"?",1)

End Function