Remote and shared lock management (DDE with COM/DCOM way).

 

When ScriptEZ.API based script file is running, an identification area is appended

to the system menu of the console windows as following:

 

 

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

' Author: SY

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

' . Dynamic Data Exchange (DDE) usage

' . Thread Pool 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: Uses DDE msg to ask 3 times per second for  lock/unlock onto  random

'          values from 1 to 10 via DDE Server aka Rldrmon.exe, itself connected

'          to COM/DCOM App aka RLoader.exe running into a local or remote PC

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

 

const SCRIPTEZ_API = "ScriptEZ.API" ' Component name

 

Dim HOSTNAME

Dim WshShell

Dim ScriptEZ

Dim StartTime

Dim onExit

Dim hDde

Dim hThreadPool

Dim locked

Dim failed

 

' start the script

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

'On Error Resume Next

Main()

 

' Main Subroutine

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

Sub Main()

 

  locked       = 0

  failed       = 0

  onExit       = 0

  StartTime    = Now()

  Set WshShell = WScript.CreateObject("WScript.Shell")

 

  ' instantiate component object with callback interface

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

  ScriptEZ.SetCPUCoreAffinity "0" ' CORE1 if multi-core CPU

 

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

  ScriptEZ.AppRegister

 

  Dim hStringTable

  hStringTable = ScriptEZ.MallocStringTable(1)

  r = ScriptEZ.AddStringTableItem(hStringTable,"local")

  HOSTNAME = ScriptEZ.InputComboBox(0,"Select or enter a host where RemoteLoader is running: ",hStringTable)

  ScriptEZ.FreeStringTable hStringTable

  if Len(HOSTNAME) <= 0 Then

      ScriptEZ.AppUnregister

      Set ScriptEZ = Nothing

      Exit Sub

  End If

 

  hDde = ScriptEZ.StartConnectionToDDEServer("dde_remotelock@"+HOSTNAME,"lock@"+HOSTNAME)

  ' retry to start DDE server (rldrmon.exe), if first failure occurred

  If hDde <= 0 Then

   WshShell.Run "rldrmon.exe /autoconnect:"+HOSTNAME,0

   Do Until ScriptEZ.FindWindow("ATL:00424730","Remote Loader Monitor ["+HOSTNAME+"]") > 0

     WScript.Sleep 2000

     ScriptEZ.clearConsole

     ScriptEZ.Echo "...Wait till Remote Loader Monitor ["+HOSTNAME+"] is up !"

   Loop

   WScript.Sleep 3000

   hDde = ScriptEZ.StartConnectionToDDEServer("dde_remotelock@"+HOSTNAME,"lock@"+HOSTNAME)

  End If

  If hDde <= 0 Then

   ScriptEZ.AppUnregister

   Set ScriptEZ = Nothing

   Set WshShell = Nothing

   Exit Sub

  End If

 

  ' Hide console window if 0

  ScriptEZ.ShowConsoleWindow 1

 

  ' creating thread pool with 1 thread for polling info

  hThreadPool = ScriptEZ.CreateTaskQueueObject(1)

  r = ScriptEZ.AddTask( hThreadPool, hDde )   

 

  ScriptEZ.SetConsoleTitle CStr(StartTime) + " - LockOverDDE"

 

  ' block until AppEventID = 3 is received

   Do Until onExit = 3

       WScript.Sleep 200

   Loop

 

  ScriptEZ.AppUnregister

  WScript.Sleep 200

  If hDde > 0 Then ScriptEZ.ReleaseConnectionFromDDEServer hDde

  Set ScriptEZ = Nothing

  Set WshShell = Nothing

End Sub

 

' Callbacks for App Events notification and fetching help text

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

Sub ScriptEZ_onAppEvent(EventID)

 

  If EventID = 3 Then onExit = EventID

  

End Sub

 

Function ScriptEZ_AppEventHelp()

 

 ScriptEZ_AppEventHelp = CStr("AppEventID = 3 to exit")

 

End Function

 

' Callback for thread pool object (TaskQueueObject)

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

Function ScriptEZ_onRun( localStorage )

 

  Dim isLocked

  WScript.Sleep 333

  If hDde > 0 Then

    ' initialise random number

    Randomize

 

    ' random number between 1 and 10 as access-keys

    random = Int( (10-1+1) * Rnd + 1)

 

     isLocked = ScriptEZ.ExecuteCommandOfDDEServer(hDde,"lockon["+CStr(random)+"]")

     If isLocked > 0 Then

      locked = locked + 1

      isLocked = ScriptEZ.ExecuteCommandOfDDEServer(hDde,"lockoff["+CStr(random)+"]")

     Else

      failed = failed + 1

     End If

  End If

  ScriptEZ.SetConsoleTitle CStr(StartTime) + " - LockOverDDE: locked(" + CStr(locked) + "), failed(" + CStr(failed) + ")"

  ScriptEZ.ClearConsole

  ScriptEZ.Echo "Lock server: " + HOSTNAME + vbLF + _

                "Locked ....: " + CStr(Fix((locked/(locked+failed))*100)) +"%" + vbLF + _

                "Failed ....: " + CStr(Fix((failed/(locked+failed))*100)) +"%"

  If onExit = 3 Then

   ScriptEZ_onRun = CLng(0) ' stop this task

  Else

   ScriptEZ_onRun = CLng(1) ' run again this task

  End If

 

End Function