diff --git a/BuildNo.rc b/BuildNo.rc index cf39e7e..694ff18 100644 --- a/BuildNo.rc +++ b/BuildNo.rc @@ -1 +1 @@ -#define BUILD_NUMBER 29 +#define BUILD_NUMBER 30 diff --git a/IDirectInputDeviceX.cpp b/IDirectInputDeviceX.cpp index bd81935..2b23c98 100644 --- a/IDirectInputDeviceX.cpp +++ b/IDirectInputDeviceX.cpp @@ -118,8 +118,7 @@ HRESULT m_IDirectInputDeviceX::GetDeviceData(DWORD cbObjectData, LPDIDEVICEOBJEC return DIERR_INVALIDPARAM; } - // Verify that only one thread can use the variable at a time - SetCriticalSection(dodThreadFlag); + EnterCriticalSection(&dics); // Check the size of the array if (rgdod && pdwInOut && *pdwInOut > pdod.size()) @@ -140,7 +139,7 @@ HRESULT m_IDirectInputDeviceX::GetDeviceData(DWORD cbObjectData, LPDIDEVICEOBJEC } } - ReleaseCriticalSection(dodThreadFlag); + LeaveCriticalSection(&dics); return hr; } @@ -152,6 +151,8 @@ HRESULT m_IDirectInputDeviceX::SetDataFormat(LPCDIDATAFORMAT lpdf) // Fix unsupported flags if (lpdf && lpdf->dwNumObjs && lpdf->dwObjSize == 16) { + EnterCriticalSection(&dics); + if (rgodf.size() < lpdf->dwNumObjs) { rgodf.resize(lpdf->dwNumObjs); @@ -173,7 +174,11 @@ HRESULT m_IDirectInputDeviceX::SetDataFormat(LPCDIDATAFORMAT lpdf) rgodf[x].dwFlags = lpdf->rgodf[x].dwFlags; } - return ProxyInterface->SetDataFormat(&df); + HRESULT hr = ProxyInterface->SetDataFormat(&df); + + LeaveCriticalSection(&dics); + + return hr; } return ProxyInterface->SetDataFormat(lpdf); @@ -351,8 +356,7 @@ HRESULT m_IDirectInputDeviceX::SendDeviceData(DWORD cbObjectData, LPCDIDEVICEOBJ return DIERR_INVALIDPARAM; } - // Verify that only one thread can use the variable at a time - SetCriticalSection(dodThreadFlag); + EnterCriticalSection(&dics); // Check the size of the array if (rgdod && pdwInOut && *pdwInOut > pdod.size()) @@ -374,7 +378,7 @@ HRESULT m_IDirectInputDeviceX::SendDeviceData(DWORD cbObjectData, LPCDIDEVICEOBJ HRESULT hr = ProxyInterface->SendDeviceData(sizeof(DIDEVICEOBJECTDATA), (rgdod) ? &pdod[0] : nullptr, pdwInOut, fl); - ReleaseCriticalSection(dodThreadFlag); + LeaveCriticalSection(&dics); return hr; } diff --git a/IDirectInputDeviceX.h b/IDirectInputDeviceX.h index 25e2bbc..6f61ace 100644 --- a/IDirectInputDeviceX.h +++ b/IDirectInputDeviceX.h @@ -13,8 +13,10 @@ class m_IDirectInputDeviceX // For CooperativeLevel bool CanAquireDevice = false; + // Critical section for shared memory + CRITICAL_SECTION dics; + // For DeviceData - bool dodThreadFlag = false; std::vector pdod; // For DataFormat @@ -27,10 +29,16 @@ class m_IDirectInputDeviceX StringType = GetStringType(WrapperID); LOG_LIMIT(3, "Creating device " << __FUNCTION__ << "(" << this << ")" << " converting device from v" << Version << " to v8 using " << ((StringType == DEFAULT_CHARSET) ? "UNICODE" : "ANSI")); + + // Initialize Critical Section + InitializeCriticalSection(&dics); } ~m_IDirectInputDeviceX() { LOG_LIMIT(3, __FUNCTION__ << "(" << this << ")" << " deleting device!"); + + // Delete Critical Section + DeleteCriticalSection(&dics); } /*** IUnknown methods ***/ diff --git a/dinputto8.cpp b/dinputto8.cpp index 00aca56..3dca362 100644 --- a/dinputto8.cpp +++ b/dinputto8.cpp @@ -162,20 +162,3 @@ HRESULT WINAPI DllUnregisterServer() return m_pDllUnregisterServer(); } - -void dinputto8::SetCriticalSection(bool &ThreadSyncFlag) -{ - if (ThreadSyncFlag) - { - do { - Sleep(0); - } while (ThreadSyncFlag); - } - - ThreadSyncFlag = true; -} - -void dinputto8::ReleaseCriticalSection(bool &ThreadSyncFlag) -{ - ThreadSyncFlag = false; -}