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 8208799
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T09:29:54+00:00 2026-06-07T09:29:54+00:00

Very weird error showing up when trying to set a structure to a pointer…

  • 0

Very weird error showing up when trying to set a structure to a pointer… the invalid cast exception is thrown and I’m completely confused as to why. The function asks for 3 parameters, an object, IntPtr, and a boolean flag. All of which I am setting… and even explicitly casting them as such makes no difference. The object is a structure, the Intptr is a pointer and has data, and the flag is merely set to false.

Has anybody received this invalidcastexception in a scenario like this before?

EDIT

internal class FrameTransport
{
    #region Fields
    internal SharedMemoryChannel controlChannel;
    internal ControlChannelStruct controlStruct;
    internal SharedMemoryChannel frameChannel1;
    internal SharedMemoryChannel frameChannel2;
    internal byte[] bitmapData;
    internal int bitmapDataSize;
    internal uint sharedInfoSize;
    internal bool haveFrame;
    internal int height;
    internal int width;
    #endregion


    #region Structures
    [StructLayout(LayoutKind.Sequential, Pack = 1)]
    internal struct FrameChannelStruct
    {
        public Int32 bufid;
        public Int32 size;
        public Int32 width;
        public Int32 height;
        public Int32 bitsperpixel;
        public Int32 fourcc;
        public Int32 orientation;
        public Int64 clientpointer;
    };

    [StructLayout(LayoutKind.Sequential, Pack = 1)]
    internal struct ControlChannelStruct
    {
        public Int32 bufferstates;
        public FrameChannelStruct buffer1;
        public FrameChannelStruct buffer2;
        public BufferType buftype;
        public Int32 configuration;
        public Int32 fourcccount;
        public Int32 fourcc01;
        public Int32 fourcc02;
        public Int32 fourcc03;
        public Int32 fourcc04;
        public Int32 fourcc05;
        public Int32 fourcc06;
        public Int32 fourcc07;
        public Int32 fourcc08;
        public Int32 fourcc09;
        public Int32 fourcc10;
    };
    #endregion


    #region Constructors
    /// <summary>
    /// Constructor.
    /// </summary>
    public FrameTransport()
    {
        sharedInfoSize = (uint)Marshal.SizeOf((Type)typeof(ControlChannelStruct));
        haveFrame = false;
        bitmapData = null;
        bitmapDataSize = 0;
        frameChannel1 = null;
        frameChannel2 = null;

        controlStruct = new ControlChannelStruct();
        controlStruct.bufferstates = 1;
        controlStruct.buffer1.bufid = -1;
        controlStruct.buffer2.bufid = -1;
        controlStruct.buffer1.clientpointer = 1;
        controlStruct.buffer2.clientpointer = 2;
        controlStruct.fourcccount = 0;
        controlStruct.buftype = BufferType.WINBUFFERS;

        controlChannel = new SharedMemoryChannel();
        bool success = controlChannel.CreateMapping(sharedInfoSize);

        if (!success)
        {
            throw new Exception("Unable to create memory mapping for video frame transport.");
        }

        controlChannel.OpenMapping(controlChannel.key);
        int error = Win32.GetLastError();

        if (error != 0)
        {
            throw new Exception("Unable to map memory for video frame transport.");
        }
    }
    #endregion


    #region Internal Members
    /// <summary>
    /// Send the control data.
    /// </summary>
    internal void SendControlData()
    {
        Marshal.StructureToPtr(controlStruct, controlChannel.data, false);
    }

    /// <summary>
    /// Get the control data.
    /// </summary>
    internal void GetControlData()
    {
        controlStruct.bufferstates = -1;
        controlStruct = (ControlChannelStruct)Marshal.PtrToStructure(controlChannel.data, typeof(ControlChannelStruct));
    }

    /// <summary>
    /// Communicate supported pixel formats to the runtime.
    /// </summary>
    /// <param name="count"></param>
    /// <param name="fourCcs"></param>
    internal void SetPreferences(int count, Int32[] fourCcs)
    {
        if (fourCcs.Length != 1)
        {
            throw new Exception("For now im assuming only one fourcc here.");
        }

        controlStruct.fourcccount = count;
        controlStruct.fourcc01 = fourCcs[0];
        controlStruct.fourcc02 = 0;
        controlStruct.fourcc03 = 0;
        controlStruct.fourcc04 = 0;
        controlStruct.fourcc05 = 0;
        controlStruct.fourcc06 = 0;
        controlStruct.fourcc07 = 0;
        controlStruct.fourcc08 = 0;
        controlStruct.fourcc09 = 0;
        controlStruct.fourcc10 = 0;

        SendControlData();
    }

    /// <summary>
    /// Get the buffer states.
    /// </summary>
    /// <returns></returns>
    internal int GetBufStates()
    {
        GetControlData();

        return controlStruct.bufferstates;
    }

    /// <summary>
    /// Return the channel's key.
    /// </summary>
    /// <returns></returns>
    internal uint Key()
    {
        return (uint)controlChannel.key;
    }

    /// <summary>
    /// Check if a new frame is available.
    /// </summary>
    /// <returns></returns>
    internal bool IsNewFrameAvailable()
    {
        GetControlData();
        int bufferState = (controlStruct.bufferstates & 0x3);

        if ((bufferState != 0x00) & (bufferState != 0x3))
        {
            return false;
        }

        return true;
    }

    /// <summary>
    /// Get the frame.
    /// </summary>
    /// <returns></returns>
    internal bool GetFrame()
    {
        int bufferState = (controlStruct.bufferstates & 0x03);

        if (!haveFrame & bufferState == 0x01) 
        { 
            return false; 
        }

        if (!haveFrame)
        {
            haveFrame = true;
        }

        if (bufferState == 0x00)
        {
            controlStruct.bufferstates |= 0x02;
        }

        if (bufferState == 0x03)
        {
            controlStruct.bufferstates &= ~0x02;
        }

        bufferState = (controlStruct.bufferstates & 0x03);

        FrameChannelStruct buffer;
        SharedMemoryChannel channel;

        if (bufferState == 0x1)
        {
            buffer = controlStruct.buffer1;
            channel = frameChannel1;
        }
        else if (bufferState == 0x2)
        {
            buffer = controlStruct.buffer2;
            channel = frameChannel2;
        }
        else
        {
            throw new Exception("Error: unexpected video control buffer state.");
        }

        if (channel == null)
        {
            channel = new SharedMemoryChannel();
            bool success = channel.OpenMapping(buffer.bufid);

            if (!success)
            {
                throw new Exception("Unable to map frame bitmap channel.");
            }
        };

        if (channel != null)
        {
            if (channel.key != buffer.bufid)
            {
                return false;
            };
        };

        width = buffer.width;
        height = buffer.height;
        int bytesPerPixel = buffer.bitsperpixel >> 3;
        int newFrameSize = width * height * bytesPerPixel;

        if (newFrameSize != bitmapDataSize)
        {
            bitmapData = null;
            bitmapDataSize = newFrameSize;
            bitmapData = new byte[bitmapDataSize];
        }

        Marshal.Copy(channel.data, bitmapData, 0, bitmapDataSize);

        if (bufferState == 0x1)
        {
            controlStruct.buffer1 = buffer;
            frameChannel1 = channel;
        }
        else if (bufferState == 0x2)
        {
            controlStruct.buffer2 = buffer;
            frameChannel2 = channel;
        }

        SendControlData();

        return true;
    }
    #endregion
}
  • 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-06-07T09:29:56+00:00Added an answer on June 7, 2026 at 9:29 am

    BufferType inside the structure had to be an enum, as a custom Type holding the enumerated values was the flaw.

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

Sidebar

Related Questions

I'm getting a very weird exception when trying to deploy my app on JBOSS
I have come across a very weird error. I'm on Solaris 10, using Ruby
So, I have this very weird error - Some advanced PHP devs might also
I am getting a very weird error when using VBA in excel. I am
There is a weird error in a very basic implementation of phpmailer in which
I'm getting a very weird answer when I'm trying to install ImageMagick through Homebrew.
I have a very weird problem while trying to pass and SQL query using
I have a very weird error with c++. I have two values, max and
I m facing very weird problem in wordpress wp-admin. I was trying to upload
I am having a very weird error on this block: <script type=text/javascript> var captions

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.