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!
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!
Share Improve this question asked Jul 12, 2010 at 21:01 shaikshaik 311 gold badge1 silver badge5 bronze badges 5- 1 If you mean JScript, which is a MS' dialect of ECMAScript, it support ActiveX objects. – Georg Fritzsche 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. – shaik 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. – Georg Fritzsche 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. – David Commented Mar 31, 2015 at 21:40
- 1 Also, testing a COM ponent 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. – David Commented Mar 31, 2015 at 21:42
3 Answers
Reset to default 2Use 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 plicated 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
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 acplish 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 ponent 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./en-us/dotnet/csharp/programming-guide/interop/example--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.
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