static int GetGamepadCount()
        {
            // determine number of joysticks installed in Windows 95

            JOYINFOEX info = new JOYINFOEX();      // extended information

            info.dwFlags = 128; // buttons!
            info.dwSize = System.Runtime.InteropServices.Marshal.SizeOf(typeof(JOYINFOEX));

            Int32 dwResult;   // examine return values

            // Loop through all possible joystick IDs until we get the error
            // JOYERR_PARMS. Count the number of times we get JOYERR_NOERROR
            // indicating an installed joystick driver with a joystick currently
            // attached to the port.
            for (int i = 0; i < joyGetNumDevs(); i++)
            {
                dwResult = GamePad.joyGetPosEx(i, ref info);
                if (dwResult == (int)JoyError.JOYERR_UNPLUGGED || dwResult == (int)JoyError.JOYERR_PARMS)
                    return i;

            }
            return -1;
        }
 internal static extern int joyGetPosEx(int uJoyID, ref JOYINFOEX pji);