Coding4Fun Blog

XInput PInvoking your XBox 360 Controller

Description

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.

Using XInput to access an Xbox 360 Controller in Managed Code

image

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 XInput dll 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.dll will 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 .

SNAGHTML15c5dd17[4]

SNAGHTML15c6405c

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...

The Discussion

  • mikebmcl

    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!

Comments closed

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.