简体   繁体   English

指向C结构的第一个元素的指针与指向该结构的指针相同。 但不是在C#中:互操作性问题

[英]Pointer to first element of C struct is the same as pointer to the struct. But not in C#: Interop problem

Sorry for title, I have an interop problem. 对不起,标题,我遇到互操作问题。 The C dll that I'm using request, in a function, a pointer to the first element of a struct, however this in C can be cast to a pointer to that struct, while this is not the same in C#. 我在函数中使用的C dll在一个函数中指向结构的第一个元素的指针,但是可以在C中将其强制转换为该结构的指针,而在C#中则不同。 How can I "simulate" this behavior (I need to use that function in C#). 如何“模拟”此行为(我需要在C#中使用该函数)。

I'll post here a code snippet: 我将在此处发布一个代码段:

[StructLayout(LayoutKind.Sequential)]
struct lgLcdBitmapHeader
{
    public Formats Format;
}

[StructLayout(LayoutKind.Explicit)]
struct lgLcdBitmap
{
    [FieldOffset(0)]
    lgLcdBitmapHeader hdr;
    [FieldOffset(0)]
    lgLcdBitmap160x43x1 bmp_mono;
    [FieldOffset(0)]
    lgLcdBitmapQVGAx32 bmp_qvga32;
}

[StructLayout(LayoutKind.Sequential)]
struct lgLcdBitmap160x43x1 : IDisposable
{
    /// <summary>
    /// Format = LGLCD_BMP_FORMAT_160x43x1
    /// </summary>
    public lgLcdBitmapHeader hdr;
    /// <summary>
    /// byte array of size LGLCD_BMP_WIDTH * LGLCD_BMP_HEIGHT, use AllocHGlobal to make code safe
    /// </summary>
    public IntPtr pixels;

    public void SetPixels(byte[] value)
    {
        if (value.Length < (int)BitmapSizes.Size)
            throw new InvalidOperationException(string.Format("Byte array must be at least of size {0}", (int)BitmapSizes.Size));

        if (pixels == IntPtr.Zero)
            pixels = Marshal.AllocHGlobal((int)BitmapSizes.Size);

        Marshal.Copy(value, 0, pixels, (int)BitmapSizes.Size);
    }

    public void GetPixels(byte[] arrayToBeFilled)
    {
        if (arrayToBeFilled.Length < (int)BitmapSizes.Size)
            throw new InvalidOperationException(string.Format("Byte array must be at least of size {0}", (int)BitmapSizes.Size));

        Marshal.Copy(pixels, arrayToBeFilled, 0, (int)BitmapSizes.Size);
    }

    public void Dispose()
    {
        if (pixels != IntPtr.Zero)
            Marshal.FreeHGlobal(pixels);
    }
}

[StructLayout(LayoutKind.Sequential)]
struct lgLcdBitmapQVGAx32 : IDisposable
{
    /// <summary>
    /// Format = LGLCD_BMP_FORMAT_160x43x1
    /// </summary>
    public lgLcdBitmapHeader hdr;
    /// <summary>
    /// byte array of size LGLCD_QVGA_BMP_WIDTH * LGLCD_QVGA_BMP_HEIGHT * LGLCD_QVGA_BMP_BPP, use AllocHGlobal to make code safe
    /// </summary>
    public IntPtr pixels;


    public void SetPixels(byte[] value)
    {
        if (value.Length < (int)QVGABitmapSizes.Size)
            throw new InvalidOperationException(string.Format("Byte array must be at least of size {0}", (int)QVGABitmapSizes.Size));

        if (pixels == IntPtr.Zero)
            pixels = Marshal.AllocHGlobal((int)QVGABitmapSizes.Size);

        Marshal.Copy(value, 0, pixels, (int)QVGABitmapSizes.Size);
    }

    public void GetPixels(byte[] arrayToBeFilled)
    {
        if (arrayToBeFilled.Length < (int)QVGABitmapSizes.Size)
            throw new InvalidOperationException(string.Format("Byte array must be at least of size {0}", (int)QVGABitmapSizes.Size));

        Marshal.Copy(pixels, arrayToBeFilled, 0, (int)QVGABitmapSizes.Size);
    }

    public void Dispose()
    {
        if (pixels != IntPtr.Zero)
            Marshal.FreeHGlobal(pixels);
    }
}

The problem is that the function ask for lgLcdBitmapHeader pointer 问题是该函数要求lgLcdBitmapHeader指针

public extern static uint lgLcdUpdateBitmap([In] int device, [In] ref lgLcdBitmapHeader bitmap, [In] Priorities priority);

But this in C can be easily casted to lgLcdBitmap160x43x1 or lgLcdBitmapQVGAx32 , but not in C# What should I do to effectively pass one of those 2 structs to the function? 但这可以轻松地在C中强制 转换lgLcdBitmap160x43x1lgLcdBitmapQVGAx32 ,但在C#中则不能,该如何有效地将这两个结构之一传递给函数?

您应该声明extern两个不同的ref struct参数的extern函数的两个重载。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM