views:

191

answers:

1

Hello,

I'm creating a COM callable .net assembly and now trying to use it from legacy COM clients (VB6 client in my case).

Assembly should expose API style interface, so typical function declaration would look like this:

int myRoutine (object inParam, out object result);

The problem is when I trying to use function declared as:

int GetMultipleItems (out ItemData[] itemList);

In VB6 this translated to function that have array to be passed as parameter which fails when I'm calling it with 'Invalid procedure call or argument'.

Actual call looks like:

Dim items() As ItemData  
result = SCServer.GetMultipleItems (items)

Investigated further, I tried several different ways of marking up my library with MarshalAs attributes. From my point of view, the problem is argument has to be array to be passed in and - on the other hand - a variant to be returned to VB code.

After several experiments, I got following to work (1):

int GetMultipleItems ([Out, MarshalAs (UnmanagedType.SafeArray,
    SafeArraySubType = VarEnum.VT_DISPATCH)]out object[] itemList);

(having items() declared as Object at the client side).

But I have to use exactly my initial signature (2),

int GetMultipleItems ([Out, MarshalAs (UnmanagedType.SafeArray,
    SafeArraySubType = VarEnum.VT_DISPATCH)]out ItemData[] itemList);

Which does not work either with Object or with ItemData array type declaration at client.

So, questions are:
- why (1) works while (2) not. What should I change in attribute declaration or in client to get to work with my actual type in signature (it exposes an interface which has been exported to tlb as well, so seems all should be OK here)
- what is recommended way to define sugh [out] parameter arrays
- maybe I lack some essential reading, I would be grateful for the links... still I need to get the sample working in a day or two.

Thanks in advance.

A: 

Could you change your declaraction so that you Return the Array of objects and the client can retrieve the count of them from what is returned.

ItemData[] GetMultipleItems()

in your method you could either return Null or an Empty array (length== 0) if there are none.

or Have your array returned and a variable defined with the Count.

ItemData[] GetMultipleItems(out int ItemsReturned)

WinAPI style declaractions aren't really the normal method of declaring methods etc in .NET APIs. (but i could be wrong of course).

Paul Farry
I'm considering this as option. Not mentioned in question - return value not really item count, it contains error for operation. Your suggestion still could be useful if there's no other way
Ilya Komakhin