Re: Calling C# COM object from JavaScript

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance



Justin,

You should expose the class through COM interop and then pass that to
your method. The reason for this is that the object that you are passing
from javascript is a makeshift implementation of IDispatch, which doesn't
have the same type as the object you created in .NET.

You could use reflection to set the properties of the javascript object
passed in, but that's hackney.

Expose the object in your .NET code as another COM object, and create an
instance of that in javascript and pass it into your method.


--
- Nicholas Paldino [.NET/C# MVP]
- mvp@xxxxxxxxxxxxxxxxxxxxxxxxxxx

<Justin30324@xxxxxxxxx> wrote in message
news:bf936f4f-de52-45ae-951b-25fbf0c7a905@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Nicholas,

Thank you very much for your answers. Here is what I'm doing in my
JavaScript now:

function strObject()
{
this.str1;
this.str2;
return this;
}

var myStrObject = new strObject();
myObject.CallMe2(myStrObject);

Now I'm passing a JavaScript object into the C# COM function. I should
be able to manipulate the properties of that object in the C#
function. I have a set up a class in C# to mimic the object in
JavaScript. (I know it's bad practice to have everything public like
this - I'm just trying to get it working.)

Here is the C# code:

public class MyStringClass
{
public string str1;
public string str2;
}

And here is the new function exposed in the COM object:

public void CallMe2(MyStringClass mySC)
{
mySC.str1 = "Hello";
mySC.str2 = "world";
}

When I make the JavaScript call to the C# COM function (see above), it
hits the error handler in JavaScript. The number of the error is
-2146827858. The error description is:
Class doesn't support Automation.

What am I doing wrong? Thanks again.

Justin


.



Relevant Pages

  • Re: Javascript and Microsoft Windows
    ... If we assume that there is a JavaScript program running in the web-browser, ... Great exactly how do they expose this, ...
    (comp.lang.javascript)
  • Re: Calling a Javascript function from CSharp class.
    ... We have a javascript file which has some ... You expose a function that you ... I assume you know about the RegEx support in C#? ...
    (microsoft.public.dotnet.languages.csharp)
  • accessing bho from javascript
    ... i want to create a bho that i can access from javascript on my page. ... want to expose a few async events in the bho that i can respond to with ...
    (microsoft.public.inetsdk.programming.webbrowser_ctl)
  • Re: How to retreive window.showModalDialog dialogArguments in C# code?
    ... You would have to force a postback to the page (or use javascript to ... make an XML request under the covers) passing that value to your C# page. ... > passing vArguments parameters in an array form using this syntax: ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Return a NET array to javascript (IE)
    ... > I would like to return a .NET array of strings to Javascript, ... > public string[] MyArray ... and then use the Java Script iterator ...
    (microsoft.public.dotnet.framework.interop)