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.
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.
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.
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.
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/
I'm actually using Community 2015 and didn't get any warnings. Pretty sure I'm using the default warning level.
Yeah, you need to increase warning level to 4. On commandline this is done with /W4 switch.