PtrToStructure C#中的空引用异常

时间:2021-06-02 21:33:14

标签: c# pointers structure nullreferenceexception logitech

Visual Studio 2019 发送此异常

我明白了

public struct DIJOYSTATE2ENGINES
{
    public int lX;              /* x-axis position              */
    public int lY;              /* y-axis position              */
    public int lZ;              /* z-axis position              */
    public int lRx;             /* x-axis rotation              */
    public int lRy;             /* y-axis rotation              */
    public int lRz;             /* z-axis rotation              */
    [MarshalAs(UnmanagedType.ByValArray, SizeConst = 2)]
    public int[] rglSlider;     /* extra axes positions         */
    [MarshalAs(UnmanagedType.ByValArray, SizeConst = 4)]
    public uint[] rgdwPOV;      /* POV directions               */
    [MarshalAs(UnmanagedType.ByValArray, SizeConst = 128)]
    public byte[] rgbButtons;   /* 128 buttons                  */
    public int lVX;             /* x-axis velocity              */
    public int lVY;             /* y-axis velocity              */
    public int lVZ;             /* z-axis velocity              */
    public int lVRx;            /* x-axis angular velocity      */
    public int lVRy;            /* y-axis angular velocity      */
    public int lVRz;            /* z-axis angular velocity      */
    [MarshalAs(UnmanagedType.ByValArray, SizeConst = 2)]
    public int[] rglVSlider;    /* extra axes velocities        */
    public int lAX;             /* x-axis acceleration          */
    public int lAY;             /* y-axis acceleration          */
    public int lAZ;             /* z-axis acceleration          */
    public int lARx;            /* x-axis angular acceleration  */
    public int lARy;            /* y-axis angular acceleration  */
    public int lARz;            /* z-axis angular acceleration  */
    [MarshalAs(UnmanagedType.ByValArray, SizeConst = 2)]
    public int[] rglASlider;    /* extra axes accelerations     */
    public int lFX;             /* x-axis force                 */
    public int lFY;             /* y-axis force                 */
    public int lFZ;             /* z-axis force                 */
    public int lFRx;            /* x-axis torque                */
    public int lFRy;            /* y-axis torque                */
    public int lFRz;            /* z-axis torque                */
    [MarshalAs(UnmanagedType.ByValArray, SizeConst = 2)]
    public int[] rglFSlider;    /* extra axes forces            */
};                                             
                                              
  [DllImport("LogitechSteeringWheelEnginesWrapper", CallingConvention = CallingConvention.Cdecl)]
public static extern IntPtr LogiGetStateENGINES(int index);

这是你看到的上下文,我得到了 DIJOYSTATE2ENGINES 和返回 intptr 的方法 LogiGetStateENGINES()

但是当我在这个句子中使用 ptrtoStrurcture 时,做空引用 .

ret = (DIJOYSTATE2ENGINES)Marshal.PtrToStructure(LogiGetStateENGINES(index), typeof (DIJOYSTATE2ENGINES));

这种方法。

public static DIJOYSTATE2ENGINES LogiGetStateCSharp(int index)
{
    DIJOYSTATE2ENGINES ret = new DIJOYSTATE2ENGINES();
    ret.rglSlider = new int[2];
    ret.rgdwPOV = new uint[4];
    ret.rgbButtons = new byte[128];
    ret.rglVSlider = new int[2];
    ret.rglASlider = new int[2];
    ret.rglFSlider = new int[2];
    try
    {
        ret = (DIJOYSTATE2ENGINES)Marshal.PtrToStructure(LogiGetStateENGINES(index), typeof (DIJOYSTATE2ENGINES));
    }
    catch (System.ArgumentException)
    {
        Debug.Write("Exception catched");
    }
    return ret;
}

0 个答案:

没有答案
相关问题