Option Explicit
Dim g_receivedCallback
If WScript.Arguments.Named.Exists("NEW") Then
RunSecondInstance
Else
RunFirstInstance
End If
Sub RunSecondInstance()
Dim oSa, oWindow, oData, oCallback
' Search for the window
Set oSa = CreateObject("Shell.Application")
For Each oWindow In oSa.Windows
If TypeName(oWindow.Document) = "HTMLDocument" Then
If InStr(oWindow.Document.Title, "IPC Window") > 0 Then
' Get the data object, set a property and callback a method
Set oData = oWindow.GetProperty ("IPCData")
Set oCallback = oData.Callback
oData.Value = "Success!"
Call oCallback
End If
End If
Next
End Sub
Sub RunFirstInstance()
Dim oData, oIe, oWs
' Create a object to pass to a other script
Set oData = New IPCData
' Set a property to a callback method
Set oData.Callback = GetRef("MyCallback")
' Create a window and store the data in the window
Set oIe = CreateObject("InternetExplorer.Application")
oIe.Navigate "about:blank"
Do Until oIe.ReadyState = 4 : WScript.Sleep 5 : Loop
oIe.Document.Title = "IPC Window"
oIe.PutProperty "IPCData", oData
' Run second script instance
Set oWs = CreateObject("WScript.Shell")
oWs.Run "WSCRIPT.EXE """ & WScript.ScriptFullName & """ /NEW"
' Wait for callback from second script
Do Until g_receivedCallback = True : WScript.Sleep 5 : Loop
' Display received data
MsgBox oData.Value
' Close ie
oIe.Quit
End Sub
Sub MyCallback()
g_receivedCallback = True
End Sub
Class IPCData
Private m_callback
Public Property Get Callback()
Set Callback = m_callback
End Property
Public Property Set Callback(ByVal v)
Set m_callback = v
End Property
Private m_value
Public Property Get Value()
If IsObject(m_value) Then
Set Value = m_value
Else
Value = m_value
End If
End Property
Public Property Let Value(ByVal v)
m_value = v
End Property
Public Property Set Value(ByVal v)
Set m_value = v
End Property
End Class