user1443731
user1443731

Reputation: 41

Inter Process Communication using vbscript

I need to send data form one process to another. Constraints :

Sender process is very expensive call. It needs to be done using vbscipt. For Sender process,this data transferring is an additional work.It shouldn't get affected much by this feature. There are around 1000 threads in a sender process in 4-5 mins.

Faster IPC is important.If it can be done asynchronoulsy it will be better. I read about the named pipe.Is it possible to open a named pipe using vbscript .Also is there any other possible way considering the above constraints.

Upvotes: 3

Views: 6310

Answers (2)

Vozzie
Vozzie

Reputation: 21

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

Upvotes: 2

shf301
shf301

Reputation: 31404

Using a named pipe is probably your only option from native VBScript. You could access any of the other IPC methods by writing a COM object in some other language.

A named pipe can be written to just like a file so you can use the FileSystemObject to open and read/write from a named pipe. The format for opening a named pipe is to use the format \\\\.\pipe\PipeName (Replace PipeName with the pipe's actual name).

So to write to a named pipe in VBScript:

Set fs = CreateObject("Scripting.FileSystemObject")
Set a = fs.CreateTextFile("\\.\pipe\PipeName", True)
a.WriteLine("This is a test.")
a.Close

Upvotes: 5

Related Questions