Calling C# COM object from JavaScript

J

Justin30324

Hello. I have a COM object written in C#. I am creating an instance of
it in JavaScript.

I instantiate the COM object in JavaScript like this:
var myObject = new ActiveXObject("NamespaceName.ClassName");

I have the following function in the C# COM object:
public string CallMe()
{
return "Hello world";
}

I call the function from JavaScript like this:
var strFoo = myObject.CallMe();
strFoo is successfully set to Hello world. (This is good.)

However, what I really want is to pass the variable by reference so I
can return more than one value from the COM object function. This is
the function I cannot get to work:
public void CallMe2(out string strInput1, out string strInput2)
{
strInput1 = "Hello";
strInput2 = "world";
}

I call the fuction from JavaScript like this:
var strResult1 = "xxx";
var strResult2 = "yyy";
myObject.CallMe(strResult1, strResult2);
After the function has returned, strResult1 and strResult2 have not
been changed.

I can call CallMe2 from a C++ program (I pass variants instead of
strings), and the strings are changed to Hello and world. But, the
strings are not changed when I call the function from Java Script. Is
there a way to do this?

Thanks in advance.

Justin
 
N

Nicholas Paldino [.NET/C# MVP]

Justin,

It's simple, really. JavaScript does not support the passing of
parameters by reference. You can't do this.

That doesn't mean that you can't pass an object in and have it's
properties set by the function being called (you can set the properties of
an object passed in).
 
L

LVP

Nicholas,

Can you expand/clarify on this please?
You pass an Object to COM. Does this mean JavaScript and COM are pointing
to the same Object? if so then referenced.
Or are you saying you pass an object, COM populates its properties then
passes the Object back with data to JavaScript?

Thank You,

LVP


Nicholas Paldino said:
Justin,

It's simple, really. JavaScript does not support the passing of
parameters by reference. You can't do this.

That doesn't mean that you can't pass an object in and have it's
properties set by the function being called (you can set the properties of
an object passed in).

--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Hello. I have a COM object written in C#. I am creating an instance of
it in JavaScript.

I instantiate the COM object in JavaScript like this:
var myObject = new ActiveXObject("NamespaceName.ClassName");

I have the following function in the C# COM object:
public string CallMe()
{
return "Hello world";
}

I call the function from JavaScript like this:
var strFoo = myObject.CallMe();
strFoo is successfully set to Hello world. (This is good.)

However, what I really want is to pass the variable by reference so I
can return more than one value from the COM object function. This is
the function I cannot get to work:
public void CallMe2(out string strInput1, out string strInput2)
{
strInput1 = "Hello";
strInput2 = "world";
}

I call the fuction from JavaScript like this:
var strResult1 = "xxx";
var strResult2 = "yyy";
myObject.CallMe(strResult1, strResult2);
After the function has returned, strResult1 and strResult2 have not
been changed.

I can call CallMe2 from a C++ program (I pass variants instead of
strings), and the strings are changed to Hello and world. But, the
strings are not changed when I call the function from Java Script. Is
there a way to do this?

Thanks in advance.

Justin
 
N

Nicholas Paldino [.NET/C# MVP]

This has nothing to do with COM.

JavaScript as a language does not support passing parameters by
reference, so you can't pass two variables to a method exposed in JavaScript
(through any means) and have them changed by the method itself.

So, you should create a different type, which exposes the properties
that you want to set in your method, and pass THAT to your method.

So, you create the object in JavaScript, pass that object to the method,
the COM object then sets the properties on the object passed in and then the
JavaScript code accesses the properties on the object.

--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

LVP said:
Nicholas,

Can you expand/clarify on this please?
You pass an Object to COM. Does this mean JavaScript and COM are pointing
to the same Object? if so then referenced.
Or are you saying you pass an object, COM populates its properties then
passes the Object back with data to JavaScript?

Thank You,

LVP


Nicholas Paldino said:
Justin,

It's simple, really. JavaScript does not support the passing of
parameters by reference. You can't do this.

That doesn't mean that you can't pass an object in and have it's
properties set by the function being called (you can set the properties
of an object passed in).

--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Hello. I have a COM object written in C#. I am creating an instance of
it in JavaScript.

I instantiate the COM object in JavaScript like this:
var myObject = new ActiveXObject("NamespaceName.ClassName");

I have the following function in the C# COM object:
public string CallMe()
{
return "Hello world";
}

I call the function from JavaScript like this:
var strFoo = myObject.CallMe();
strFoo is successfully set to Hello world. (This is good.)

However, what I really want is to pass the variable by reference so I
can return more than one value from the COM object function. This is
the function I cannot get to work:
public void CallMe2(out string strInput1, out string strInput2)
{
strInput1 = "Hello";
strInput2 = "world";
}

I call the fuction from JavaScript like this:
var strResult1 = "xxx";
var strResult2 = "yyy";
myObject.CallMe(strResult1, strResult2);
After the function has returned, strResult1 and strResult2 have not
been changed.

I can call CallMe2 from a C++ program (I pass variants instead of
strings), and the strings are changed to Hello and world. But, the
strings are not changed when I call the function from Java Script. Is
there a way to do this?

Thanks in advance.

Justin
 
J

Justin30324

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
 
N

Nicholas Paldino [.NET/C# MVP]

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.
 
L

LVP

Nicholas,

This means when you pass the JavaObject to the COM Object both the COM
Object and JavaScript have the same Reference to the JavaObject passed.
No Reference Parameters.

Thanks for you reply.

LVP

Nicholas Paldino said:
This has nothing to do with COM.

JavaScript as a language does not support passing parameters by
reference, so you can't pass two variables to a method exposed in
JavaScript (through any means) and have them changed by the method itself.

So, you should create a different type, which exposes the properties
that you want to set in your method, and pass THAT to your method.

So, you create the object in JavaScript, pass that object to the
method, the COM object then sets the properties on the object passed in
and then the JavaScript code accesses the properties on the object.

--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

LVP said:
Nicholas,

Can you expand/clarify on this please?
You pass an Object to COM. Does this mean JavaScript and COM are
pointing to the same Object? if so then referenced.
Or are you saying you pass an object, COM populates its properties then
passes the Object back with data to JavaScript?

Thank You,

LVP


Nicholas Paldino said:
Justin,

It's simple, really. JavaScript does not support the passing of
parameters by reference. You can't do this.

That doesn't mean that you can't pass an object in and have it's
properties set by the function being called (you can set the properties
of an object passed in).

--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Hello. I have a COM object written in C#. I am creating an instance of
it in JavaScript.

I instantiate the COM object in JavaScript like this:
var myObject = new ActiveXObject("NamespaceName.ClassName");

I have the following function in the C# COM object:
public string CallMe()
{
return "Hello world";
}

I call the function from JavaScript like this:
var strFoo = myObject.CallMe();
strFoo is successfully set to Hello world. (This is good.)

However, what I really want is to pass the variable by reference so I
can return more than one value from the COM object function. This is
the function I cannot get to work:
public void CallMe2(out string strInput1, out string strInput2)
{
strInput1 = "Hello";
strInput2 = "world";
}

I call the fuction from JavaScript like this:
var strResult1 = "xxx";
var strResult2 = "yyy";
myObject.CallMe(strResult1, strResult2);
After the function has returned, strResult1 and strResult2 have not
been changed.

I can call CallMe2 from a C++ program (I pass variants instead of
strings), and the strings are changed to Hello and world. But, the
strings are not changed when I call the function from Java Script. Is
there a way to do this?

Thanks in advance.

Justin
 

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