none
Unsafe structure declaration RRS feed

  • Question

  • Hi,

    does anybody know how to declare the following c struct in unsafe c#:

    typedef struct {

    int csp; /* [in] colorspace; or with XVID_CSP_VFLIP to perform vertical flip */

    void * plane[4]; /* [in] image plane ptrs */

    int stride[4]; /* [in] image stride; "bytes per row"*/

    } xvid_image_t;

    thank you in advance.

    Marco

    Friday, June 13, 2008 12:24 PM

Answers

  • Indeed.  Try a safe structure instead, marshaled by the P/Invoke marshaler:

    using System.Runtime.InteropServices;
    ...

        [StructLayout(LayoutKind.Sequential)]
        private struct xvid_image_t {
          int csp;
          [MarshalAs(UnmanagedType.ByValArray, SizeConst = 4)]
          IntPtr[] plane;
          [MarshalAs(UnmanagedType.ByValArray, SizeConst = 4)]
          int[] stride;

          void Initialize() {
            plane = new IntPtr[4];
            stride = new int[4];
          }
        }


    Hans Passant.
    • Marked as answer by Bruno Yu Monday, June 16, 2008 5:43 AM
    Saturday, June 14, 2008 3:43 PM
    Moderator

All replies

  •  

    unsafe struct xvid_image_t

    {

    int csp; /* [in] colorspace; or with XVID_CSP_VFLIP to perform vertical flip */

    void*[] plane; /* [in] image plane ptrs */

    int[] stride; /* [in] image stride; "bytes per row"*/

    }


    Vitaliy Liptchinsky
    Friday, June 13, 2008 2:54 PM
  • Use the fixed keyword.
    Hans Passant.
    Friday, June 13, 2008 3:06 PM
    Moderator
  • Yes, but how do I specify that the plane and stride arrays have 4 occurences?

    Marco
    Saturday, June 14, 2008 2:01 PM
  •  The 'fixed' keyword for fixed size buffer can't be used with void* data type.

    Marco
    Saturday, June 14, 2008 2:03 PM
  • Indeed.  Try a safe structure instead, marshaled by the P/Invoke marshaler:

    using System.Runtime.InteropServices;
    ...

        [StructLayout(LayoutKind.Sequential)]
        private struct xvid_image_t {
          int csp;
          [MarshalAs(UnmanagedType.ByValArray, SizeConst = 4)]
          IntPtr[] plane;
          [MarshalAs(UnmanagedType.ByValArray, SizeConst = 4)]
          int[] stride;

          void Initialize() {
            plane = new IntPtr[4];
            stride = new int[4];
          }
        }


    Hans Passant.
    • Marked as answer by Bruno Yu Monday, June 16, 2008 5:43 AM
    Saturday, June 14, 2008 3:43 PM
    Moderator