Handmade Hero»Forums»Code
Simon Morgan
6 posts
LoadLibrary XInput returns NULL
I used to have controller support working. It has stopped working. I have no idea why it has stopped working. LoadLibrary always returns NULL. I'm linking against XInput9_1_0.lib. I'm trying to load xinput1_4.dll and xinput1_3.dll. I've tried using an official Xbox 360 controller and a Logitech F310 both of which support XInput. Can somebody please help?

I've uploaded my code here.
Simon Morgan
6 posts
LoadLibrary XInput returns NULL
I found the cause (I think) but don't know what was causing it. This code didn't work:

1
2
3
4
5
HMODULE XInputLibrary = LoadLibrary("xinput1_4.dll");

if (!XInputLibrary) {
    HMODULE XInputLibrary = LoadLibrary("xinput1_3.dll");
}


and this did:

1
2
3
4
5
HMODULE XInputLibrary = LoadLibrary("xinput1_4.dll");

if (!XInputLibrary) {
    XInputLibrary = LoadLibrary("xinput1_3.dll");
}


Declared values seem to be non-NULL by default and I can see why this would obviously screw with the if statement, but I don't see why this would be a problem in practise because XInputLibrary is immediately assigned the return value of LoadLibrary.
511 posts
LoadLibrary XInput returns NULL
drguildo
I found the cause (I think) but don't know what was causing it. This code didn't work:

1
2
3
4
5
HMODULE XInputLibrary = LoadLibrary("xinput1_4.dll");

if (!XInputLibrary) {
    HMODULE XInputLibrary = LoadLibrary("xinput1_3.dll");
}


and this did:

1
2
3
4
5
HMODULE XInputLibrary = LoadLibrary("xinput1_4.dll");

if (!XInputLibrary) {
    XInputLibrary = LoadLibrary("xinput1_3.dll");
}


Declared values seem to be non-NULL by default and I can see why this would obviously screw with the if statement, but I don't see why this would be a problem in practise because XInputLibrary is immediately assigned the return value of LoadLibrary.


Because the second assignment to XInputLibrary in

1
2
3
4
5
HMODULE XInputLibrary = LoadLibrary("xinput1_4.dll");

if (!XInputLibrary) {
    HMODULE XInputLibrary = LoadLibrary("xinput1_3.dll");
}


is assigned to a whole new variable which is thrown away when it exits the block. Leaving you with the null in the original variable. This is called variable shadowing.
Simon Morgan
6 posts
LoadLibrary XInput returns NULL
ratchetfreak
Because the second assignment to XInputLibrary in

1
2
3
4
5
HMODULE XInputLibrary = LoadLibrary("xinput1_4.dll");

if (!XInputLibrary) {
    HMODULE XInputLibrary = LoadLibrary("xinput1_3.dll");
}


is assigned to a whole new variable which is thrown away when it exits the block. Leaving you with the null in the original variable. This is called variable shadowing.


That makes sense. Thanks.
Mārtiņš Možeiko
2559 posts / 2 projects
LoadLibrary XInput returns NULL
If you use Visual Studio, try to upgrade to 2015. It detects these situations and produces warning or error: https://blogs.msdn.microsoft.com/...ts-to-warnings-in-the-c-compiler/
Simon Morgan
6 posts
LoadLibrary XInput returns NULL
I'm actually using Community 2015 and didn't get any warnings. Pretty sure I'm using the default warning level.
Mārtiņš Možeiko
2559 posts / 2 projects
LoadLibrary XInput returns NULL
Yeah, you need to increase warning level to 4. On commandline this is done with /W4 switch.