From 30fe442f524e91639e9d5f27d86c2edf290ab9c0 Mon Sep 17 00:00:00 2001 From: Stanislaw Halik Date: Fri, 5 Jul 2013 00:19:03 +0200 Subject: Robustify SimConnect protocol - No longer leaks library handle/address space - Use an activation context instead of hardcoding probable DLL filenames --- CMakeLists.txt | 2 +- ftnoir_protocol_sc/ftnoir-protocol-sc.rc | 4 +-- ftnoir_protocol_sc/ftnoir_protocol_sc.cpp | 56 +++++++++++++++++++++--------- ftnoir_protocol_sc/ftnoir_protocol_sc.h | 12 +++---- ftnoir_protocol_sc/scserver.manifest | 5 --- ftnoir_protocol_sc/simconnect.aps | Bin 1264 -> 0 bytes 6 files changed, 47 insertions(+), 32 deletions(-) delete mode 100644 ftnoir_protocol_sc/simconnect.aps diff --git a/CMakeLists.txt b/CMakeLists.txt index 7bc0f497..ef231b06 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -189,7 +189,7 @@ if(NOT SDK_FACEAPI_ONLY) QT4_WRAP_UI(opentrack-proto-win32-mouse-uih ${opentrack-proto-win32-mouse-ui}) QT4_ADD_RESOURCES(opentrack-proto-win32-mouse-rcc ${opentrack-proto-win32-mouse-rc}) - file(GLOB opentrack-proto-simconnect-c "ftnoir_protocol_sc/*.cpp") + file(GLOB opentrack-proto-simconnect-c "ftnoir_protocol_sc/*.cpp" "ftnoir_protocol_sc/ftnoir-protocol-sc.rc") file(GLOB opentrack-proto-simconnect-h "ftnoir_protocol_sc/*.h") QT4_WRAP_CPP(opentrack-proto-simconnect-moc ${opentrack-proto-simconnect-h}) file(GLOB opentrack-proto-simconnect-ui "ftnoir_protocol_sc/*.ui") diff --git a/ftnoir_protocol_sc/ftnoir-protocol-sc.rc b/ftnoir_protocol_sc/ftnoir-protocol-sc.rc index 3838d941..3b43715e 100644 --- a/ftnoir_protocol_sc/ftnoir-protocol-sc.rc +++ b/ftnoir_protocol_sc/ftnoir-protocol-sc.rc @@ -1,2 +1,2 @@ -#include "winuser.h" -2 RT_MANIFEST scserver.manifest \ No newline at end of file +#include +142 RT_MANIFEST scserver.manifest \ No newline at end of file diff --git a/ftnoir_protocol_sc/ftnoir_protocol_sc.cpp b/ftnoir_protocol_sc/ftnoir_protocol_sc.cpp index 3cc25be4..a178e009 100644 --- a/ftnoir_protocol_sc/ftnoir_protocol_sc.cpp +++ b/ftnoir_protocol_sc/ftnoir_protocol_sc.cpp @@ -54,6 +54,8 @@ float FTNoIR_Protocol::prevSCRotX = 0.0f; float FTNoIR_Protocol::prevSCRotY = 0.0f; float FTNoIR_Protocol::prevSCRotZ = 0.0f; +static QLibrary SCClientLib; + /** constructor **/ FTNoIR_Protocol::FTNoIR_Protocol() { @@ -139,7 +141,7 @@ PDWORD_PTR MsgResult = 0; // prevRotY = virtRotY; // prevRotZ = virtRotZ; - if SUCCEEDED(simconnect_calldispatch(hSimConnect, processNextSimconnectEvent, NULL)) { + if (SUCCEEDED(simconnect_calldispatch(hSimConnect, processNextSimconnectEvent, NULL))) { qDebug() << "FTNoIR_Protocol::sendHeadposeToGame() says: Dispatching"; } else { @@ -153,25 +155,39 @@ PDWORD_PTR MsgResult = 0; // bool FTNoIR_Protocol::checkServerInstallationOK() { - qDebug() << "SCCheckClientDLL says: Starting Function"; - - const char* simconnect_paths[] = { - "SimConnect.DLL", - "C:\\Windows\\WinSxS\\x86_microsoft.flightsimulator.simconnect_67c7c14424d61b5b_10.0.60905.0_none_dd92b94d8a196297\\SimConnect.DLL", - "C:\\Windows\\WinSxS\\x86_microsoft.flightsimulator.simconnect_67c7c14424d61b5b_10.0.61242.0_none_e079b46b85043c20\\SimConnect.DLL", - "C:\\Windows\\WinSxS\\x86_microsoft.flightsimulator.simconnect_67c7c14424d61b5b_10.0.61259.0_none_55f5ecdc14f60568\\SimConnect.DLL", - NULL - }; - - for (int i = 0; simconnect_paths[i]; i++) + HANDLE hactctx = INVALID_HANDLE_VALUE; + ULONG_PTR actctx_cookie = NULL; + if (!SCClientLib.isLoaded()) { - SCClientLib.setFileName(simconnect_paths[i]); + ACTCTXA actx = {0}; + actx.cbSize = sizeof(ACTCTXA); + actx.lpResourceName = MAKEINTRESOURCEA(142); + actx.dwFlags = ACTCTX_FLAG_RESOURCE_NAME_VALID; + QString path = QCoreApplication::applicationDirPath() + "/opentrack-proto-simconnect.dll"; + QByteArray name = QFile::encodeName(path); + actx.lpSource = name.constData(); + hactctx = CreateActCtxA(&actx); + actctx_cookie = 0; + if (hactctx != INVALID_HANDLE_VALUE) { + if (!ActivateActCtx(hactctx, &actctx_cookie)) { + qDebug() << "SC: can't set win32 activation context" << GetLastError(); + ReleaseActCtx(hactctx); + hactctx = INVALID_HANDLE_VALUE; + } + } else { + qDebug() << "SC: can't create win32 activation context"; + } + + qDebug() << "SCCheckClientDLL says: Starting Function"; + + SCClientLib.setFileName("SimConnect.DLL"); + if (!SCClientLib.load()) { - qDebug() << "SCCheckClientDLL says: Error loading SimConnect DLL"; - qDebug() << SCClientLib.errorString(); - continue; + qDebug() << "SC load" << SCClientLib.errorString(); + return false; } - break; + } else { + qDebug() << "SimConnect already loaded"; } // @@ -226,6 +242,12 @@ bool FTNoIR_Protocol::checkServerInstallationOK() } qDebug() << "FTNoIR_Protocol::checkServerInstallationOK() says: SimConnect functions resolved in DLL!"; + + if (hactctx != INVALID_HANDLE_VALUE) + { + DeactivateActCtx(0, actctx_cookie); + ReleaseActCtx(hactctx); + } return true; } diff --git a/ftnoir_protocol_sc/ftnoir_protocol_sc.h b/ftnoir_protocol_sc/ftnoir_protocol_sc.h index 62210d32..c22c36c7 100644 --- a/ftnoir_protocol_sc/ftnoir_protocol_sc.h +++ b/ftnoir_protocol_sc/ftnoir_protocol_sc.h @@ -30,7 +30,6 @@ #ifndef INCLUDED_SCSERVER_H #define INCLUDED_SCSERVER_H #include "facetracknoir/global-settings.h" - // // Prevent the SimConnect manifest from being merged in the application-manifest // This is necessary to run FaceTrackNoIR on a PC without FSX @@ -38,11 +37,11 @@ #define SIMCONNECT_H_NOMANIFEST #define _WIN32_WINNT 0x0502 -#include "Windows.h" -#include "SimConnect.h" +#include +#include -#include "..\ftnoir_protocol_base\ftnoir_protocol_base.h" -#include "ui_FTNoIR_SCcontrols.h" +#include <..\ftnoir_protocol_base\ftnoir_protocol_base.h> +#include #include #include #include @@ -92,7 +91,6 @@ public: private: // Private properties QString ProgramName; - QLibrary SCClientLib; static float virtSCPosX; static float virtSCPosY; @@ -121,7 +119,7 @@ private: importSimConnect_AddClientEventToNotificationGroup simconnect_addclienteventtonotificationgroup; importSimConnect_SetNotificationGroupPriority simconnect_setnotificationgrouppriority; - static HANDLE hSimConnect; // Handle to SimConnect + static HANDLE hSimConnect; // Handle to SimConnect static void CALLBACK processNextSimconnectEvent(SIMCONNECT_RECV* pData, DWORD cbData, void *pContext); void loadSettings(); }; diff --git a/ftnoir_protocol_sc/scserver.manifest b/ftnoir_protocol_sc/scserver.manifest index 19b123ba..6d6a8edb 100644 --- a/ftnoir_protocol_sc/scserver.manifest +++ b/ftnoir_protocol_sc/scserver.manifest @@ -5,9 +5,4 @@ - - - - - diff --git a/ftnoir_protocol_sc/simconnect.aps b/ftnoir_protocol_sc/simconnect.aps deleted file mode 100644 index 89c5a65a..00000000 Binary files a/ftnoir_protocol_sc/simconnect.aps and /dev/null differ -- cgit v1.2.3