Accessing C# classes from IE (JScript)

Hi,

I'm looking for a way to access some C# from JScript. I tried to wrap the class with COM proxy by using some COM attributes (ClassInterface, InterfaceType ...).

When I try to instantiate the object from within a C++ program, I succeed.

when I try to declare the class as an object in HTML page and access it from
JScript, I do not succeed.

When I try to use the ActiveX Control Test Container, it would not succeed creating the object.

attached : a code sample of the C# class as it appears in MSDN tutorials.

using System;
using System.Runtime.InteropServices;
using System.Windows.Forms;
namespace CSharpServer
{
// Since the .NET Framework interface and coclass have to behave as
// COM objects, we have to give them guids.
[Guid("DBE0E8C4-1C61-41f3-B6A4-4E2F353D3D05")]
publicinterface IManagedInterface
{
int PrintHi(string name);
}

[Guid("C6659361-1625-4746-931C-36014B146679"),
ProgId("CSharpServer::InterfaceImplementation")]
publicclass InterfaceImplementation : IManagedInterface
{
publicint PrintHi(string name)
{
Console.WriteLine("Hello, {0}!", name);
return 1;
}
}
}

[2842 byte] By [MosheShitrit] at [2008-2-20]
# 1
Script requires that interfaces derive from IDispatch, so you should attribute your IManagedInterface with InterfaceTypeAttribute(ComInterfaceType.InterfaceIsIUnknown) or passComInterfaceType.InterfaceIsDual. You should also attribute your coclass with ClassInterfaceAttribute(ClassInterfaceType.None) so that the IManagedInterface is used as the class interface and one is not generated automatically. For more on these guidelines, read http://blogs.msdn.com/heaths/archive/2005/03/09/391358.aspx.

Now you have to instantiate the control in your page. You need to modify your Code Access Security for the Machine policy to create a code group that allows the control from its host URL (like http://www.mydomain.com/*) to run with the necessary permissions. Also be sure it doesn't use any SystemColor enumerations (by default most controls do, like the DataGrid).

I have a very old article written about this and how to even hook-up events to a control. This also covers the CAS modifications necessary to allow code from an otherwise partly- or un-trusted source. Read http://www.devhood.com/Tutorials/tutorial_details.aspx?tutorial_id=388.

HeathStewart at 2007-10-6 > top of Msdn Tech,.NET Development,JScript for the .NET Framework...

.NET Development

Site Classified