joyGetPosEx 在 C# 中返回 165

本文关键字:返回 joyGetPosEx | 更新日期: 2023-09-27 17:56:44

我试图用 C# 从我的 JoyStick 中读出数据,只要我使用 jeyGetPos 就可以正常工作。但是我需要使用 joyGetPosEx,因为它提供了更多的日期,例如我需要的操纵杆的旋转。

class JoyStick
{
    JOYINFO pji;
    JOYINFOEX pjiex;
    MMRESULT mmresult;
    public JoyStick()
    {
        pji = new JOYINFO();
        pjiex = new JOYINFOEX();
    }
    [StructLayout(LayoutKind.Sequential)]
    public struct JOYINFO
    {
        public uint wYpos;
        public uint wZpos;
        public uint wButtons;
    }
    [StructLayout(LayoutKind.Sequential)]
    public struct JOYINFOEX
    {
        public uint dwSize;
        public uint dwFlags;
        public uint dwXpos;
        public uint dwYpos;
        public uint dwZpos;
        public uint dwRpos;
        public uint dwUpos;
        public uint dwVpos;
        public uint dwButtons;
        public uint dwButtonNumber;
        public uint dwPOV;
        public uint dwReserved1;
        public uint dwReserved2;
    }
    [StructLayout(LayoutKind.Sequential)]
    public struct MMRESULT
    {
        public uint uJoyID;
    }
    [DllImport("winmm.dll")]
    public static extern uint joyGetNumDevs();
    [DllImport("winmm.dll")]
    public static extern MMRESULT joyGetPos(uint uJoyID, ref JOYINFO pji);
    [DllImport("winmm.dll"), System.Security.SuppressUnmanagedCodeSecurity]
    public static extern MMRESULT joyGetPosEx(uint uJoyID, ref JOYINFOEX pjiex);
    public uint getNumDevs()
    {
        return joyGetNumDevs();
    }
    private MMRESULT getPos(uint uJoyID, ref JOYINFO pji)
    {
        return joyGetPos(uJoyID, ref pji);
    }
    private MMRESULT getPosEx(uint uJoyID,  ref JOYINFOEX pjiex)
    {
        return joyGetPosEx(uJoyID, ref pjiex);
    }
    public JOYINFO getPos(uint id)
    {
        mmresult = getPos(id, ref pji);
        return pji;
    }
    public JOYINFOEX getPosEx(uint id)
    {
        mmresult = getPosEx(id, ref pjiex);
        return pjiex;
    }
    public uint getMMRESULT(){
        return mmresult.uJoyID;
    }
}

MMRESULT.uJoyID 是 165,只要我尝试使用 joyGetPosEx 读取数据但它是 0 与 joyGetPos.我相信 165 表示 ID 是错误的,但我尝试使用从 0 到 15 的每个有效 ID。

我的错误在哪里?

joyGetPosEx 在 C# 中返回 165

  1. 无需声明 MMRESULT 结构,只需使用 uint/int 作为函数返回值,或使用枚举
  2. 您必须填写dwSize字段。

    pjiex.dwSize = Marshal.SizeOf(pjiex);