XInput PInvoking your XBox 360 Controller
- Posted: Nov 30, 2012 at 6:00 AM
- 4,428 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.
Introduction
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.
Prerequisites
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 inC:\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 fromxinput9_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...
Comments have been closed since this content was published more than 30 days ago, but if you'd like to continue the conversation,
please create a new thread in our Forums,
or
Contact Us and let us know.
Follow the Discussion
Oops, something didn't work.
What does this mean?
Following an item on Channel 9 allows you to watch for new content and comments that you are interested in. You need to be signed in to Channel 9 to use this feature.What does this mean?
Following an item on Channel 9 allows you to watch for new content and comments that you are interested in and view them all on your notifications page.sign up for email notifications?
DirectSound is only meant for legacy code; modern games should use XAudio2 and/or Microsoft Media Foundation for audio. DirectInput is deprecated and should only be used for legacy joystick and gamepad support (any controller that you can use XInput with you should use XInput, and keyboard and mouse should be used with standard Win32 messages, .NET classes, or Windows Runtime functionality.
Otherwise, great article and sample!
Remove this comment
Remove this thread
close