summaryrefslogtreecommitdiff
path: root/Common/NightSecApp.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'Common/NightSecApp.cpp')
-rw-r--r--Common/NightSecApp.cpp138
1 files changed, 138 insertions, 0 deletions
diff --git a/Common/NightSecApp.cpp b/Common/NightSecApp.cpp
new file mode 100644
index 0000000..0fcec5e
--- /dev/null
+++ b/Common/NightSecApp.cpp
@@ -0,0 +1,138 @@
+// NightSecApp.cpp: implementation of the CNightSecApp class.
+//
+//////////////////////////////////////////////////////////////////////
+
+#include "stdafx.h"
+#include "NightSecApp.h"
+
+#include <regsvr.h>
+#include <appmisc.h>
+#include <path.h>
+#include "../common/defines.h"
+
+//////////////////////////////////////////////////////////////////////
+// Construction/Destruction
+//////////////////////////////////////////////////////////////////////
+
+CComponentArray g_aComponents;
+
+#define NS_REGISTER_FAILED _T("Night Security was unable to register the %s component.\n\n%s\nIf Night Security doesn't seem to be working properly please reinstall.")
+const DWORD dwTimeOut = 5000; // time for EXE to be idle before shutting down
+
+CNightSecApp::CNightSecApp(const string& sAppName, const string& sMtxName)
+ : m_sAppName(sAppName), m_oneInstance(sMtxName),
+ m_settings(HKEY_CURRENT_USER, NS_REG_MAIN)
+{
+
+}
+
+CNightSecApp::~CNightSecApp()
+{
+
+}
+
+bool CNightSecApp::InitInstance()
+{
+ LPCTSTR szCmdLine = GetCommandLine();
+
+ // If we've been asked to close then exit here
+ if(_tcsstr(szCmdLine, _T("close")) != NULL)
+ return false;
+
+ // Load up all Components
+ if(!g_aComponents.LoadComponents())
+ {
+ RegisterDlls();
+ g_aComponents.LoadComponents();
+ }
+
+ return true;
+}
+
+bool CNightSecApp::ExitInstance()
+{
+ g_aComponents.UnloadComponents();
+ return false;
+}
+
+HRESULT CNightSecApp::RegisterDlls(bool bUI /*= true*/)
+{
+ file_iterator iterFiles(GetProgramFolder(_Module.m_hInst),
+ _T("*.dll"), file_iterator::full_paths);
+
+ file_iterator end;
+
+ HRESULT hRet = S_OK;
+
+ for( ; iterFiles != end; iterFiles++)
+ {
+ HRESULT hr = RegisterDLL(iterFiles->cFileName);
+
+ if(SUCCEEDED(hr) || !bUI)
+ continue;
+
+ string sError;
+ file_path fileDll(iterFiles->cFileName);
+ sError.format(NS_REGISTER_FAILED, (LPCTSTR)fileDll.file(), (LPCTSTR)FormatHR(hr));
+
+ MessageBox(NULL, sError, m_sAppName, MB_OK | MB_ICONEXCLAMATION);
+ }
+
+ return hRet;
+}
+
+// Passed to CreateThread to monitor the shutdown event
+/*static DWORD WINAPI MonitorProc(void* pv)
+{
+ CNightSecApp* p = (CNightSecApp*)pv;
+ p->MonitorShutdown();
+ return 0;
+}*/
+
+/*LONG CNightSecApp::Unlock()
+{
+ LONG l = CComModule::Unlock();
+ if (l == 0)
+ {
+ bActivity = true;
+ SetEvent(hEventShutdown); // tell monitor that we transitioned to zero
+ }
+ return l;
+}
+*/
+//Monitors the shutdown event
+/*void CNightSecApp::MonitorShutdown()
+{
+ while (1)
+ {
+ WaitForSingleObject(hEventShutdown, INFINITE);
+ DWORD dwWait=0;
+ do
+ {
+ bActivity = false;
+ dwWait = WaitForSingleObject(hEventShutdown, dwTimeOut);
+ } while (dwWait == WAIT_OBJECT_0);
+ // timed out
+ if (!bActivity && m_nLockCnt == 0) // if no activity let's really bail
+ {
+#if _WIN32_WINNT >= 0x0400 & defined(_ATL_FREE_THREADED)
+ CoSuspendClassObjects();
+ if (!bActivity && m_nLockCnt == 0)
+#endif
+ break;
+ }
+ }
+ CloseHandle(hEventShutdown);
+ PostThreadMessage(dwThreadID, WM_QUIT, 0, 0);
+}
+
+bool CNightSecApp::StartMonitor()
+{
+ hEventShutdown = CreateEvent(NULL, false, false, NULL);
+ if (hEventShutdown == NULL)
+ return false;
+ DWORD dwThreadID;
+ HANDLE h = CreateThread(NULL, 0, MonitorProc, this, 0, &dwThreadID);
+ return (h != NULL);
+}
+*/ \ No newline at end of file