Passing managed objects to unmanaged methods as method argument

It is well documented how to expose a .net class as a COM object through the COM callable wrapper. However, it is not well documented how to pass a .net object to a COM method in an argument and have the COM method treat the .net object as COM. It turns out all we need to do is to add the [ComVisible(true)] to the classes that we need to pass to COM method; runtime would recognize that we are passing the object to COM and generate the COM callable wrapper automatically.

However, there are some impedance mismatches between COM and .NET. For example, .NET supports method overloading while COM does not. COM objects often uses optional arguments so that the client can pass variable number of arguments to the server. I used the following pattern to expose the Assert object to unmanaged VBScript engine in my VBScriptTest project in ASP Classic Compiler:

        [ComVisible(false)]
        public void AreEqual(object expected, object actual)
        {
            Assert.AreEqual(expected, actual);
        }
        [ComVisible(false)]
        public void AreEqual(object expected, object actual, string message)
        {
            Assert.AreEqual(expected, actual, message);
        }
        [ComVisible(true)]
        public void AreEqual(object expected, object actual, [Optional]object message)
        {
            if (message == null || message == System.Type.Missing)
                AreEqual(expected, actual);
            else
                AreEqual(expected, actual, message.ToString());
        }

Note that I marked the first two overloaded methods with [ComVisible(false)] so that they are only visible to other .net objects. Next, I created a COM callable method with an optional parameter. The optional parameter must have the object type; runtime will supply the System.Type.Missing object if a parameter is not supplied by the called. So in the body of the method, I just need to check whether the parameter equals to System.Type.Missing to determine whether the parameter is passed.

No Comments