joyGetPosEx returns 165 in C#

人盡茶涼 提交于 2019-12-02 12:26:22

问题


I tried to read out the data from my JoyStick in C# which is working fine as long as i do it with jeyGetPos. But I need to use joyGetPosEx because it delivers more date like the rotation of the Joystick which I need.

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

The MMRESULT.uJoyID is 165 as long as I try to read the data with joyGetPosEx but it is 0 with joyGetPos. I believe 165 means the ID is wrong but I tried with every valid ID from 0 to 15.

Where is my mistake?


回答1:


  1. No need to declare MMRESULT structure, just use uint/int as function returned value, or use enum
  2. You have to fill dwSize field.

    pjiex.dwSize = Marshal.SizeOf(pjiex);



来源:https://stackoverflow.com/questions/22885662/joygetposex-returns-165-in-c-sharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!