question

anandan002 avatar image
0 Votes"
anandan002 asked ·

dynamic vbscript execution

Hi,

My requirement is to execute the vbscript from C# (not from file). Followed the below article and tried to execute a simple statement WScript.Echo "Missing parameters but it's always giving syntax error as mentioned in the below article.

https://stackoverflow.com/questions/42632185/how-to-format-vbscript-string-executed-dynamically-through-c-sharp

Please guide to execute vbscript from C#

dotnet-csharpdotnet-runtime
· 2
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

@anandan002
Are you writing like this?
string sScript = "WScript.Echo \"Missing parameters\"";
Modify it to string sScript = "WScript.Echo'Missing parameters'"; then this error will disappear, but a new error will appear at the same time:
System.Runtime.InteropServices.COMException: 'Object required: 'WScript''
It seems that writing vbscript directly in c# has many limitations.
Why can't you write it to the file and then use the file?

0 Votes 0 ·

@TimonYang-MSFT

I'm able to execute the vbscript file without any error, but the requirement is to embed the vbscript in the executable file and execute.

Since the vbscript file is embedded i'm not able to execute it directly (Error: path not found)

Instead i'm reading the content of the file and executing it as dynamic script.

0 Votes 0 ·

1 Answer

Castorix31 avatar image
0 Votes"
Castorix31 answered ·

This test works fine for me =>

 var scriptType = Type.GetTypeFromCLSID(Guid.Parse("0E59F1D5-1FBE-11D0-8FF2-00A0D10038BC"));
 dynamic objScriptControl = Activator.CreateInstance(scriptType);
 objScriptControl.Language = "vbscript";
 string sScript = "function Myfunction()" + Environment.NewLine +
     "Dim Text, Button, Title, Result" + Environment.NewLine +
      "Text = \"Are you OK ?\"" + Environment.NewLine +
        "Button = vbYesNoCancel+ vbQuestion" + Environment.NewLine +
         "Title = \"Question\"" + Environment.NewLine +
           "Result = MsgBox(Text, Button, Title)    " + Environment.NewLine +
 "end function" + Environment.NewLine;
 objScriptControl.AddCode(sScript);
 objScriptControl.Eval("Myfunction()");
· 4 ·
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Thanks for the solution..

Is it possible to get the vb script execution output in C#? (like Process.StandardOutput & Process.StandardError)

This would be helpful to know the script execution status

0 Votes 0 ·

From this old MSDN article, event handlers can be added, but I never tested...


0 Votes 0 ·

@Castorix31

Thanks for the update..

One more last question I'm getting the below error when i tried to execute WScript.S leep(5000)

  System.Runtime.InteropServices.COMException
  HResult=0x800A01A8
  Message=Object required: 'WScript'
  Source=System.Dynamic

I have also tried the below script, but it's also throwing the same Object Required error

 set objShell = createObject("wscript.shell")
 objShell.S leep(5000)
0 Votes 0 ·
Show more comments