Skip to content

Commit 3e7a7c8

Browse files
committed
Fix bug with LoadLibrary being in a static constructor from inside a DLL. Removed FreeLibrary intentionally, let me know if you know a more elegant solution.
1 parent 1ab05fd commit 3e7a7c8

File tree

8 files changed

+26
-9
lines changed

8 files changed

+26
-9
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,5 @@
2828
*.unityPackage
2929
obj/
3030
Binaries/
31-
XInputUnity/Library/
31+
XInputUnity/Library/
32+
Temp/

XInputInterface/GamePad.cpp

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include "GamePad.h"
2+
#include <cstdio>
23

34
namespace
45
{
@@ -9,25 +10,38 @@ namespace
910
{
1011
public:
1112
XInputLoader()
12-
: mHandle(0), mGetState(0), mSetState(0)
13+
: mLoaded(false), mHandle(0), mGetState(0), mSetState(0)
1314
{
14-
mHandle = LoadLibrary("xinput1_3.dll");
15-
16-
// Ordinal 100 is the same as XInputGetState but supports the Guide button.
17-
mGetState = (XInputGetStatePointer)GetProcAddress(mHandle, (LPCSTR)100);
18-
19-
mSetState = (XInputSetStatePointer)GetProcAddress(mHandle, "XInputSetState");
2015
}
2116

2217
~XInputLoader()
2318
{
24-
FreeLibrary(mHandle);
19+
}
20+
21+
void ensureLoaded()
22+
{
23+
if (!mLoaded)
24+
{
25+
mHandle = LoadLibrary("xinput1_3.dll");
26+
if (mHandle != NULL)
27+
{
28+
mGetState = (XInputGetStatePointer)GetProcAddress(mHandle, (LPCSTR)100); // Ordinal 100 is the same as XInputGetState but supports the Guide button.
29+
mSetState = (XInputSetStatePointer)GetProcAddress(mHandle, "XInputSetState");
30+
mLoaded = true;
31+
}
32+
else
33+
{
34+
printf_s("[XInput.NET] Failed to loaded xinput1_3.dll (error code 0x%08x, check that DirectX End-User Runtimes"
35+
" is installed (http://www.microsoft.com/en-us/download/details.aspx?id=8109)\n", GetLastError());
36+
}
37+
}
2538
}
2639

2740
XInputGetStatePointer mGetState;
2841
XInputSetStatePointer mSetState;
2942

3043
private:
44+
bool mLoaded;
3145
HMODULE mHandle;
3246
};
3347

@@ -36,11 +50,13 @@ namespace
3650

3751
DWORD XInputGamePadGetState(DWORD dwUserIndex, XINPUT_STATE* pState)
3852
{
53+
gXInputLoader.ensureLoaded();
3954
return gXInputLoader.mGetState(dwUserIndex, pState);
4055
}
4156

4257
void XInputGamePadSetState(DWORD dwUserIndex, float leftMotor, float rightMotor)
4358
{
59+
gXInputLoader.ensureLoaded();
4460
XINPUT_VIBRATION vibration = { (int)(leftMotor * 65535), (int)(rightMotor * 65535) };
4561
gXInputLoader.mSetState(dwUserIndex, &vibration);
4662
}
0 Bytes
Binary file not shown.
12 KB
Binary file not shown.
0 Bytes
Binary file not shown.
13.5 KB
Binary file not shown.
188 Bytes
Binary file not shown.

XInputUnity/XInputInterface.dll

12 KB
Binary file not shown.

0 commit comments

Comments
 (0)