问题描述
我尝试从C#中的JoyStick读取数据,只要我使用jeyGetPos即可正常工作.但是我需要使用joyGetPosEx,因为它可以提供更多的日期,例如我需要的操纵杆旋转.
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;
}
}
只要我尝试使用joyGetPosEx读取数据,则MMRESULT.uJoyID为165但对于joyGetPos,它为0.我相信165表示ID错误,但是我尝试使用0到15之间的每个有效ID.
The MMRESULT.uJoyID is 165 as long as I try to read the data with joyGetPosExbut it is 0 with joyGetPos.I believe 165 means the ID is wrong but I tried with every valid ID from 0 to 15.
我的错误在哪里?
推荐答案
- 无需声明MMRESULT结构,只需使用uint/int作为函数返回值或使用枚举
-
您必须填写
dwSize
字段.
- No need to declare MMRESULT structure, just use uint/int as functionreturned value, or useenum
You have to fill
dwSize
field.
pjiex.dwSize = Marshal.SizeOf(pjiex);
pjiex.dwSize = Marshal.SizeOf(pjiex);
这篇关于joyGetPosEx在C#中返回165的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!