summaryrefslogtreecommitdiff
path: root/Common/NightSecApp.cpp
blob: 0fcec5e15b2bf2dc8ccb2486c2e1dbdea0188a75 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
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);
}
*/