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#. How can I "simulate" this behavior (I need to use that function in 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(pix开发者_如何学编程els);
}
}
The problem is that the function ask for lgLcdBitmapHeader pointer
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?
You should declare two overloads of your extern
function that take two different ref
struct parameters.
精彩评论