473,395 Members | 1,516 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Hosting Javascript in .NET Windows Form App? (Somewhat Advanced)

All:

I am writing a .NET 1.1 Windows Forms app in C#. I would like to offer
users the ability to script the application via javascript.

In the "old days," I would have used the Active (ActiveX) Scripting
host (COM based) offered by Microsoft. Is this still the best way to do
it?

If this is not the way to do it, what is the new way in .NET?

In either case, where would I get a javascript engine to host?

Many thanks,
John

Nov 17 '05 #1
5 2570
I think you still need to use the Active Scripting Host for javascript. On
the other hand, you can use the CodeDOM and support C#, J#, or Visual Basic
for your scripting instead.

Pete
"John Puopolo" <jp******@mvisiontechnology.com> wrote in message
news:11*********************@g43g2000cwa.googlegro ups.com...
All:

I am writing a .NET 1.1 Windows Forms app in C#. I would like to offer
users the ability to script the application via javascript.

In the "old days," I would have used the Active (ActiveX) Scripting
host (COM based) offered by Microsoft. Is this still the best way to do
it?

If this is not the way to do it, what is the new way in .NET?

In either case, where would I get a javascript engine to host?

Many thanks,
John

Nov 17 '05 #2
Look into the ICodeCompiler interface. You can dynamically build and
run C# code in your application at run-time.

Here's a real quick-and-dirty sample:

void DoSomethingInAWindowsApp()
{
ICodeCompiler ic = new
CSharpCodeProvider().CreateCompiler();
CompilerResults cr = ic.CompileAssemblyFromSource(new
CompilerParameters(), @"
namespace Test
{
class DynamaCompile
{
public static int Add (int x, int y)
{
return x + y;
}
}
}");

MessageBox.Show(cr.CompiledAssembly.GetType("Test. DynamaCompile").InvokeMember("Add",
System.Reflection.BindingFlags.InvokeMethod, null, null, new
object[]{3, 4}).ToString());
}

-Alan

Nov 17 '05 #3
Thanks Alan, etc.

I like the idea of ICodeCompiler, etc., but what I need is something
that enables for script debugging, etc. I envision a mini-editor where
people can enter scripts and control my program (via a set of objects
the program exposes, of course). Suppose, in your example, the code
snippet contains a syntax error - or a more subtle error. Things like
this crop up all the time in development, so (unfortunately!) this
solution will not work completely for my situation - but thank you for
taking the time to put together the example.

John


Alan Samet wrote:
Look into the ICodeCompiler interface. You can dynamically build and
run C# code in your application at run-time.

Here's a real quick-and-dirty sample:

void DoSomethingInAWindowsApp()
{
ICodeCompiler ic = new
CSharpCodeProvider().CreateCompiler();
CompilerResults cr = ic.CompileAssemblyFromSource(new
CompilerParameters(), @"
namespace Test
{
class DynamaCompile
{
public static int Add (int x, int y)
{
return x + y;
}
}
}");

MessageBox.Show(cr.CompiledAssembly.GetType("Test. DynamaCompile").InvokeMember("Add",
System.Reflection.BindingFlags.InvokeMethod, null, null, new
object[]{3, 4}).ToString());
}

-Alan


Nov 17 '05 #4
The CompileAssemblyFromSource function takes a "CompilerParameters" option,
where you can tell it to include debugging information. Unfortunately, I am
not aware of an easy way to make use of the debugging information when
actually running the script. It is probably worth digging around, though.

The CompileAssemblyFromSource routine returns a CompilerResults that gives
you all the error messages and stuff you might need to correct syntax errors.
It is pretty well done. You can supply this information directly to your
user, or you can digest it yourself (it includes line/column numbers) and
present him/her with an error list.

"John Puopolo" wrote:
Thanks Alan, etc.

I like the idea of ICodeCompiler, etc., but what I need is something
that enables for script debugging, etc. I envision a mini-editor where


Nov 17 '05 #5
I've just gone down this road myself. Use the ICodeCompiler stuff as
mentioned before, and look into "mdbg", a managed debugger sample w/source
provided on MSDN. I have been able to fully implement scripting in any .NET
available language (except C++ - a long story...) and have a debugger built
into my app for debugging scripts. Its a bit rough - little documentation -
but it does work. Also search for blog posts from Mike Stall - he has lots of
information on managed debugging.

Brian.

"John Puopolo" wrote:
Thanks Alan, etc.

I like the idea of ICodeCompiler, etc., but what I need is something
that enables for script debugging, etc. I envision a mini-editor where
people can enter scripts and control my program (via a set of objects
the program exposes, of course). Suppose, in your example, the code
snippet contains a syntax error - or a more subtle error. Things like
this crop up all the time in development, so (unfortunately!) this
solution will not work completely for my situation - but thank you for
taking the time to put together the example.

John


Nov 17 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Gowhera Hussain | last post by:
Use This for Learning Only .... Do Not Try To Act Smart HACKING WITH JAVASCRIPT Dr_aMado Sun, 11 Apr 2004 16:40:13 UTC This tutorial is an overview of how javascript can be used to bypass...
53
by: Cardman | last post by:
Greetings, I am trying to solve a problem that has been inflicting my self created Order Forms for a long time, where the problem is that as I cannot reproduce this error myself, then it is...
15
by: binnyva | last post by:
Hello Everyone, I have just compleated a JavaScript tutorial and publishing the draft(or the beta version, as I like to call it) for review. This is not open to public yet. The Tutorial is...
20
by: Dr. Colombes | last post by:
For a personal Web site with modest throughput and interactivity demans, I'm interested in your recommendation(s) for good (cheap, reliable, Linux friendly, very little, if any, sponsor...
12
by: pantagruel | last post by:
Hi, I'm thinking of making a WScript based JavaScript library, I can think of some specific non-browser specific scripting examples that should probably make it in, like Crockford's little...
7
by: keydrive | last post by:
I am trying to save a users selection when toggle a hidden field between forms. So far, the toggle works great but the action between forms does not. I could use some assistance with my js. thanks...
93
by: lorlarz | last post by:
Here, let's see what you JavaScript programmers have got Here is a place to share your large or larger full-blown JavaScript applications: real.comp.lang.js.apps (a new google group) Here is...
0
by: ablesteve | last post by:
Looking for full DVD video tutoring series on C++. Somewhat basic to advanced windows and even some Web work. Please send url to my E-mail steve1va@yahoo.com All is greatly appreciated and will...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.