Click here to Skip to main content
14,428,551 members
Rate this:
Please Sign up or sign in to vote.
See more:
ok, i've got a callback from some C++ code to some C# code to handle an array of data, and a data structure.

In C++ the callback is declared as:
int MyCallBack(const MyStruct* pMyStruct,  // pointer to a struct
               unsigned short* pMyData,    // array of ushorts
               unsigned int nMyDataLength, // length of the previous array
               void* pInfo);               // some extra data (not relevant to the question)


I declare the delegate to handle this in C# as follows:
public delegate int MyCallBack(
    ref MyStruct myStruct,
    [MarshalAs(UnmanagedType.LPArray,   SizeParamIndex = 2)] ushort[] myData,
    uint myDataLength,
    IntPtr userInfoStruct);


This all works fine so far.
However now i want to pass multiple arrays of data back to the C# in a single call to avoid some of the native->managed overhead

My declaration in C++ for the multiple callbacks is as follows:

int MyCallBack2(const MyStruct* pMyStruct,          // array of structs
                unsigned short** pMyDataArray,      // array of ushort arrays
                unsigned int* nMyDataLengthArray,   // array of lengths 
                              // corresponding to each of the ushort arrays
                unsigned int dataCount,             // the count of elements in 
                                                    // the arrays above
                void* pInfo);                       // some extra data (not relevant to the question)



I'm having some trouble figuring out the declaration for the delegate for this callback in the C#

This is what I've got so far:
public delegate int MyCallBack2(
    [MarshalAs(UnmanagedType.LPArray, SizeParamIndex=3)] MyStruct[] myStructArray,
    [MarshalAs(UnmanagedType.LPArray, SizeParamIndex=3)] IntPtr[] myDataArray,
    [MarshalAs(UnmanagedType.LPArray, SizeParamIndex=3)] int[] myDataLengthArray,
    uint myDataCount,
    IntPtr userInfoStruct);



Can anyone tell me how I get each of the ushort[] arrays from the IntPtrs in myDataArray above?

Thanks,
Pete
Posted

1 solution

Rate this:
Please Sign up or sign in to vote.

Solution 1

Ok,
I seem to have a solution.
Using the declaration for MyCallBack2 above:
public ushort ShortToUShort(short val)
{
    return (ushort)val;
}
public int MyCallBack2(MyStruct[] myStructArray,
                       IntPtr[] myDataArray,
                       uint[] myDataLengthArray,
                       uint myDataCount,
                       IntPtr userInfoStruct)
{
    for (int i = 0; i < myDataCount; ++i)
    {
        short[] tmpMyData = new short[myDataLengthArray[i]];
        Marshal.Copy(myStructArray[i], tmpMyData, 0, (int)myDataLengthArray[i]);
        ushort[] myData = Array.ConvertAll<short, ushort>(tmpMyData,
                                                          new Converter<short, ushort>(ShortToUShort));
        MyCallBack(ref myStructArray[i],
                   myData,
                   myDataLengthArray[i],
                   userInfoStruct);
    }
    return 1;
}



This appears to run ok, however it looks messy converting the short[] to a ushort[], there has to be a cleaner way to do this.

Also when i run this in the debugger, Visual Studio hangs on me when I stop debugging.
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100