How to send data to a C# App from VBScript

S

ShieldsJared

Hello all,

I've been working on an application for a while now and have now come
to a standstill.

I have an application that I intend to have stored on a file server
(developed in C#), which is where the application will be directly ran
from. While running, the application will run a various assortment of
VBScripts (also located on the file server) to which I would like the
vbscripts to be able to pass data back to the currently running
application.

There will never be more than one instance of the application running,
and the scripts will never be ran w/o the application.

I have looked into making the executable a COM Server but then I found
out when publishing public functions form an Executable to COM, only
makes a new object when instantiating it. In other words, in my
VBScript if I can CreateObject() It literally creates a new instance
of the executable. Im trying to pass data to the currently running
instance. I basically want to send status and such from the vbscript
to the running executable. The running executable will be the calling
process to run these scripts.

Does anyone have any ideas on how I can do this?

I'd like to avoid the registry to do this, unless its just a one time
addition to the registry.

Thanks so much in advance for your help!
 
D

David Browne

Hello all,

I've been working on an application for a while now and have now come
to a standstill.

I have an application that I intend to have stored on a file server
(developed in C#), which is where the application will be directly ran
from. While running, the application will run a various assortment of
VBScripts (also located on the file server) to which I would like the
vbscripts to be able to pass data back to the currently running
application.

There will never be more than one instance of the application running,
and the scripts will never be ran w/o the application.

I have looked into making the executable a COM Server but then I found
out when publishing public functions form an Executable to COM, only
makes a new object when instantiating it. In other words, in my
VBScript if I can CreateObject() It literally creates a new instance
of the executable. Im trying to pass data to the currently running
instance. I basically want to send status and such from the vbscript
to the running executable. The running executable will be the calling
process to run these scripts.

Does anyone have any ideas on how I can do this?

I'd like to avoid the registry to do this, unless its just a one time
addition to the registry.

Thanks so much in advance for your help!

I would just shell out (using System.Diagnostics.Process) to cscript.exe,
passing it the name of the vbscript file and redirect standard output. Then
in the script use WScript.Echo to write to the output.

Like this:


string scriptName = "foo.vbs";
ProcessStartInfo si = new
ProcessStartInfo("cscript.exe",scriptName);
si.RedirectStandardOutput = true;
si.UseShellExecute = false;

Process p = Process.Start(si);
while (true)
{
string s = p.StandardOutput.ReadLine();
if (s == null)
{
break;
}
Console.WriteLine("VBScript output:" + s);
}


David
 
W

Willy Denoyette [MVP]

Hello all,

I've been working on an application for a while now and have now come
to a standstill.

I have an application that I intend to have stored on a file server
(developed in C#), which is where the application will be directly ran
from. While running, the application will run a various assortment of
VBScripts (also located on the file server) to which I would like the
vbscripts to be able to pass data back to the currently running
application.

There will never be more than one instance of the application running,
and the scripts will never be ran w/o the application.

I have looked into making the executable a COM Server but then I found
out when publishing public functions form an Executable to COM, only
makes a new object when instantiating it. In other words, in my
VBScript if I can CreateObject() It literally creates a new instance
of the executable. Im trying to pass data to the currently running
instance. I basically want to send status and such from the vbscript
to the running executable. The running executable will be the calling
process to run these scripts.

Does anyone have any ideas on how I can do this?

I'd like to avoid the registry to do this, unless its just a one time
addition to the registry.

Thanks so much in advance for your help!


Both C# and your VBScript run in separate processes, that means that you'll have to start
the script from C# using System.Diagnostics.Process, all you can do to "communicate back" to
the parent process is by redirecting the standard output of the scripting engine.
This is quite limiting, all you can do is send text back using WScript.Echo, but maybe it
suits your needs.

Willy.
 
S

ShieldsJared

Both C# and your VBScript run in separate processes, that means that you'll have to start
the script from C# using System.Diagnostics.Process, all you can do to "communicate back" to
the parent process is by redirecting the standard output of the scripting engine.
This is quite limiting, all you can do is send text back using WScript.Echo, but maybe it
suits your needs.

Willy.- Hide quoted text -

- Show quoted text -

Im familar w/ wscript.echo, but my goal is to have the output
displayed within the gui I am creating. I basically want to create a
library vb script function foo(string) that passes the string to the
external application....

I am aware of how to call the vbscripts....
 
D

David Browne

Im familar w/ wscript.echo, but my goal is to have the output
displayed within the gui I am creating. I basically want to create a
library vb script function foo(string) that passes the string to the
external application....

If you use CScript for your script host then

sub foo(string)
Wscript.Echo(string)
end sub

will write the string to the process's standard output. When you spawn a
process from C#, you can redirect the child process's standard output and
read it from C#.

David
 
S

ShieldsJared

If you use CScript for your script host then

sub foo(string)
Wscript.Echo(string)
end sub

will write the string to the process's standard output. When you spawn a
process from C#, you can redirect the child process's standard output and
read it from C#.

David- Hide quoted text -

- Show quoted text -

Oh really??

How would I go about redirecting the output so that I can read it in
C#?

That sounds perfect!
 
S

ShieldsJared

If you use CScript for your script host then

sub foo(string)
Wscript.Echo(string)
end sub

will write the string to the process's standard output. When you spawn a
process from C#, you can redirect the child process's standard output and
read it from C#.

David- Hide quoted text -

- Show quoted text -

NM, I found it! Yall are awesome and this is so much easier!!!

Thanks!!!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top