Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6601683
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T18:47:08+00:00 2026-05-25T18:47:08+00:00

Sorry for title, I have an interop problem. The C dll that I’m using

  • 0

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(pixels);
    }
}

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T18:47:09+00:00Added an answer on May 25, 2026 at 6:47 pm

    You should declare two overloads of your extern function that take two different ref struct parameters.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Sorry about the terrible title. I have a signed assembly 'Signed.dll' that is a
Sorry the title isn't more help. I have a database of media-file URLs that
Sorry for the title may seem confusing. i have 2 element. with no relation
Sorry for the title, my problem is as follows. I have a list of
(Sorry if the title is pretty useless) I have this function to get the
First, sorry for bad title, couldn't come up with something more descriptive. I have
Sorry for stupid title, first. :) Problem is very simple... .gitignore ignores me! Scenario:
Using MS Access. I'm sorry that the title is vague--i just don't exactly know
Sorry about that confusing title :) I have a resource, ComatosePage (used in the
Sorry if the title doesn't make sense. Basically I have a series of strings

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.