-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApplicationMonitor.cpp
More file actions
280 lines (223 loc) · 6.29 KB
/
ApplicationMonitor.cpp
File metadata and controls
280 lines (223 loc) · 6.29 KB
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* ApplicationMonitor.cpp
* ApplicationMonitor
*
* Main code for ApplicationMonitor. A simple module which triggers events
* when programs start or stop.
*
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
#include "ApplicationMonitor.h"
// Module entry point
BOOL APIENTRY DllMain(HINSTANCE hInst, DWORD dwReason, LPVOID /* pvReserved */)
{
if (dwReason == DLL_PROCESS_ATTACH)
{
DisableThreadLibraryCalls(hInst);
}
return TRUE;
}
// Called on module load
int initModuleW(HWND hwndParent, HINSTANCE hDllInstance, LPCWSTR szPath)
{
g_hParent = hwndParent;
g_hInstance = hDllInstance;
StringCchCopy(g_szPath, _countof(g_szPath), szPath);
if (!CreateMessageHandler())
{
return 1;
}
AddBangCommand(_T("!ApplicationMonitor"), BangApplicationMonitor);
LoadConfig();
return 0;
}
// Called on module unload
void quitModule(HINSTANCE hDllInstance)
{
RemoveBangCommand(_T("!ApplicationMonitor"));
if (g_hwndMessageHandler)
{
SendMessage(GetLitestepWnd(), LM_UNREGISTERMESSAGE, (WPARAM)g_hwndMessageHandler, (LPARAM)g_lsMessages);
DestroyWindow(g_hwndMessageHandler);
}
UnregisterClass(g_szMsgHandler, hDllInstance);
}
// Loads all the .RC configurations
void LoadConfig()
{
LPVOID f = LCOpen(NULL);
TCHAR szLine[MAX_LINE_LENGTH];
while (LCReadNextConfig(f, _T("*ApplicationMonitor"), szLine, _countof(szLine)))
{
ParseConfigLine(szLine);
}
LCClose(f);
}
//
void BangApplicationMonitor(HWND, LPCTSTR pszArgs)
{
ParseConfigLine(pszArgs);
}
//
void ParseConfigLine(LPCTSTR szLine)
{
TCHAR szTimer[MAX_LINE_LENGTH];
LPCTSTR pszNext = szLine;
MonitorData monData;
monData.iUpdateFrequency = 100;
// Retrive information from the line
GetToken(pszNext, monData.szName, &pszNext, false);
GetToken(pszNext, monData.szName, &pszNext, false);
if (*monData.szName == _T('\0'))
{
return;
}
GetToken(pszNext, monData.szApplication, &pszNext, false);
if (*monData.szApplication == _T('\0'))
{
return;
}
if (GetToken(pszNext, szTimer, &pszNext, false))
{
if (*szTimer != _T('\0'))
{
monData.iUpdateFrequency = _ttoi(szTimer);
}
}
// Keep the timer within resonable bounds
if (monData.iUpdateFrequency > USER_TIMER_MAXIMUM) monData.iUpdateFrequency = USER_TIMER_MAXIMUM;
if (monData.iUpdateFrequency < USER_TIMER_MINIMUM) monData.iUpdateFrequency = USER_TIMER_MINIMUM;
// TODO::Avoid duplicates!
// Find a free timer id
UINT id = 0;
MonitorMap::iterator iter;
do
{
iter = g_MonitorMap.find(++id);
}
while (iter != g_MonitorMap.end());
// Set the timer
SetTimer(g_hwndMessageHandler, id, monData.iUpdateFrequency, NULL);
// Load events from the RCs
GetPrefixedRCLine(monData.szOnStart, monData.szName, _T("OnStart"), _T(""));
GetPrefixedRCLine(monData.szOnEnd, monData.szName, _T("OnEnd"), _T(""));
// Store all information in the MonitorMap
g_MonitorMap.insert(MonitorMap::value_type(id, monData));
// Initialize all values (avoids events fireing on startup/recycle)
Check(id, true);
}
// Check if the application is running
void Check(UINT id, bool bInitialize)
{
MonitorMap::iterator iter = g_MonitorMap.find(id);
if (iter != g_MonitorMap.end())
{
bool bInitialState = iter->second.bIsRunning;
iter->second.bIsRunning = AppIsRunning(iter->second.szApplication);
if (bInitialize)
{
SetEvar(iter->second.szName, _T("IsRunning"), _T("%s"), iter->second.bIsRunning ? _T("true") : _T("false"));
}
if (bInitialState != iter->second.bIsRunning )
{
SetEvar(iter->second.szName, _T("IsRunning"), _T("%s"), iter->second.bIsRunning ? _T("true") : _T("false"));
if (!bInitialize)
{
if (iter->second.bIsRunning)
{
LSExecute(NULL, iter->second.szOnStart, SW_SHOWNORMAL);
}
else
{
LSExecute(NULL, iter->second.szOnEnd, SW_SHOWNORMAL);
}
}
}
}
}
// Check whether or not an application is running
bool AppIsRunning(LPCTSTR szApp)
{
bool bReturn = false;
HANDLE hSnapShot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
PROCESSENTRY32* processInfo = new PROCESSENTRY32;
processInfo->dwSize = sizeof(PROCESSENTRY32);
while (Process32Next(hSnapShot, processInfo) != FALSE)
{
if (!_tcscmp(processInfo->szExeFile, szApp))
{
bReturn = true;
break;
}
}
CloseHandle(hSnapShot);
delete processInfo;
return bReturn;
}
// Sets an evironment variable
void SetEvar(LPCTSTR pszName, LPCTSTR pszEvar, LPCTSTR pszFormat, ...)
{
TCHAR szValue[MAX_LINE_LENGTH] = { 0 };
TCHAR szEvar[MAX_LINE_LENGTH] = { 0 };
va_list argList;
va_start(argList, pszFormat);
StringCchVPrintf(szValue, MAX_LINE_LENGTH, pszFormat, argList);
va_end(argList);
StringCchPrintf(szEvar, sizeof(szEvar), _T("%s%s"), pszName, pszEvar);
LSSetVariable(szEvar, szValue);
}
// Retrive a prefixed line from the RCs
void GetPrefixedRCLine(LPTSTR szDest, LPCTSTR szPrefix, LPCTSTR szOption, LPCTSTR szDefault)
{
TCHAR szOptionName[MAX_LINE_LENGTH];
StringCchPrintf(szOptionName, MAX_LINE_LENGTH, _T("%s%s"), szPrefix, szOption);
GetRCLine(szOptionName, szDest, MAX_LINE_LENGTH, szDefault);
}
// Creates the message handler
bool CreateMessageHandler()
{
WNDCLASSEX wc = {0};
wc.cbSize = sizeof(WNDCLASSEX);
wc.lpfnWndProc = MessageHandlerProc;
wc.hInstance = g_hInstance;
wc.lpszClassName = g_szMsgHandler;
wc.hIconSm = 0;
wc.style = CS_NOCLOSE;
if (!RegisterClassEx(&wc))
return false;
g_hwndMessageHandler = CreateWindowEx(WS_EX_TOOLWINDOW, g_szMsgHandler, 0, WS_POPUP, 0, 0, 0, 0, g_hParent, 0, g_hInstance, 0);
if (!g_hwndMessageHandler)
{
return false;
}
SendMessage(GetLitestepWnd(), LM_REGISTERMESSAGE, (WPARAM)g_hwndMessageHandler, (LPARAM)g_lsMessages);
return true;
}
// Message Handler
LRESULT WINAPI MessageHandlerProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
switch(uMsg)
{
case LM_REFRESH:
{
LoadConfig();
return 0;
}
case LM_GETREVID:
{
StringCchPrintf((LPTSTR)lParam, 64, _T("%s: %s"), g_szAppName, g_rcsRevision);
size_t uLength;
if (SUCCEEDED(StringCchLength((LPTSTR) lParam, 64, &uLength)))
{
return uLength;
}
lParam = NULL;
return 0;
}
case WM_TIMER:
{
Check((UINT)wParam);
return 0;
}
}
return DefWindowProc(hWnd, uMsg, wParam, lParam);
}