DLL UINT array pointer

Hey everyone,

I was just trying to use an external DLL developed in flat assembly. I got everything nicely imported, except I am stuck on the "UINT pointer to an array of floats:

original:

UINT GetData(
UINT ptrFloatArray);

my version:

[DllImport (“dll”)]
private static extern uint GetData( uint ptrFloatArray );

I have tried a lot the last couple of hours, but Unity keeps crashing (and I am totally the one to blame for that! ;)).

Most of the time I have been trying to use the GCHandle in various ways:

	private GCHandle _hGCFile;
	public float[] buffer;

		// get the legth of the file
		uint length = 512;
		
		// create the buffer which will keep the file in memory
		buffer = new float[length];

		_hGCFile = GCHandle.Alloc( buffer, GCHandleType.Pinned );

and then using GCHandle.ToIntPtr(_hGCFile) as parameter, or other variants including casts to uint, UInt32, Int32, etc. Or just passing the array as float[ ] to the function.

But nothing works, and if Unity does not crash, the array stays empty (which is not good as well ;)). Anyone have a simple suggestion on what I might be overlooking?

Thanks!

The TexturePlugin example helped me out a lot.

It’s pretty much exactly what you want.
They parse Unity’s Color (which is made of four floats) - so just take is as a reference and edit as needed.

This is a small snippet of some dll interface code I wrote. Maybe you can get some hints to your problem from that.

static extern int VcGetEncBytes(IntPtr pData, int maxLength, IntPtr pWrittenBytes);


public static bool GetEncodedBytes(ref byte[] array, ref int writtenBytes)
{
    GCHandle pArray = GCHandle.Alloc(array, GCHandleType.Pinned);
    GCHandle pWrittenBytes = GCHandle.Alloc(writtenBytes, GCHandleType.Pinned);
    if (VcGetEncBytes(pArray.AddrOfPinnedObject(), array.Length*sizeof(byte), pWrittenBytes.AddrOfPinnedObject()) == (int)VoiceComponentReturn.Success)
    {
        writtenBytes = (int)pWrittenBytes.Target;
        pArray.Free();
        pWrittenBytes.Free();
        return true;
    }
    writtenBytes = 0;
    pArray.Free();
    pWrittenBytes.Free();
    return false;
}

//perlohmann