Custom constructor COM call

I have a DLL (written in C #) containing a class with 2 constructors; the default constructor (without arguments) and the other with three arguments.

In VBscript, I want to call the second constructor, but CreateObject only gets the classValue parameter classValue no possible arguments .

I assume that the base implementation of CreateObject uses the system function CoCreateObject , which according to this answer does not support arguments, but, on the other hand, QTP / UFT DotNetFactory , which is capable of such a thing, so there should be a way to do this in pure VBscript.

(I want to avoid the obvious solution to the init method, if possible).

Any ideas on how to call my constructor different from the standard?

+4
source share
1 answer

COM does not support passing arguments to the constructor. The main object of the factory method (IClassFactory :: CreateInstance) does not accept arguments.

The workaround is quite simple, all problems in software development can be solved by a different level of indirection :) Just create your own factory method. You can write one that takes the arguments needed by the constructor. Roughly speaking:

 [ComVisible(true)] public interface IFoo { //... } class Foo : IFoo { public Foo(int a, string b) { ... } //... } [ComVisible(true)] public class FooFactory { public IFoo CreateInstance(int a, string b) { return new Foo(a, b); } } 

And your VBScript can now call the FooFactory CreateInstance () method to create your class object. Otherwise, the very common model in COM object models is a very remarkable example of Microsoft Office automation.

+5
source

All Articles