栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

P /调用ioctl系统调用

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

P /调用ioctl系统调用

用法示例:

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

能力:

[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);    }}


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/409028.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号