Csaba
Wed Jul 27 04:14:16 CDT 2005
An idea to call another .vbs program and pick up its output appears in
Post 8 of a thread started March 27, 2003 titled:
Can .Exec be called hidden
http://groups-beta.google.com/group/microsoft.public.scripting.vbscript/msg/b6119a027f408a9c
This method uses volatile environment variables to return the output of
a foreign program without flicker
There are additional thoughts from Michael Harris on the matter from an
April 24, 2005 post titled:
Command line input to CScript/WScript?
http://groups-beta.google.com/group/microsoft.public.scripting.vbscript/browse_frm/thread/b2004813fb2e6eba/
A second idea is to take the file with the code you want to execute,
read it in, and pass its contents through a
CreateObject("MSScriptControl.ScriptControl")
A very primitive example follows, but it shows that it is possible to
pass even objects. The problem in how I have set it up is that it is
not generic enough and each use must be customized. As shown, a fixed
number of arguments must be passed (an array of them would be better),
and their types are assumed. Objects (passed in with .AddObject), in
particular, are a problem for passing in as both of the execution forms
.Eval (returns a value) and .ExecuteStatement take strings so they must
be passed in ahead of time. Nevertheless, this would seem to be a
simple and viable approach in many situations.
Subroutine.vbs:
Function AddToDictionary (key, value)
'arg1 is a dictionary object, defined in caller.vbs
arg1.add key, value
AddToDictionary = true
End function
Caller.vbs
' --- Test section ---
Dim oDict
Set oDict = CreateObject("Scripting.Dictionary")
callForeignFunc3 "AddToDictionary", "subroutine.vbs", _
oDict, "item1", "value1"
'Next line proves the subroutine executed successfully
MsgBox oDict.item("item1")
' --- End Test section ---
Function callForeignFunc3(functionName, fileItLivesIn, _
arg1, arg2, arg3)
Dim fileContent, oScript
callForeignFunc3 = null 'Default return value
fileContent = slurpFile (fileItLivesIn)
If fileContent="" Then Exit Function
Set oScript=CreateObject("MSScriptControl.ScriptControl")
oScript.Language = "VBScript"
oScript.AddCode fileContent
'just to show objects can be passed
oScript.AddObject "arg1", arg1
'Next line assumes arg2 and arg3 are strings,
'but does not properly escape them for double quotes, vbCrLF, etc.
callForeignFunc3 = oScript.Eval _
(functionName & "(""" & arg2 & """,""" & arg3 & """)")
End Function
Function slurpFile(fileName)
'returns the contents of the path fileName as a string
Dim FSO, oFile
const ForReading=1
Set FSO = CreateObject("Scripting.FileSystemObject")
Set oFile = FSO.OpenTextFile(fileName, ForReading)
slurpFile = oFile.ReadAll
oFile.Close
End Function
Csaba Gabor from Vienna