المساعد الشخصي الرقمي

مشاهدة النسخة كاملة : Retrieve array from unmanaged DLL into C# application



C# Programming
04-07-2009, 05:02 PM
I'm trying to get an output array from a function in an unmanaged DLL into my C# application. I've already done some googling on this topic and while I've found some articles, none of the suggestions seem to work.

The DLL has a function prototype of
int GetData(int ID, float* Results)

The int that is returned is the length of the array

Internally to send the results back it does

Results=new float[length];
//Fill in results here


In my C# application I use a delegate to get the function results:

[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
private delegate int delGetData(int TagID, ref IntPtr Results);


I then try and retrieve the results and copy to a managed array doing this:

IntPtr data=IntPtr.Zero;
int Len=GetData(ID, ref data);

if(Len==0)
//something happened, exit

float[] results=new float[Len];
Marshal.Copy(data, results, 0, Len);


I always get an exception on the Marshal.Copy because the source (data) is null.

I have some control over the unmanaged DLL, any suggestions on how to get these to interface?

Thanks