3

我需要将数据从一个进程发送到另一个进程。约束:

发送方进程调用非常昂贵。它需要使用 vbscipt 来完成。对于 Sender 进程,此数据传输是一项额外的工作。它不应受到此功能的太大影响。在 4-5 分钟内,发送方进程中有大约 1000 个线程。

更快的IPC很重要。如果可以异步完成会更好。我读到了命名管道。是否可以使用 vbscript 打开命名管道。考虑到上述约束,还有其他可能的方法。

4

2 回答 2

4

使用命名管道可能是原生 VBScript 的唯一选择。您可以通过用其他语言编写 COM 对象来访问任何其他 IPC 方法。

命名管道可以像文件一样被写入,因此您可以使用FileSystemObject从命名管道打开和读取/写入。打开命名管道的格式是使用格式\\\\.\pipe\PipeName(将 PipeName 替换为管道的实际名称)。

所以要在 VBScript 中写入命名管道:

Set fs = CreateObject("Scripting.FileSystemObject")
Set a = fs.CreateTextFile("\\.\pipe\PipeName", True)
a.WriteLine("This is a test.")
a.Close
于 2012-06-08T05:37:10.140 回答
2
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
于 2013-03-13T02:20:41.293 回答