Reputation: 375
I have the following code:
Process scriptProc = new Process();
scriptProc.StartInfo.FileName = @"cscript";
scriptProc.StartInfo.WorkingDirectory = @"C:\MyPath\";
scriptProc.StartInfo.Arguments = "filename.vbs //X";
scriptProc.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
scriptProc.Start();
scriptProc.WaitForExit();
scriptProc.Close();
My VBS opens in an editor(Visual Studio) which is specified by the //X attribute, but this only opens if the script has no syntax errors, it is not opening in the editor if I have script errors, which basically makes the use of the debugger as redundant.
Is there any way with which I can debug a VBScript using C# only?
Upvotes: 2
Views: 1318
Reputation: 12602
The code below uses @Ekkehard.Horner approaches. Compile it, then drag and drop .vbs files onto executable to test whether the file has syntax errors or not:
using System;
using System.IO;
using System.Reflection;
using System.Windows.Forms;
using System.Runtime.InteropServices;
// Add reference to COM Microsoft Script Control 1.0
// Code works for .Net 2.0 and above
class Program
{
static void Main(string[] args)
{
// Check whether a file was dragged onto executable
if (args.Length != 1)
{
MessageBox.Show("Drag'n'drop .vbs file onto this executable to check syntax");
return;
}
MessageBox.Show("Syntax will be checked for\r\n" + args[0]);
String vbscode = "";
// Read the content of the file
try
{
StreamReader sr = new StreamReader(args[0]);
vbscode = sr.ReadToEnd();
}
catch (Exception e)
{
MessageBox.Show("File reading error " + e.Message);
return;
}
// Add statement raising runtime error -2147483648 in the first line to ScriptControl
int hr = 0;
try
{
vbscode = "Err.Raise &H80000000\r\n" + vbscode;
MSScriptControl.ScriptControl sc = new MSScriptControl.ScriptControl();
sc.Language = "VBScript";
sc.AddCode(vbscode);
}
catch (Exception e)
{
hr = Marshal.GetHRForException(e);
// First line of code executed if no syntax errors only
if (hr == -2147483648)
{
// Run time error -2147483648 shows that execution started without syntax errors
MessageBox.Show("Syntax OK");
}
else
{
// Otherwise there are syntax errors
MessageBox.Show("Syntax error");
}
}
}
}
Upvotes: 1
Reputation: 38745
A debugger is a tool for dealing with run-time errors. So it can't be used to check for compile-time errors.
Unfortunately, the c|wscript.exe script hosts don't have an option like Perl's -c (syntax check). Running cscript maybebad.vbs
to catch syntax errors may be not convenient if that executes a flawless shutdown/format my harddisk/... script accidentally/unwittingly. You could write a script that Execute(Global)
the code of maybebad.vbs with a WScript.Quit 1
prepended.
There is the MS ScriptControl that could be used to avoid the shelling out; I'm not sure, whether that will streamline your 'debugging experience'.
Upvotes: 2
Reputation: 3255
In answer to your question, no, I'm afraid you cannot debug the VBScript from within a debugging context of C#. Try debugging your script directly with something like http://www.vbsedit.com. By launching the script in C# first, you're complicating matters.
Upvotes: 1