COM and .NET: what's wrong with this code?

Can you spot the mistake in this code? Here we implement a CCW-based .NET object. This COM class is exposed as from a certain process (say, a service) to other processes in the system. We implement a simple get/set accessor that receives an array of objects:

[ComVisible(true), GuidAttribute("61b5d373-4258-40a2-89ac-2783d8ac0e99")]
class MyComObject
{
public object[] Filter
{
get { return objArray; }
set
{
Debug.Trace("SET: new array received: '{0}'" + objArray.Length);
objArray = value;
}
}

private object[] objArray = new object[0];
}

This COM object can be potentially called from another process B, for example from a VB script. For example, something like this:

Dim formatsArray(2)
formatsArray(1) = "abc"
formatsArray(2) = "def"

Set myObject = CreateObject( "TEST.MyComObject" )
myObject.Filter = formatsArray

Comments

  • Anonymous
    April 01, 2005
    No ProgId Attribute, and shouldn't you make a copy of the object array, incomming, I think the array is a variant array that needs some sort of conversion.
  • Anonymous
    April 01, 2005
    OK - let's assume that the COM object is correctly registered... :-)
  • Anonymous
    April 01, 2005
    The comment has been removed
  • Anonymous
    April 01, 2005
    Doh - you are right. Silly me, I should have come with a more complicated question :-)

    Anyway, the main point I want to emphasise is this: when marshalling top-level COM pointers as IN parameters in a routine (including a setter), then it is possible to put NULL as the parameter value. Probably not from VBScript, but certainly from C++.

    In the example above, a NULL value for the objArray safe array is nicely transmitted to the server, causing an AV in this case

    Note that when you have an IN/OUT or OUT parameter, the NULL value (of a pointer to a SAFEARRAY reference) is not sent over the wire. The COM marshalling will return an error to the client directly.