Question

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?

Was it helpful?

Solution

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top