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

  • SEARCH
  • Home
  • 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 3673782
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T02:46:30+00:00 2026-05-19T02:46:30+00:00

Ok guys, this is making me crazy. I’m creating a port for a C

  • 0

Ok guys, this is making me crazy.
I’m creating a port for a C lib in C# but I’ve a problem in using a bitmap (generated with gdi) with a byte array (required from the c lib)

Here are is the code, (pastie) divided in files:

  1. Lglcd.dll: http://pastie.org/1424596
    (compiled)
  2. G19dotNet.dll:
    http://pastie.org/1424600 (compiled,
    this is the interop lib for c#)
  3. TestProject:
    http://pastie.org/1424603 (compile,
    but throws an exception)

The problem is in the last file (the other two are pretty straightforward), line 116

res = LgLcd.NativeMethods.lgLcdUpdateBitmap(openContext.device, ref bmp.hdr, LgLcd.NativeConstants.LGLCD_SYNC_UPDATE(LgLcd.NativeConstants.LGLCD_PRIORITY_NORMAL));

This throws an exception for an invalid memory access to managed memory.

The signature of the function is this one:

/// Return Type: DWORD->unsigned int
            ///device: int
            ///bitmap: lgLcdBitmapHeader*
            ///priority: DWORD->unsigned int
            [System.Runtime.InteropServices.DllImportAttribute("LgLcd", EntryPoint = "lgLcdUpdateBitmap")]
            public static extern uint lgLcdUpdateBitmap([System.Runtime.InteropServices.In] int device, [System.Runtime.InteropServices.In] ref lgLcdBitmapHeader bitmap, [System.Runtime.InteropServices.In] uint priority);

As you can see the second param is a pointer to lgLcdBitmapHeader, but I suppose (because I’ve seen an older version of the lib) that this pointer is casted to a lgLcdBitmapQVGAx32 pointer (which is a struct of different size)

I think the problem is there, however I can’t manage to solve this, really

here is the signature of the struct:

    [System.Runtime.InteropServices.StructLayoutAttribute(System.Runtime.InteropServices.LayoutKind.Sequential)]
    public struct lgLcdBitmapHeader
    {

        /// DWORD->unsigned int
        public uint Format;
    }

and

    [System.Runtime.InteropServices.StructLayoutAttribute(System.Runtime.InteropServices.LayoutKind.Sequential)]
    public struct lgLcdBitmap160x43x1
    {

        /// lgLcdBitmapHeader->Anonymous_5fa96ca7_9cc3_4b33_b5aa_ccff9833813a
        public lgLcdBitmapHeader hdr;

        /// BYTE[]
        //public fixed byte pixels[NativeConstants.LGLCD_BMP_WIDTH * NativeConstants.LGLCD_BMP_HEIGHT * NativeConstants.LGLCD_BMP_BPP];
        [System.Runtime.InteropServices.MarshalAs(System.Runtime.InteropServices.UnmanagedType.ByValArray, SizeConst = NativeConstants.LGLCD_BMP_WIDTH * NativeConstants.LGLCD_BMP_HEIGHT * NativeConstants.LGLCD_BMP_BPP)]
        public byte[] pixels;
        public static int SizeConst { get { return NativeConstants.LGLCD_BMP_WIDTH * NativeConstants.LGLCD_BMP_HEIGHT * NativeConstants.LGLCD_BMP_BPP; } }
    }

I hope someone can help me, I’m watching all over the web and I’ve found a .net port of this lib but it’s very old, and doesn’t have the problem that I have because doesn’t use colored bitmaps (with 4 bytes for each color) and doesn’t use lgLcdBitmapHeader struct (it uses a simpler one). Also it’s source code is very similar to mine.

Any help will be appreciated

Useful Links:

http://lglcdnet.codeplex.com/SourceControl/changeset/changes/5538

Update 1:

I did some progress based on a teory.

DWORD WINAPI lgLcdUpdateBitmap(IN int device,
                           IN const lgLcdBitmapHeader *bitmap,
                           IN DWORD priority);

This signature has “a meaning” in c, because the pointer to the first element of a struct is also the pointer to that struct.
In fact lgLcdBitmapQVGAx32 has a first element of type lgLcdBitmapHeader.
That said, they are using the possibility of C to cast everything to everything to create a “generic method”, because lgLcdBitmapHeader can be both lgLcdBitmap160x43x1 (first elem is lgLcdBitmapHeader) or lgLcdBitmapQVGAx32.

This is a problem because in C# I can’t emulate this capacity, so I created some “helper” functions which accept lgLcdBitmap160x43x1 and lgLcdBitmapQVGAx32 which are internally used as pointers to lgLcdBitmapHeader.

This done, however, gave me another error:

System.Runtime.InteropServices.MarshalDirectiveException non è stata gestita
  Message=Impossibile effettuare il marshalling di 'parameter #2': Limitazione interna: la struttura è troppo complessa o troppo grande.
  Source=G19dotNet
  StackTrace:
       in G19dotNet.LgLcd.NativeMethods.lgLcdUpdateBitmapQVGAx32(Int32 device, lgLcdBitmapQVGAx32& bitmap, UInt32 priority)
       in ConsoleTest2.Program.Main(String[] args) in C:\Documents and Settings\Administrator\documenti\visual studio 2010\Projects\G19dotNet\ConsoleTest2\Program.cs:riga 116
       in System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
       in System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
       in Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       in System.Threading.ThreadHelper.ThreadStart_Context(Object state)
       in System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
       in System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       in System.Threading.ThreadHelper.ThreadStart()
  InnerException: 

English version of System.Runtime.InteropServices.MarshalDirectiveException non è stata gestita
Message=Impossibile effettuare il marshalling di ‘parameter #2’: Limitazione interna: la struttura è troppo complessa o troppo grande.
:

System.Runtime.InteropServices.MarshalDirectiveException not handled
Message=Impossible do marshalling of ‘parameter #2’: Internal limitation: the structure is too big or too complex

It has an array of 307200 bytes, what should I do?

Update 2:

I managed to show an image on my screen, means that my teory was correct, I had to use this type of “thing” to make it works: http://bytes.com/topic/c-sharp/answers/272048-internal-limitation-structure-too-complex-too-large
However the image shown is “broken”, I mean it has the shape of the original image but a bit confused and uncolored, maybe because the way on how I pass the bitmap?

  • 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-19T02:46:31+00:00Added an answer on May 19, 2026 at 2:46 am

    Usage of unmanaged allocation is needed as explained directly in the question.

    Code example:

    IntPtr unhandledPtr = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(LgLcd.lgLcdBitmapQVGAx32)));
    Marshal.StructureToPtr(bmp, unhandledPtr, true);
    res = LgLcd.NativeMethods.lgLcdUpdateBitmap(openContext.device, unhandledPtr, LgLcd.NativeConstants.LGLCD_SYNC_UPDATE(LgLcd.NativeConstants.LGLCD_PRIORITY_NORMAL));
    Marshal.FreeHGlobal(unhandledPtr);
    

    Also I needed to change the extern method signature:

        [DllImportAttribute("LgLcd", EntryPoint = "lgLcdUpdateBitmap")]
        public static extern uint lgLcdUpdateBitmap([In] int device, [In] IntPtr bitmap, [In] uint priority);
        //OLD ONE:
        public static extern uint lgLcdUpdateBitmap([In] int device, [In] ref lgLcdBitmapHeader bitmap, [In] uint priority);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hi Guys new to this site but a big fan. Right the problem. It's
Hey guys..this is kind of a weird question but I'm making a webapp in
Sorry for the wall of text guys but this one requires expliaining, way too
guys I know this question is very basic but I've met in few publications
Guys I know this question is silly but just to make sure: Having in
Ok guys I imagine this is easy but I can't seem to find how
Hey guys i'm making a website but for some reason my div tags are
hey guys, this might be really stupid, but hopefully someone can help. I'm trying
I dunno guys, this is a really weird one, but I might just be
Hope to get a little help from you guys. Im using this script when

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.