Consider the automation compatible COM library in C # below. This follows the general COM pattern for having a visible factory coclass FooFactory that implements ICreateFoos, which creates an object of type IFoo. FooFactory is the only class in the type library. (The factory pattern is especially useful for COM because it does not allow parameterized constructors).
In the code below, I found that I cannot access the returned IFoo interface from jscript unless I make the FooImpl class ComVisible (by uncommenting the commented lines, which causes it to appear as a class in the type library). There is no such problem to access this from VBscript.
That is, I can run this VBScript:
set ff = CreateObject("jstest.FooFactory")
set foo = ff.CreateFoo(0)
foo.Foo
But this functionally identical JScript crashes with the error "C: \ temp \ jstest \ jstest.js (4, 1) Microsoft JScript runtime error:" foo "is null or not an object":
var ff = new ActiveXObject("jstest.FooFactory");
var foo = ff.CreateFoo(0)
foo.Foo();
If I uncomment the line, I see that null == foo is false.
Why is this happening? This is mistake? Please note that I think this is a problem, this is a combination of JScript and C # /. NET-specific implementation (possibly IDispatch), because I have other similar COM servers implemented in C ++ that do not exhibit this problem from JScript.
, , FooImpl coclass, , . , -, , FooImpl ComVisible, , , CoCreate, .
WinXP SP3 Visual Studio 2005,.net 2 TinyXP VirtualBox ( Windows Script Host 5.7), Windows 7 Ultimate .net SDK 2.0, 3.0, 3.5 4.0 (WSH 5.8). 32-.
:
using System;
using System.Runtime.InteropServices;
[assembly: ComVisible(false)]
namespace jstest
{
[ComVisible(true)]
public interface ICreateFoos
{
IFoo CreateFoo(int importantNumber);
}
[ComVisible(true)]
public interface IFoo
{
void Foo();
}
[ComVisible(true)]
public class FooFactory : ICreateFoos
{
public IFoo CreateFoo(int importantNumber)
{
return new FooImpl();
}
}
public class FooImpl : IFoo
{
public void Foo()
{
Console.WriteLine("Foo");
}
}
}
(, admin regasm)
csc /target:library jstest.cs
regasm /codebase jstest.dll