English 中文(简体)
.NET dll in PowerBuilder (as COM) problem with Lists
原标题:

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.

问题回答

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?

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.





相关问题
Manually implementing high performance algorithms in .NET

As a learning experience I recently tried implementing Quicksort with 3 way partitioning in C#. Apart from needing to add an extra range check on the left/right variables before the recursive call, ...

Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

How do I compare two decimals to 10 decimal places?

I m using decimal type (.net), and I want to see if two numbers are equal. But I only want to be accurate to 10 decimal places. For example take these three numbers. I want them all to be equal. 0....

Exception practices when creating a SynchronizationContext?

I m creating an STA version of the SynchronizationContext for use in Windows Workflow 4.0. I m wondering what to do about exceptions when Post-ing callbacks. The SynchronizationContext can be used ...

Show running instance in single instance application

I am building an application with C#. I managed to turn this into a single instance application by checking if the same process is already running. Process[] pname = Process.GetProcessesByName("...

How to combine DataTrigger and EventTrigger?

NOTE I have asked the related question (with an accepted answer): How to combine DataTrigger and Trigger? I think I need to combine an EventTrigger and a DataTrigger to achieve what I m after: when ...

热门标签