I want to capture input from joystick using winmm dll .. I don't have much knowledge of importing and working with DLLs.
I tried to do it but i have no idea how to do it ..
[DllImport("winmm.dll")]
public static extern string joyGetPosEx(uint dev, JoyinfoEx ) //Something Similar
How do I get joyinfoEx struct from DLL and put it into JoygetPosEx .. ? :S
As your question suggests, you have to import the joyGetPosEx
function from wimmm.dll
. This is often called P/Invoking because you're invoking a function out of a native Win32 API (or "platform" DLL). You already know that you need to use that function, so let's look at it more carefully.
The documentation for that function indicates that it takes two parameters:
The first (
uJoyID
) is a simpleint
(or, integer) value that identifies the joystick to be queried.The second (
pji
) is a little bit more complicated than the first, and I think this is the part you were struggling with. Instead of a standard value type, it is a structure calledJOYINFOEX
, and you have to define that structure in your code. What thejoyGetPosEx
function does is fill that special structure with information about the joystick's position, allowing it to return a bunch of different values all at once. That's why you have to pass it as a reference (ref
), because the function is actually going to fill the instance of the structure that you pass to it with a set of values. Your code can then read those values back out of the variable containing the instance of the structure.
The documentation for that structure is available here, and it tells you the names and data types
of each of its members. You can treat aDWORD
as anint
, so its declaration looks like this:[StructLayout(LayoutKind.Sequential)] public struct JOYINFOEX { public int dwSize; public int dwFlags; public int dwXpos; public int dwYpos; public int dwZpos; public int dwRpos; public int dwUpos; public int dwVpos; public int dwButtons; public int dwButtonNumber; public int dwPOV; public int dwReserved1; public int dwReserved2; }
Finally, the joyGetPosEx
function returns a value of type int
. This value tells you whether or not the function succeeded, and if it didn't, what exactly went wrong. It's an error code. The documentation linked above gives you a table of all the possible values that can be returned. If you want to check against those values, you'll need to define them as constants in your code:
public const int JOYERR_BASE = 160
public const int JOYERR_PARMS = (JOYERR_BASE + 5);
public const int JOYERR_UNPLUGGED = (JOYERR_BASE + 7);
public const int MMSYSERR_BASE = 0;
public const int MMSYSERR_BADDEVICEID = (MMSYSERR_BASE + 2);
public const int MMSYSERR_INVALPARAM = (MMSYSERR_BASE + 11);
So, given the above, the final function definition looks like this:
[DllImport("winmm.dll")]
public static extern int joyGetPosEx(int uJoyID, ref JOYINFOEX pji);
In the future, keep in mind pinvoke.net as a reference for how to define and declare these Win32 API functions in your code. It doesn't happen to have the joyGetPosEx
function (or at least not that I found), but it usually has almost everything you need. And then if that fails, you can do as I tried to explain above: look at the original documentation for the function and try to figure out to define it accordingly to work in .NET.
The function returns data through the second argument. The return value is an integer, indicating any possible error. The proper declaration is:
[DllImport("winmm.dll")]
private static extern int joyGetPosEx(int joystickNumber, ref JOYINFOEX info);
You'll have to declare the structure like this:
[StructLayout(LayoutKind.Sequential)]
private struct JOYINFOEX {
public int dwSize;
public int dwFlags;
public int dwXpos;
public int dwYpos;
public int dwZpos;
public int dwRpos;
public int dwUpos;
public int dwVpos;
public int dwButtons;
public int dwButtonNumber;
public int dwPOV;
public int dwReserved1;
public int dwReserved2;
}
A sample call:
JOYINFOEX info = new JOYINFOEX();
info.dwSize = Marshal.SizeOf(info);
int err = joyGetPosEx(1, ref info);
if (err != 0) reportError(err);
else {
int xpos = info.dwXpos;
// Do something with xpos
//...
}
来源:https://stackoverflow.com/questions/4416994/joystick-key-capture