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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T16:04:28+00:00 2026-06-03T16:04:28+00:00

I’m developing an application which needs to interact with Video4Linux abstraction. The application is

  • 0

I’m developing an application which needs to interact with Video4Linux abstraction. The application is developed in C#, using the mono framework.

The problem I’m facing is that I can’t P/Invoke the ioctl system call. Or, more precisely, I can P/Invoke it, but it crashes badly.

The extern declaration is the following:

[DllImport("libc", EntryPoint = "ioctl", SetLastError = true)]
private extern static int KernelIoCtrl(int fd, int request, IntPtr data);

So far so good.

The actual routine that uses the KernelIoCtrl is the following:

protected virtual int Control(IoSpecification request, object data)
{
    GCHandle dataHandle;
    IntPtr dataPointer = IntPtr.Zero;

    try {
        // Pin I/O control data
        if (data != null) {
            dataHandle = GCHandle.Alloc(data, GCHandleType.Pinned);
            dataPointer = dataHandle.AddrOfPinnedObject();
        }
        // Perform I/O control
        int result =  KernelIoCtrl(mFileDescriptor, request.RequestCode, dataPointer);
        int errno = Marshal.GetLastWin32Error();

        // Throw exception on errors
        if (errno != (int)ErrNumber.NoError)
            throw new System.ComponentModel.Win32Exception(errno);

        return (result);
    } finally {
        if (dataPointer != IntPtr.Zero)
            dataHandle.Free();
    }
}

All the above code seems good. The class IoSpecification is used for computing the I/O request code following the header specification (basically it follows the _IOC macro declared at /usr/include/linux/asm/ioctl.h.

The data parameter is a structure, declared as follow:

[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
public struct Capability
{
    [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 16)]
    public string Driver;

    [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)]
    public string Device;

    [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)]
    public string BusInfo;

    public UInt32 Version;

    public CapabilityFlags Capabilities;

    [MarshalAs(UnmanagedType.ByValArray, SizeConst=4)]
    public UInt32[] Reserved;
}

which should mimic the following structure (declared at /usr/include/linux/videodev2.h):

struct v4l2_capability {
    __u8    driver[16];     /* i.e. "bttv" */
    __u8    card[32];       /* i.e. "Hauppauge WinTV" */
    __u8    bus_info[32];   /* "PCI:" + pci_name(pci_dev) */
    __u32   version;        /* should use KERNEL_VERSION() */
    __u32   capabilities;   /* Device capabilities */
    __u32   reserved[4];
};

Before having the crash, there was a problem in the IOCTL request code computation, and the KernelIoCtrl was working as expected (returning an errno equals to EINVAL). When I corrected the bug (and indeed having the correct IOCTRL request code), the call has started to cause a crash.

In conclusion, it seems there’s a problem in the structure marshalling, but I cannot see what it’s going wrong.

I fear that the problem is the variable argument list, because the ioctl routine is declared as follow (taken from man):

int ioctl(int d, int request, ...);

But I saw lot of code declaring the above routine as int ioctl(int d, int request, void*);, and I can ensure that the specific IOCTRL request takes only one argument.

  • 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-03T16:04:29+00:00Added an answer on June 3, 2026 at 4:04 pm

    Example usage:

    Capability capability;
    
    if (UnsafeNativeMethods.Ioctl(handle, request, ref capability) == -1)
    {
        throw new UnixIOException();
    }
    

    Capability:

    [StructLayout(LayoutKind.Sequential, Size = 104)]
    internal struct Capability
    {
        [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 16)]
        public string Driver;
    
        [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)]
        public string Device;
    
        [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)]
        public string BusInfo;
    
        public uint Version;
    
        public CapabilityFlags Capabilities;
    }
    

    UnsafeNativeMethods:

    internal static class UnsafeNativeMethods
    {
        [ReliabilityContract(Consistency.WillNotCorruptState, Cer.MayFail)]
        [DllImport("libc", EntryPoint = "close", SetLastError = true)]
        internal static extern int Close(IntPtr handle);
    
        [DllImport("libc", EntryPoint = "ioctl", SetLastError = true)]
        internal static extern int Ioctl(SafeUnixHandle handle, uint request, ref Capability capability);
    
        [DllImport("libc", EntryPoint = "open", SetLastError = true)]
        internal static extern SafeUnixHandle Open(string path, uint flag, int mode);
    
        internal static string Strerror(int error)
        {
            try
            {
                var buffer = new StringBuilder(256);
                var result = Strerror(error, buffer, (ulong)buffer.Capacity);
                return (result != -1) ? buffer.ToString() : null;
            }
            catch (EntryPointNotFoundException)
            {
                return null;
            }
        }
    
        [DllImport("MonoPosixHelper", EntryPoint = "Mono_Posix_Syscall_strerror_r", SetLastError = true)]
        private static extern int Strerror(int error, [Out] StringBuilder buffer, ulong length);
    }
    

    SafeUnixHandle:

    [SecurityPermission(SecurityAction.InheritanceDemand, UnmanagedCode = true)]
    [SecurityPermission(SecurityAction.Demand, UnmanagedCode = true)]
    internal sealed class SafeUnixHandle : SafeHandle
    {
        [ReliabilityContract(Consistency.WillNotCorruptState, Cer.MayFail)]
        private SafeUnixHandle()
            : base(new IntPtr(-1), true)
        {
        }
    
        public override bool IsInvalid
        {
            get { return this.handle == new IntPtr(-1); }
        }
    
        [ReliabilityContract(Consistency.WillNotCorruptState, Cer.MayFail)]
        protected override bool ReleaseHandle()
        {
            return UnsafeNativeMethods.Close(this.handle) != -1;
        }
    }
    

    UnixIOException:

    [Serializable]
    public class UnixIOException : ExternalException
    {
        private readonly int nativeErrorCode;
    
        [SecurityPermission(SecurityAction.Demand, UnmanagedCode = true)]
        public UnixIOException()
            : this(Marshal.GetLastWin32Error())
        {
        }
    
        [SecurityPermission(SecurityAction.Demand, UnmanagedCode = true)]
        public UnixIOException(int error)
            : this(error, GetErrorMessage(error))
        {
        }
    
        [SecurityPermission(SecurityAction.Demand, UnmanagedCode = true)]
        public UnixIOException(string message)
            : this(Marshal.GetLastWin32Error(), message)
        {
        }
    
        public UnixIOException(int error, string message)
            : base(message)
        {
            this.nativeErrorCode = error;
        }
    
        public UnixIOException(string message, Exception innerException)
            : base(message, innerException)
        {
        }
    
        protected UnixIOException(SerializationInfo info, StreamingContext context)
            : base(info, context)
        {
            this.nativeErrorCode = info.GetInt32("NativeErrorCode");
        }
    
        public int NativeErrorCode
        {
            get { return this.nativeErrorCode; }
        }
    
        public override void GetObjectData(SerializationInfo info, StreamingContext context)
        {
            if (info == null)
            {
                throw new ArgumentNullException("info");
            }
    
            info.AddValue("NativeErrorCode", this.nativeErrorCode);
            base.GetObjectData(info, context);
        }
    
        private static string GetErrorMessage(int error)
        {
            var errorDescription = UnsafeNativeMethods.Strerror(error);
            return errorDescription ?? string.Format("Unknown error (0x{0:x})", error);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

That's pretty much it. I'm using Nokogiri to scrape a web page what has
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am trying to understand how to use SyndicationItem to display feed which is
I used javascript for loading a picture on my website depending on which small
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I am reading a book about Javascript and jQuery and using one of the
I have a French site that I want to parse, but am running into
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and

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.