How to Load & Call a Vbscript Function from Within C++

How to get a value from a VBScript to my C++ host?

Just add a second call to ParseScriptText that evaluates your string variable as an expression.

You have to pass SCRIPTTEXT_ISEXPRESSION as the dwFlags argument so that the scripting engine knows that you want to get the value of an expression.

That is also in the official documentation for ParseScriptText:

https://learn.microsoft.com/en-us/scripting/winscript/reference/iactivescriptparse-parsescripttext

I tested it in Delphi and it works like a charm:

procedure TestActiveScripting;
const
SCRIPTTEXT_ISEXPRESSION = $00000020;
var
hr: HResult;
ScriptSite: IActiveScriptSite;
VBScript: IActiveScript;
VBScriptParse: IActiveScriptParse;
res: Variant;
ei: TExcepInfo;
begin
// Initialize
ScriptSite := TSimpleScriptSite.Create as IActiveScriptSite;

hr := CoCreateInstance(CLSID_VBScript, nil, CLSCTX_INPROC_SERVER, IID_IActiveScript, VBScript);
hr := VBScript.SetScriptSite(ScriptSite);
VBScriptParse := VBScript as IActiveScriptParse;
hr := VBScriptParse.InitNew;

// Run some scripts
hr := VBScriptParse.ParseScriptText('s = "something"', nil, nil, nil, 0, 0, 0, @res, ei);
hr := VBScriptParse.ParseScriptText('s', nil, nil, nil, 0, 0, SCRIPTTEXT_ISEXPRESSION, @res, ei);
ShowMessage(res);
end;

How do I include a common file in VBScript (similar to C #include)?

The "Windows Script Host" framework (if ya want to call it that), offers an XML wrapper document that adds functionality over regular vbs files. One of which is the ability to include external script files of both the VBscript and Jscript flavors. I never got very deep into it, but I think it would do what you're wanting to do.
http://msdn.microsoft.com/en-us/library/15x4407c(VS.85).aspx

You can include JavaScript, VBScript, or modules of other WScript script languages.

Example WSF file:

<job id="IncludeExample">
<script language="JavaScript" src="sprintf.js"/>
<script language="VBScript" src="logging.vbs"/>
<script language="VBScript" src="iis-queryScriptMaps.vbs"/>
</job>

If the above file is called "iis-scriptmaps.wsf", run it this way with cscript.exe:

cscript.exe  iis-scriptmaps.wsf

Call another vbscript

I'm not sure what the question is but it sounds like you're wondering how you know what path to use. If so, I think that it should just work with a relative path like .\B.vbs.

Otherwise if the question is how do you execute one script from another, look at Shell.Run.

So all put together, something like WshShell.Run ".\B.vbs arg1 arg2" should work I think.

Edit: If the relative path doesn't work, just use WScript.ScriptFullName to get the path of the currently executing script as:

WshShell.Run Replace(WScript.ScriptFullName, WScript.ScriptName, "") & "B.vbs arg1 arg2"

How to call a VBScript file in a C# application?

The following code will execute a VBScript script with no prompts or errors and no shell logo.

System.Diagnostics.Process.Start(@"cscript //B //Nologo c:\scripts\vbscript.vbs");

A more complex technique would be to use:

Process scriptProc = new Process();
scriptProc.StartInfo.FileName = @"cscript";
scriptProc.StartInfo.WorkingDirectory = @"c:\scripts\"; //<---very important
scriptProc.StartInfo.Arguments ="//B //Nologo vbscript.vbs";
scriptProc.StartInfo.WindowStyle = ProcessWindowStyle.Hidden; //prevent console window from popping up
scriptProc.Start();
scriptProc.WaitForExit(); // <-- Optional if you want program running until your script exit
scriptProc.Close();

Using the StartInfo properties will give you quite granular access to the process settings.

You need to use Windows Script Host if you want windows, etc. to be displayed by the script program. You could also try just executing cscript directly but on some systems it will just launch the editor :)



Related Topics



Leave a reply



Submit