Executing javascript from app?

J

jodleren

Hi all

Somehow e.g. InternetExplorer uses the JavaScript plugin - any ideas
how to do that from a C# programme?

The idea is to utilize JS in the software, so we can add a number of
functions depeding on the job etc.
No, I do not want to have it in the code, as it is NOT a part of the
application itself, it is the application which takes use of JS. As of
now I am happy so see alert('Hello world'); work.

I also need to add a plugin onto JS, so we can add a few instructions
there. Mainly IO and some sort of delay to wait for IO.

The script will be client and product dependent.

We have this solution already in Delphi, and need to move to C#. The
standard is given, so there is not much I can change. The system it
already in use.

Any help apriciated

WBR
Sonnich
 
A

Arne Vajhøj

Somehow e.g. InternetExplorer uses the JavaScript plugin - any ideas
how to do that from a C# programme?

The idea is to utilize JS in the software, so we can add a number of
functions depeding on the job etc.
No, I do not want to have it in the code, as it is NOT a part of the
application itself, it is the application which takes use of JS. As of
now I am happy so see alert('Hello world'); work.

I also need to add a plugin onto JS, so we can add a few instructions
there. Mainly IO and some sort of delay to wait for IO.

The script will be client and product dependent.

We have this solution already in Delphi, and need to move to C#. The
standard is given, so there is not much I can change. The system it
already in use.

..NET comes with JScript.NET which supports the JavaScript
language and all of .NET libraries.

You can compile JavaScript code into your app at build time.

And you can also use it dynamically at runtime (little bit
more cumbersome in 2.0+ than in 1.x but still possible).

I can find some code snippets if interested.

Arne
 
J

jodleren

.NET comes with JScript.NET which supports the JavaScript
language and all of .NET libraries.

You can compile JavaScript code into your app at build time.

And you can also use it dynamically at runtime (little bit
more cumbersome in 2.0+ than in 1.x but still possible).

I can find some code snippets if interested.

Yes please. It is funny I have installed .NET with C# but I dont seem
to have JScript.
What have I done wrong?

I chose the complete installation

Sonnich
 
A

Arne Vajhøj

Yes please. It is funny I have installed .NET with C# but I dont seem
to have JScript.
What have I done wrong?

I chose the complete installation

You don't have C:\WINDOWS\Microsoft.NET\Framework\v4.0.30319\jsc.exe and
C:\WINDOWS\Microsoft.NET\Framework\v4.0.30319\
Microsoft.JScript.dll ??

Arne
 
J

jodleren

You don't have C:\WINDOWS\Microsoft.NET\Framework\v4.0.30319\jsc.exe and
C:\WINDOWS\Microsoft.NET\Framework\v4.0.30319\
Microsoft.JScript.dll ??

Arne

I have v1.1.4322\jsc.exe and v2.0.50727\jsc.exe

Using M$.JScript; fails.

I have VS2008


Sonnich
 
A

Arne Vajhøj

Using M$.JScript; fails.

Oh - and the spelling M$ was pretty funny 15 years ago, but
today it just send out signals about not being professional.

Arne
 
J

jodleren

Oh - and the spelling M$ was pretty funny 15 years ago, but
today it just send out signals about not being professional.

Arne

Well it is also faster than writing Microsoft. Anyway, that is the
situaiton.
And I am professional enough to look to a solution. I am looking into
the present Delphi code to see how it hooks up with Scripting.

S
 
J

jodleren

But it is not faster than writing ms.


Did you try adding the ref?

Arne

Well it works now, but.... I like the error :)


Error 1 Argument '2' must be passed with the 'ref' keyword C:\stuff
\.NET\trunk\GripperStend\MainForm.cs 314 60 GripperStend
Error 2 The best overloaded method match for
'MSScriptControl.ScriptControlClass.Run(string, params object[])' has
some invalid arguments C:\stuff\.NET\trunk\GripperStend\MainForm.cs
314 21 GripperStend
Error 3 Argument '2' should not be passed with the 'ref' keyword C:
\stuff\.NET\trunk\GripperStend\MainForm.cs 314 60 GripperStend
Warning 4 The variable 'Result' is assigned but its value is never
used C:\stuff\.NET\trunk\GripperStend\GripMem.cs 100 20 GripperStend


Error1 wants it with REF
Error3 wants it without REF

The line in question: Run("....", (ref) xParms);

Now, how do I add it with and without REF?
I tried to remove all references, and start all over - before it wants
both, it asked for either one, then when changed, the other one. Can
it make up its mind?

The code:
private void button1_Click(object sender, EventArgs e)
{
object xRslt;
object[] xParms = new object[] { null };

MSScriptControl.ScriptControlClass myScriptingEngine;
myScriptingEngine = new
MSScriptControl.ScriptControlClass();
myScriptingEngine.AllowUI = false;
myScriptingEngine.Language = "JScript";
myScriptingEngine.Timeout = 3000;
myScriptingEngine.UseSafeSubset = false;
myScriptingEngine.Reset();
myScriptingEngine.AddObject("x", System.DBNull.Value,
true);
//myScriptingEngine.AddCode(" function test()
{ alert('y'); }");
myScriptingEngine.Eval("test");
//myScriptingEngine.Run(
xRslt = myScriptingEngine.Run("alert(5);", ref xParms);
// xRslt = myScriptingEngine.Run( "test"); //, xParms);
}
 
A

Arne Vajhøj

But it is not faster than writing ms.


Did you try adding the ref?

Well it works now, but.... I like the error :)


Error 1 Argument '2' must be passed with the 'ref' keyword C:\stuff
\.NET\trunk\GripperStend\MainForm.cs 314 60 GripperStend
Error 2 The best overloaded method match for
'MSScriptControl.ScriptControlClass.Run(string, params object[])' has
some invalid arguments C:\stuff\.NET\trunk\GripperStend\MainForm.cs
314 21 GripperStend
Error 3 Argument '2' should not be passed with the 'ref' keyword C:
\stuff\.NET\trunk\GripperStend\MainForm.cs 314 60 GripperStend
Warning 4 The variable 'Result' is assigned but its value is never
used C:\stuff\.NET\trunk\GripperStend\GripMem.cs 100 20 GripperStend


Error1 wants it with REF
Error3 wants it without REF

The line in question: Run("....", (ref) xParms);

Now, how do I add it with and without REF?
I tried to remove all references, and start all over - before it wants
both, it asked for either one, then when changed, the other one. Can
it make up its mind?

The code:
private void button1_Click(object sender, EventArgs e)
{
object xRslt;
object[] xParms = new object[] { null };

MSScriptControl.ScriptControlClass myScriptingEngine;
myScriptingEngine = new
MSScriptControl.ScriptControlClass();
myScriptingEngine.AllowUI = false;
myScriptingEngine.Language = "JScript";
myScriptingEngine.Timeout = 3000;
myScriptingEngine.UseSafeSubset = false;
myScriptingEngine.Reset();
myScriptingEngine.AddObject("x", System.DBNull.Value,
true);
//myScriptingEngine.AddCode(" function test()
{ alert('y'); }");
myScriptingEngine.Eval("test");
//myScriptingEngine.Run(
xRslt = myScriptingEngine.Run("alert(5);", ref xParms);
// xRslt = myScriptingEngine.Run( "test"); //, xParms);
}

That is using the JScript COM stuff.

I think you will be better of using JScript.NET!

Arne
 
I

ib.dangelmeyr

On 13-02-2011 12:58, jodleren wrote:
But it is not faster than writing ms.
Did you try adding the ref?

Well it works now, but.... I like the error :)

Error   1       Argument '2' must be passed with the 'ref' keyword      C:\stuff
\.NET\trunk\GripperStend\MainForm.cs    314     60      GripperStend
Error   2       The best overloaded method match for
'MSScriptControl.ScriptControlClass.Run(string, params object[])' has
some invalid arguments  C:\stuff\.NET\trunk\GripperStend\MainForm.cs
314     21      GripperStend
Error   3       Argument '2' should not be passed with the 'ref' keyword        C:
\stuff\.NET\trunk\GripperStend\MainForm.cs      314     60      GripperStend
Warning 4       The variable 'Result' is assigned but its value is never
used    C:\stuff\.NET\trunk\GripperStend\GripMem.cs     100     20      GripperStend

Error1 wants it with REF
Error3 wants it without REF

The line in question:    Run("....", (ref) xParms);

Now, how do I add it with and without REF?
I tried to remove all references, and start all over - before it wants
both, it asked for either one, then when changed, the other one. Can
it make up its mind?

The code:
 private void button1_Click(object sender, EventArgs e)
        {
            object xRslt;
            object[] xParms = new object[] { null };

            MSScriptControl.ScriptControlClass myScriptingEngine;
            myScriptingEngine = new
MSScriptControl.ScriptControlClass();
            myScriptingEngine.AllowUI = false;
            myScriptingEngine.Language = "JScript";
            myScriptingEngine.Timeout = 3000;
            myScriptingEngine.UseSafeSubset = false;
            myScriptingEngine.Reset();
            myScriptingEngine.AddObject("x", System.DBNull.Value,
true);
            //myScriptingEngine.AddCode(" function test()
{ alert('y'); }");
            myScriptingEngine.Eval("test");
            //myScriptingEngine.Run(
            xRslt = myScriptingEngine.Run("alert(5);", ref xParms);
//            xRslt = myScriptingEngine.Run( "test"); //, xParms);
        }

You're already close.

Try

public class foo
{
public void alert(string x)
{
MessageBox.Show(x, "bla");
}
}

[STAThread]
public static void Main()
{
MSScriptControl.ScriptControlClass js = new
MSScriptControl.ScriptControlClass();
js.AllowUI = false;
js.Language = "JScript";
js.Timeout = 0;
js.UseSafeSubset = false;
js.Reset();
js.AddObject("host", new foo(), true);

js.AddCode(@"
function bla(x)
{
host.alert(""Test"");
host.alert( x.toString() );
return 5;
}
");

object[] x = new object[1];
x[0] = "Foo-Test";
int rc = (int)js.Run("bla", ref x);
Console.WriteLine(rc);
}


Btw. window.alert() is a "host object" of internet explorer. So if you
host your own script manager, it's not available. You have to support
your own host object.
 
J

jodleren

You're already close.

Try

public class foo
{
public void alert(string x)
{
MessageBox.Show(x, "bla");
}
}

[STAThread]
public static void Main()
{
MSScriptControl.ScriptControlClass js = new
MSScriptControl.ScriptControlClass();
js.AllowUI = false;
js.Language = "JScript";
js.Timeout = 0;
js.UseSafeSubset = false;
js.Reset();
js.AddObject("host", new foo(), true);

js.AddCode(@"
function bla(x)
{
host.alert(""Test"");
host.alert( x.toString() );
return 5;}

");

object[] x = new object[1];
x[0] = "Foo-Test";
int rc = (int)js.Run("bla", ref x);
Console.WriteLine(rc);
}

Btw. window.alert() is a "host object" of internet explorer. So if you
host your own script manager, it's not available. You have to support
your own host object.

Thanks. You even reply to my next question for how we add our own
additions to the script.

I am really close, but it still crashes as AddObject and it still
wants it both with and without the REF in .RUN(name, objectlist)
The AddObject results in {"Specified cast is not valid."}

Error 1 The best overloaded method match for
'MSScriptControl.ScriptControlClass.Run(string, params object[])' has
some invalid arguments MainForm.cs 347 27 GripperStend
Error 2 Argument '2' should not be passed with the 'ref' keyword
MainForm.cs 347 45 GripperStend

Yesterday I had 3 errors, I needed both ref and should not use it.
Either way it does not work, simple crashes

js._AboutBox tells me that I have version 1, 2006.

This is really starting to p... me off.

Sonnich
 
I

ib.dangelmeyr

I am really close, but it still crashes as AddObject and it still
wants it both with and without the REF in .RUN(name, objectlist)
The AddObject results in {"Specified cast is not valid."}

Error   1       The best overloaded method match for
'MSScriptControl.ScriptControlClass.Run(string, params object[])' has
some invalid arguments  MainForm.cs     347     27      GripperStend
Error   2       Argument '2' should not be passed with the 'ref' keyword
MainForm.cs     347     45      GripperStend

Yesterday I had 3 errors, I needed both ref and should not use it.
Either way it does not work, simple crashes

js._AboutBox tells me that I have version 1, 2006.

This is really starting to p... me off.

Sonnich

What's the problem???
The Run-Method takes two parameters,
1. the string of the function to execute
2. an array of objects as parameters passed with the ref keyword

E.g. calling a function "func" with two paramenters, a string and an
integer:

object[] x = new object[2];
x[0] = "Test";
x[1] = 42;
scriptEngine.Run("func", ref x);


And if that still don't work for you (which should) there's still the
possibility to call the function by "ExecuteStatement", e.g.

scriptEngine.ExecuteStatement( "func();" );
 
J

jodleren

I am really close, but it still crashes as AddObject and it still
wants it both with and without the REF in .RUN(name, objectlist)
The AddObject results in {"Specified cast is not valid."}
Error   1       The best overloaded method match for
'MSScriptControl.ScriptControlClass.Run(string, params object[])' has
some invalid arguments  MainForm.cs     347     27      GripperStend
Error   2       Argument '2' should not be passed with the 'ref' keyword
MainForm.cs     347     45      GripperStend
Yesterday I had 3 errors, I needed both ref and should not use it.
Either way it does not work, simple crashes
js._AboutBox tells me that I have version 1, 2006.
This is really starting to p... me off.

What's the problem???
The Run-Method takes two parameters,
1. the string of the function to execute
2. an array of objects as parameters passed with the ref keyword

E.g. calling a function "func" with two paramenters, a string and an
integer:

object[] x = new object[2];
x[0] = "Test";
x[1] = 42;
scriptEngine.Run("func", ref x);

And if that still don't work for you (which should) there's still the
possibility to call the function by "ExecuteStatement", e.g.

scriptEngine.ExecuteStatement( "func();" );

The problem is, that when I try to compile it with REF, then it marks
it as red, stating that it should be without. Removing REF it works
for now, compiles, but states when running that it should be with REF.
Anyway, it crashes at AddObject, and the ExecuteStatement also crashes
as all time.

Sonnich
 
I

ib.dangelmeyr

The problem is, that when I try to compile it with REF, then it marks
it as red, stating that it should be without. Removing REF it works
for now, compiles, but states when running that it should be with REF.
Anyway, it crashes at AddObject, and the ExecuteStatement also crashes
as all time.

Sonnich

Here's your example from above again, slightly modified, which works
for me (no compile error and no "crash"):

object xRslt;
object[] xParms = new object[] { null };

MSScriptControl.ScriptControlClass myScriptingEngine;
myScriptingEngine = new
MSScriptControl.ScriptControlClass();
myScriptingEngine.AllowUI = false;
myScriptingEngine.Language = "JScript";
myScriptingEngine.Timeout = 3000;
myScriptingEngine.UseSafeSubset = false;
myScriptingEngine.Reset();

myScriptingEngine.AddObject("x", System.DBNull.Value,
true);

myScriptingEngine.AddCode(" function test() { return
42; }");

//myScriptingEngine.Eval("test");
//myScriptingEngine.Run(
//xRslt = myScriptingEngine.Run("alert(5);", ref xParms);

xRslt = myScriptingEngine.Run( "test", ref xParms);

Console.WriteLine( (int)xRslt );

Remember: if you don't have a error callback and there's some script
error (compile or runtime) than there will be an exception which will
terminate your program. Maybe you see that as a "crash".

If your still having problems show (as Peter Duniho always says) a
small but complete example of your code which we can compile/run to
debug the problem.
 
J

jodleren

The problem is, that when I try to compile it with REF, then it marks
it as red, stating that it should be without. Removing REF it works
for now, compiles, but states when running that it should be with REF.
Anyway, it crashes at AddObject, and the ExecuteStatement also crashes
as all time.

Here's your example from above again, slightly modified, which works
for me (no compile error and no "crash"):

            object xRslt;
            object[] xParms = new object[] { null };

            MSScriptControl.ScriptControlClass myScriptingEngine;
            myScriptingEngine = new
MSScriptControl.ScriptControlClass();
            myScriptingEngine.AllowUI = false;
            myScriptingEngine.Language = "JScript";
            myScriptingEngine.Timeout = 3000;
            myScriptingEngine.UseSafeSubset = false;
            myScriptingEngine.Reset();

            myScriptingEngine.AddObject("x", System.DBNull.Value,
true);

            myScriptingEngine.AddCode(" function test() { return
42; }");

            //myScriptingEngine.Eval("test");
            //myScriptingEngine.Run(
            //xRslt = myScriptingEngine.Run("alert(5);", ref xParms);

            xRslt = myScriptingEngine.Run( "test", ref xParms);

            Console.WriteLine( (int)xRslt );

Remember: if you don't have a error callback and there's some script
error (compile or runtime) than there will be an exception which will
terminate your program. Maybe you see that as a "crash".

If your still having problems show (as Peter Duniho always says) a
small but complete example of your code which we can compile/run to
debug the problem.

This works. Still, I get an error that I should do it without REF, but
it works. (compiles and works, but there are errors)

What I meant by crashes was an unhandled casting.

And I still face that problem - as I need to copy some old
functionality, a java-extension.
It get an error at AddObject

WBR
Sonnich
 
K

kndg

Hi all

Somehow e.g. InternetExplorer uses the JavaScript plugin - any ideas
how to do that from a C# programme?

The idea is to utilize JS in the software, so we can add a number of
functions depeding on the job etc.
No, I do not want to have it in the code, as it is NOT a part of the
application itself, it is the application which takes use of JS. As of
now I am happy so see alert('Hello world'); work.

I also need to add a plugin onto JS, so we can add a few instructions
there. Mainly IO and some sort of delay to wait for IO.

The script will be client and product dependent.

We have this solution already in Delphi, and need to move to C#. The
standard is given, so there is not much I can change. The system it
already in use.

Any help apriciated

WBR
Sonnich

There are a lot of javascript libraries available for .Net.
Below are some of them,

Jint : http://jint.codeplex.com/
Javascript.Net : http://javascriptdotnet.codeplex.com/

Have you tried any of those?
 

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