views:

235

answers:

2

All, I am trying to access a .NET dll registered as a COM Object with PowerBuilder 10. I keep running into problems where .NET objects return lists.

I have built out a very simple class for a proof of concept, and to better explain what I am encountering. See below:

.NET:

public class ListsArrays
{
    public int[] GetArray()
    {
        int[] array = new int[3];
        array[0] = 1;
        array[1] = 10;
        array[2] = 100;

        return array;
    }

    public List<int> GetList()
    {
        List<int> list = new List<int>();
        list.Add(3);
        list.Add(33);
        list.Add(333);
        return list;
    }

    public int[] GetListArray()
    {
        List<int> list = new List<int>();
        list.Add(3);
        list.Add(33);
        list.Add(333);
        return list.ToArray();
    }
}

PowerBuilder:

Integer ls_array[]
Integer obj_return, ID, netVal
String FullName
OleObject lo_dotnetobject
OleObject lo_Value

lo_Value = Create OleObject
lo_dotnetobject = Create OleObject

obj_return = lo_dotnetobject.ConnectToNewObject("XXX.ListsArrays")

//This is the tricky part
ls_array = lo_dotnetobject.GetArray()         // WORKS       (1)
ls_array = lo_dotnetobject.GetList().ToArray()  // DOES NOT WORK  (2)
lo_dotnetobject.GetList().CopyTo(ls_array)    // DOES NOT WORK (3)
ls_array = lo_dotnetobject.GetListArray()      // WORKS       (4)

For each part that “DOES NOT WORK” I receive this error message:

“Function/event with no return value used in expression at line”

Obviously, I could just wrap my .NET assembly, and return an array for every list, but I would like to be able to handle lists on the PowerBuilder side without wrapping the .NET. Does anyone have any ideas? The fact that (4) works and (2) doesn’t work drives me kind of nuts.

Thanks.

A: 

It's been more than 10 years since I worked with Powerbuilder, but I would say that List<int> would not work simply because it is a generic list which I don't think can be correctly mapped to a COM array - that would explain why 1 and 4 works but nothing else. Can you work with simple arrays instead of generic lists?

Otávio Décio
+1  A: 

I used to deal with this every day when exposing rich .NET APIs to COM. COM doesn't understand generics, but don't let that slow you down- the trick is to expose the generic list as a non-generic, com-visible base type that List<int> already exposes, like IList. Change your GetList return type to IList, rebuild/register, and make sure that Powerbuilder sees your fixed typelib, and all should be well.

nitzmahone