2

I drive into this issue: I create COM object using C#, register it and managed to work with it using powershell. when i trying to do the same with JavaScript it fails, but javascript keeps throwing object null errors.

Do you have any advice on how to fix this problem? or maybe you JavaScript doesn't support COM (if so, where can i read more about it)???

Thanks a lot!

5
  • 1
    If you mean JScript, which is a MS' dialect of ECMAScript, it support ActiveX objects. Commented Jul 12, 2010 at 21:06
  • But COM object (such as i create) doesn't work? i use new ActiveXObject() but it keeps throwing object null. Commented Jul 12, 2010 at 21:09
  • While ActiveX is built on COM, it has some additional concepts/requirements. I don't know how this works from C#, but without showing what your exposed object is defined like others probably can't help you either. Commented Jul 12, 2010 at 21:10
  • Did you test the COM in VBScript first? Usually VBScript is the de facto (scripting language) consumer/client for COM, with other languages (JScript, Python, etc.) secondary. C/C++ is another good option though from a diff perspective as it's not scripting. If it don't work in VBScript or C/C++, it not likely going to work under JScript. Commented Mar 31, 2015 at 21:40
  • 1
    Also, testing a COM component created from C# using C# or Powershell isn't a good idea since they are all within .NET framework. You have to test COM outside of that to ensure the COM interop or the reg free COM is working correctly. So best to first test in VBScript or C/C++ if not JScript. Commented Mar 31, 2015 at 21:42

3 Answers 3

2

Use Shanti Rao's JSDB shell. It's based on the core Spidermonkey engine (Mozilla's Javascript implementation) used in Firefox, but has a bunch of bindings for databases & ActiveX objects and such. It has a few limitations but unless you're using something complicated you should be able to make use of it.

Example:

x=new ActiveX('MSXML2.DOMDocument.6.0');
x.async = false;
// I forget how to use IXMLDOMDocument but other calls go here
Sign up to request clarification or add additional context in comments.

3 Comments

shouldn't it be new ActiveXObject() instead of ActiveX()?
Ok, thanks. Interesting that this solution/implementation deviates from JScript for the instantiation part. I guess similar to how older versions of IE didn't support XmlHttpRequest() before it was popularized and standardized among browsers.
JSDB is not a browser. (and to be frank, JScript is not Javascript)
1

I know this is a bit late, but for others who find this, yes this can be done easily. This assumes you're running on Windows since you're looking for Windows/JavaScript interoperability.

The most important question is "what JavaScript engine are you using?" as this functionality is determined by that engine. Since 1995, Windows has supported a system standard scripting model originally called OLE Automation or sometimes just COM. Windows-based scripting engines like the JavaScript and VBScript engines built into the Windows Scripting Host use this engine, in addition to IE through version 8 and I think up to 11. However, the IE container implements security restrictions that prevent some of what I'm describing from working. Open-source JavaScript engines like node.js typically do not use COM as this is Windows specific functionality and so cannot do what I am describing.

Given that, to accomplish what you want, you must: 1. Implement a scriptable COM object. 2. Register that object (typically automatic during your build process). 3. In JavaScript, create an instance of that object using new ActiveX object, as mentioned above.

You can write your object in both C# and C++. In both cases, you need to base your object on IDispatch. C# will make the whole process considerably easier. Essentially, you generate a few unique GUIDs for your interface and component using guidgen, then using a few COM-specific attributes in C# to provide these. Here's a link to a great simple example (ignore the Events stuff): https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/interop/example-com-class

The most important thing to know is that you will be limited on what data types you can take as parameters or return to the caller. Things like strings and ints are no problem. For others, you can describe them in C# and send them from C# to JavaScript, but the other way around is not going to work.

Comments

1

Javascript indeed does not support COM. An option is to use JScript and an ActiveX wrapper to a COM object. Also, it will only work in Internet Explorer.

Instantiating a COM class
Calling functions of a COM object in JScript
Other JScript/COM tutorials, including script callbacks

2 Comments

So I see from your answer. Thanks for enlightening me.
Hard to tell from the OP's question on specifics of usage, but JScript COM/ActiveX is usable in Windows Scripting Host (WSH) as well as Active Server Pages (ASP) not strictly confined to Internet Explorer. Course browser specific JS code won't work under WSH and ASP though.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.