UPDATE: now you can simply download the attachment, unzip it in your mame or mameui folder, and execute. Wait 10-30 seconds.
The original post follows, you can use it as reference to manually patch the mame executable.
when I tried to use mame just with my controller, without the need of a keyboard, I realized that some keys cannot be mapped to the controller's buttons. Not even with joy2key. In order to make it work, everybody suggested to re-compile mame source code replacing "#define FORCE_DIRECTINPUT 0" in src/osd/windows/input.c to "#define FORCE_DIRECTINPUT 1". That works, but it's a little cumbersome, specially for people without a programming background (you need to setup the MinGW compiler and all that stuff).
After inspecting the source code, I realized that we can easily force rawinput_device_create() to fail and return NULL without recompiling. We will attack this lines:
// if this is an RDP name, skip it
if (_tcsstr(tname, TEXT("Root#RDP_")) != NULL)
As you can see, we can force an error if we replace the second argument to _tcsstr() with NULL. In order to do so, follow the instructions (they should work for Windows, Linux, Mac and any other system):
1) Open the Mame or MameUI executable with any hexadecimal editor. There are tons of free options out there.
2) Find the string "Root#RDP_" (try to find it in Ansi and Unicode). If your text editor doesn't support string searches, try in hexadecimal: Ansi hexadecimal "52 6F 6F 74 23 52 44 50 5F" or Unicode hexadecimal "52 00 6F 00 6F 00 74 00 23 00 52 00 44 00 50 00 5F 00").
3) Once you found the pattern, simply replace it by hexadecimal zeros and save the file. Note for non-programmers: don't confuse the 0 (zero) character with the 00 (hex zero). We need hex zeros here.
Next time you run the mame or mameui executable, direct input will be forced, meaning that you can use joy2key and similar programs to map keyboard keys to your controller's buttons. Tested with mame and mameui 154.