Last active
July 21, 2024 05:32
-
-
Save codeartery/2ef06969d83c3a58544caac8a90f0974 to your computer and use it in GitHub Desktop.
Allows synchronous communication between VBScripts, if you call one from the other. Works with HTA files as well.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Class StdMessageStream | |
REM@description | |
' Allows synchronous communication between VBScripts, if you call one from the other. Works with HTA files as well. | |
' If one file is reading, the other needs to be writing. | |
REM@author | |
' Jeremy England, http://codeartery.com/ | |
REM@mini | |
' Class StdMessageStream:Private s,f,x:Private Sub Class_Initialize:Set s=CreateObject("WScript.Shell"):Set f=CreateObject("Scripting.FileSystemObject"):End Sub:Property Let RunFile(a,p):Set x=s.Exec(a&" """&p&""""):End Property:Function Read:If IsObject(x)Then:Read=x.StdOut.ReadLine:Else:Read=f.GetStandardStream(0).ReadLine:End If:End Function:Sub Write(v):If IsObject(x)Then:x.StdIn.WriteLine(v):Else:f.GetStandardStream(1).WriteLine(v):End If:End Sub:End Class | |
Private oWss, oFso, oExe | |
Private Sub Class_Initialize | |
Set oWss = CreateObject( "WScript.Shell" ) | |
Set oFso = CreateObject( "Scripting.FileSystemObject" ) | |
End Sub | |
Property Let RunFile( pWin32App, pFilePath ) | |
Set oExe = oWss.Exec( pWin32App & " """ & pFilePath & """" ) | |
End Property | |
Function Read() | |
If IsObject( oExe ) Then | |
Read = oExe.StdOut.ReadLine() | |
Else | |
Read = oFso.GetStandardStream( 0 ).ReadLine() | |
End If | |
End Function | |
Sub Write( value ) | |
If IsObject( oExe ) Then | |
oExe.StdIn.WriteLine( value ) | |
Else | |
oFso.GetStandardStream( 1 ).WriteLine( value ) | |
End If | |
End Sub | |
End Class |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
REM@usage | |
' Put the full or mini class/sub/function in your script to use. | |
Class StdMessageStream:Private s,f,x:Private Sub Class_Initialize:Set s=CreateObject("WScript.Shell"):Set f=CreateObject("Scripting.FileSystemObject"):End Sub:Property Let RunFile(a,p):Set x=s.Exec(a&" """&p&""""):End Property:Function Read:If IsObject(x)Then:Read=x.StdOut.ReadLine:Else:Read=f.GetStandardStream(0).ReadLine:End If:End Function:Sub Write(v):If IsObject(x)Then:x.StdIn.WriteLine(v):Else:f.GetStandardStream(1).WriteLine(v):End If:End Sub:End Class | |
REM initialize the object | |
Set oSms = New StdMessageStream | |
REM run a file and communicate with it via the std stream | |
oSms.RunFile("wscript") = "c:\location\of\~usage.1b.vbs" | |
REM send a message to the called script | |
oSms.Write( "Hello, from 1st file." ) | |
REM get a message from the called script | |
Dim Response : Response = oSms.Read() | |
MsgBox Response, vbInformation, WScript.ScriptName |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
REM@usage | |
' Put the full or mini class/sub/function in your script to use. | |
Class StdMessageStream:Private s,f,x:Private Sub Class_Initialize:Set s=CreateObject("WScript.Shell"):Set f=CreateObject("Scripting.FileSystemObject"):End Sub:Property Let RunFile(a,p):Set x=s.Exec(a&" """&p&""""):End Property:Function Read:If IsObject(x)Then:Read=x.StdOut.ReadLine:Else:Read=f.GetStandardStream(0).ReadLine:End If:End Function:Sub Write(v):If IsObject(x)Then:x.StdIn.WriteLine(v):Else:f.GetStandardStream(1).WriteLine(v):End If:End Sub:End Class | |
REM initialize the object | |
Set oSms = New StdMessageStream | |
REM get a message from the caller script | |
Dim Response : Response = oSms.Read() | |
MsgBox Response, vbInformation, WScript.ScriptName | |
REM send a message to the caller script | |
oSms.Write( "Hello, from 2nd file." ) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
REM@usage | |
' Put the full or mini class/sub/function in your script to use. | |
Class StdMessageStream:Private s,f,x:Private Sub Class_Initialize:Set s=CreateObject("WScript.Shell"):Set f=CreateObject("Scripting.FileSystemObject"):End Sub:Property Let RunFile(a,p):Set x=s.Exec(a&" """&p&""""):End Property:Function Read:If IsObject(x)Then:Read=x.StdOut.ReadLine:Else:Read=f.GetStandardStream(0).ReadLine:End If:End Function:Sub Write(v):If IsObject(x)Then:x.StdIn.WriteLine(v):Else:f.GetStandardStream(1).WriteLine(v):End If:End Sub:End Class | |
REM initialize the object | |
Set oSms = New StdMessageStream | |
REM run a file and communicate with it via the std stream | |
oSms.RunFile("mshta") = "c:\location\of\~usage.2b.hta" | |
REM send a message to the called script | |
oSms.Write( "Hello, from 1st file." ) | |
REM get a message from the called script | |
Dim Response : Response = oSms.Read() | |
MsgBox Response |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<head> | |
<meta http-equiv="X-UA-Compatible" content="ie=9"> | |
<title>StdStreamConnect Example</title> | |
<hta:application name="exampleApp" /> | |
<script language="VBScript"> | |
REM@usage | |
' Put the full or mini class/sub/function in your script to use. | |
Class StdMessageStream:Private s,f,x:Private Sub Class_Initialize:Set s=CreateObject("WScript.Shell"):Set f=CreateObject("Scripting.FileSystemObject"):End Sub:Property Let RunFile(a,p):Set x=s.Exec(a&" """&p&""""):End Property:Function Read:If IsObject(x)Then:Read=x.StdOut.ReadLine:Else:Read=f.GetStandardStream(0).ReadLine:End If:End Function:Sub Write(v):If IsObject(x)Then:x.StdIn.WriteLine(v):Else:f.GetStandardStream(1).WriteLine(v):End If:End Sub:End Class | |
REM initialize the object | |
Set oSms = New StdMessageStream | |
</script> | |
</head> | |
<body> | |
<!--get a message from the caller script--> | |
<div id="readPane"></div> | |
<script>readPane.innerText = oSms.Read()</script> | |
<!--send a message to the caller script--> | |
<textarea id="writePane"></textarea> | |
<input type="button" value="Send Message" onclick="oSms.Write(writePane.value)"> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment