XInput PInvoking your XBox 360 Controller
- Posted: Nov 30, 2012 at 6:00 AM
- 5,029 Views
- 1 Comment
Loading User Information from Channel 9
Something went wrong getting user information from Channel 9
Loading User Information from MSDN
Something went wrong getting user information from MSDN
Loading Visual Studio Achievements
Something went wrong getting the Visual Studio Achievements
Today's hardware Friday project by Joel Ivory Johnson is one that I thought interesting in a number of ways. One, that it shows how we can use the XBox controller without XNA, yet still using managed languages and also as a non-trivial example of how to translate native calls for use in a managed world.
A few years ago I wrote on using XNA to access the Xbox 360 controller from a Windows Form application. I had received a question about accessing the controller in an environment in which the XNA assemblies wouldn't be appropriate. So I wrote this code as an example of accessing the controller without the XNA assemblies. This is still a managed program. But it makes use of P/Invoke to use the functionality provided in the DirectX library.
I wrote this code using Visual Studio 2012. It makes use of the
XInputdll which is already part of Windows. So no additional installations are needed to use the code.
What is XInput
DirectX is composed of many libraries. There's Direct3D for for rendering 3D graphics, DirectSound for playing sound, and so on. DirectInput contains functionality for accessing various input devices (joysticks, keyboards, and more). But the Xbox 360 controller is not accessed through that library. It is accessed through a different library named
XInput. This library is dedicated to the the Xbox 360 controller. Let's take a look at what is in the C language header file to see what functionality it provides. On my computer I can find the header in
C:\Program Files (x86)\Windows Kits\8.0\Include\um\Xinput.h.
The header starts with defining the name of the DLL in which we will find the functionality.
When a program is being compiled for an OS version before Windows 8 the functionality in the
xinput1_4.dll. For programs compiled for Widnows 8 the functionality from
xinput9_1_0.dllwill be used. If you wanted to target both platforms you could make use of the older DLL (
xinput9_1_0.dll). It will work on Windows 8 too. But the newer DLL (
xinput1_4.dll) contains functionality for accessing the battery .
In short, if you have a wired Xbox 360 controller laying about and you want to use it in your managed app's, but can't use XNA, this article is just what you're looking for...