-1

I have an .net C# Winforms project which works fine.

What I want to do is create a process which executes a VB script. I already accomplished that with the following code:

string ScriptName = "myScript.vbs";

Process ScriptProcess = new Process();
ScriptProcess.StartInfo.FileName = @ScriptName;
ScriptProcess.StartInfo.WorkingDirectory = @"C:\scripts\";
ScriptProcess.StartInfo.Arguments = string.Format("\"{0}\" \"{1}\"","Arg1","Arg2");
ScriptProcess.Start();
ScriptProcess.WaitForExit();
ScriptProcess.Close();

The arguments are passed to the 'myScript.vbs' and processed. However I wanted to manipulate e.g. a label in my c# Winform to display the result by using this vbs script. Therefore I somehow need to pass the control to it.

How can I do this?

I haven't found out a solution for that and I hope you can give me a hint.

Lahiru Karunaratne
  • 2,020
  • 16
  • 18
SUM
  • 49
  • 7
  • you cannot pass a reference to the control, but you can make the vb script return a value and assign that to a control in the calling application. or use any other means of inter-process communication, even a temporary file, named pipe etc. for a simple approach see duplicate. – Cee McSharpface Aug 30 '17 at 09:03
  • Well this means I have to handle the return values in the C# code. This means I have to cover all cases which may return. Which can be very a very large number. I don't want to exchange ascii files. I planned on passing the control / or its handle so that the VB script can manipulate it itsself. There is also a difference in using WScript and CScript regarding to returning a value. – SUM Aug 30 '17 at 10:05
  • You don't have to cover any cases or do any *"calculations"* in C#. Whatever you're planning do display on the control *from VBScript*, just write it to the standard output instead, and then from C#, you can read the output and display it on the control. **Did you check the answer in the link posted by @Smartis??** – 41686d6564 stands w. Palestine Aug 30 '17 at 11:11
  • Yes I read the link provided by Smartis. I already added the outputWriter and errorWriter. I created a test vbs which now looks like this: `Dim Arg, var1, var2` `Set Arg = WScript.Arguments` `var1 = Arg(0)` `var2 = Arg(1)` `if var2 = 2 then` _DO STUFF_ `WScript.StdOut.Write "RETURNVAL"` `set Arg = Nothing` Using the suggested WScript.StdOut (see mentioned link) I get an errormsg saying 'invalid handle', because only cscript.exe supports this command. WScript doesn't. I also tried using csript.exe to execute the vbs. An error doesn't come up but there is no output (std,error). – SUM Aug 30 '17 at 12:12
  • @SUM - That's a different problem. Debug your VBScript using CScript.exe outside of your program and get that working first. Get the command-line you intend to execute completely determined, and then add taht back into your C# program. – STLDev Aug 30 '17 at 14:05

1 Answers1

0

Ok, I have written a solution: C# Code:

Process p = new Process();
p.StartInfo = new ProcessStartInfo("cscript");
p.startInfo.UseShellExecute = false;
p.startInfo.RedirectStandardOutput= true;
p.startInfo.RedirectStandardError= true;

var outputWriter = new StringWriter();
p.OutputDataReceived += (sender, args) => outputWriter.WriteLine(args.Data);
var errorWriter = new StringWriter();
p.ErrorDataReceived += (sender, args) => errorWriter.WriteLine(args.Data)

p.StartInfo.Arguments = string.Format("//Nologo C:\\scripts\myscript.vbs "\"{0} \"{1}"",x,y);

OutputWriter.NewLine ="";
errorWriter.NewLine ="";

p.Start();
p.BeginOutputReadLine();
p.BeginErrorReadLine();
p.WaitForExit();

//Just to see output and errors coming back.
Console.WriteLine(outputWriter.GetStringBuilder().ToString());
Console.WriteLine(errorWriter.GetStringBuilder().ToString());

p.Close();
p.Dispose();

My vb script looks the same I just post the important part of it: @STLDeveloper the code was fine.

Dim Arg, var1, var2
Set Arg = WScript.Arguments

Set objStdOut = WScript.StdOut
Set objStdErr = WScript.StdErr

var1 = Arg(0)
var2 = Arg(1)

if var2 = 2 then
**do stuff**
objStdOut.Write "Msg 1"
End if

set Arg = Nothing

I wanted to mention that with the code from the link (posted by Smartis) I ran into difficulties.

However now I can pass parameters and get a Output / result back which I handle further in the c# code. Not quite the thing I was looking for at the beginning but I think I can work with that.

For just one value it is good. For a set of values I have to parse them to use it properly.

Thanks.

SUM
  • 49
  • 7