Generics with P / Invoke and pointers

I use C interfaces that have a callback function where the software works with a pointer.

On the C # side, I have the following code to copy a pointer to and from a managed array, for example. for float *.

class FloatPointer
{
    readonly unsafe float* _pointer;

    public uint Size { get; set; }

    public float[] Array { get; set; }

    public unsafe void CopyToPointer ()
    {
        int length = (int)Math.Min (Size, Array.Length);
        Marshal.Copy (Array, 0, (IntPtr)_pointer, length);
        for (int i = length; i < Size; i++) {
            _pointer [i] = 0;
        }
    }

    unsafe float[] CopyFromPointer ()
    {
        float[] result = new float[Size];
        Marshal.Copy ((IntPtr)_pointer, result, 0, (int)Size);
        return result;
    }

    public unsafe FloatPointer (float* pointer, uint size)
    {
        _pointer = pointer;
        Size = size;
        Array = CopyFromPointer ();
    }
}

How should I do the same for unsigned char*, uint*etc., I was thinking about using a common class for it class Pointer<T>: where T: ValueType.

Unfortunately, this restriction is not possible.

If I changed this to an unlimited general class, then Marshal.Copyit tells me that he does not know about T[].

Is there any way to create this generic class?

+4
source share
1 answer

, , . , .

public class StructPointer<T> where T : struct
{
    /// <summary>
    /// Pointer
    /// </summary>
    public IntPtr Pointer { get; set; }

    /// <summary>
    /// Number of elements in pointer
    /// </summary>
    public int PointerElementCount { get; set; }

    /// <summary>
    /// Array
    /// </summary>
    public T[] Array { get; set; }

    private int sizeOfT;

    /// <summary>
    /// Copy Array to Pointer
    /// </summary>
    public void ArrayToPointer()
    {
        if (Array == null || Pointer == IntPtr.Zero)
        {
            return;
        }

        int length = (int)Math.Min(PointerElementCount, Array.Length);
        int byteCount = length * sizeOfT;
        GCHandle handle = GCHandle.Alloc(Array, GCHandleType.Pinned);
        unsafe
        {
            for (int i = 0; i < byteCount; i++)
            {
                *(((byte*)Pointer) + i) = *(((byte*)handle.AddrOfPinnedObject()) + i);
            }
        }
        handle.Free();

        if (PointerElementCount > Array.Length)
        {
            unsafe
            {
                int byteCount2 = byteCount + (sizeOfT * (PointerElementCount - Array.Length));
                for (int i = byteCount; i < byteCount2; i++)
                {
                    *(((byte*)Pointer) + i) = 0;
                }
            }
        }
    }

    /// <summary>
    /// Copy Pointer to Array
    /// </summary>
    public void ArrayFromPointer()
    {
        if (Pointer == IntPtr.Zero)
        {
            return;
        }

        Array = new T[PointerElementCount];
        GCHandle handle = GCHandle.Alloc(Array, GCHandleType.Pinned);
        int byteCount = Array.Length * sizeOfT;
        unsafe
        {
            for (int i = 0; i < byteCount; i++)
            {
                *(((byte*)handle.AddrOfPinnedObject()) + i) = *(((byte*)Pointer) + i);
            }
        }
        handle.Free();
    }

    public StructPointer()
    {
        sizeOfT = Marshal.SizeOf(typeof(T));
    }

    /// <summary>
    /// Constructor. Copies pointer to Array.
    /// </summary>
    /// <param name="pointer">Pointer</param>
    /// <param name="length">Number of elements in pointer</param>
    public StructPointer(IntPtr pointer, int length)
    {
        sizeOfT = Marshal.SizeOf(typeof(T));
        Pointer = pointer;
        PointerElementCount = length; // number of elements in pointer, not number of bytes
        ArrayFromPointer();
    }
}
+2

Source: https://habr.com/ru/post/1628880/


All Articles