操纵杆按键捕捉
我想使用 winmm dll 捕获来自操纵杆的输入..我对导入和使用 DLL 没有太多了解。
我尝试这样做,但我不知道该怎么做..
[DllImport("winmm.dll")]
public static extern string joyGetPosEx(uint dev, JoyinfoEx ) //Something Similar
如何从DLL获取joyinfoEx结构并将其放入JoygetPosEx..? :S
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
正如您的问题所示,您必须从
wimmm.dll
导入joyGetPosEx
函数。这通常称为 P/Invoking 因为您正在调用一个函数本机 Win32 API(或“平台”DLL)的。您已经知道需要使用该函数,所以让我们更仔细地看看它。该函数的文档表明它需要两个参数:
第一个 (
uJoyID
) 是一个简单的int
(或整数)值,用于标识要查询的操纵杆。第二个(
pji
)比第一个稍微复杂一些,我认为这是您正在努力解决的部分。它不是标准值类型,而是一个名为JOYINFOEX
的结构,您必须在代码中定义该结构。 JoyGetPosEx 函数的作用是用有关操纵杆位置的信息填充该特殊结构,使其能够一次返回一堆不同的值。这就是为什么您必须将其作为引用 (ref
) 传递,因为该函数实际上将使用一组参数来填充您传递给它的结构实例价值观。然后,您的代码可以从包含该结构实例的变量中读取这些值。该结构的文档位于此处,它会告诉您
每个成员的名称和数据类型。您可以将
DWORD
视为int
,因此其声明如下所示:最后,
joyGetPosEx
函数返回一个类型的值int
。该值告诉您该函数是否成功,如果没有成功,则到底出了什么问题。这是一个错误代码。上面链接的文档为您提供了可以返回的所有可能值的表格。如果您想检查这些值,则需要在代码中将它们定义为常量:因此,鉴于上述内容,最终的函数定义如下所示:
将来,请记住 pinvoke.net 作为如何在代码中定义和声明这些 Win32 API 函数的参考。它碰巧没有
joyGetPosEx
函数(或者至少我没有发现),但它通常几乎拥有您需要的一切。然后,如果失败,您可以按照我上面尝试解释的那样进行操作:查看该函数的原始文档,并尝试找出相应的定义以使其在 .NET 中工作。As your question suggests, you have to import the
joyGetPosEx
function fromwimmm.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 a
DWORD
as anint
, so its declaration looks like this:Finally, the
joyGetPosEx
function returns a value of typeint
. 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:So, given the above, the final function definition looks like this:
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:
You'll have to declare the structure like this:
A sample call: