-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathmain.cpp
More file actions
635 lines (560 loc) · 14.7 KB
/
Copy pathmain.cpp
File metadata and controls
635 lines (560 loc) · 14.7 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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
/*
* main.cpp
*
* SimMgr applicatiopn
*
* This file is part of the sim-mgr distribution.
*
* Copyright (c) 2019-2021 VetSim, Cornell University College of Veterinary Medicine Ithaca, NY
* Copyright (c) 2022-2025 ITown Design, Ithaca, NY
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, version 3.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
//#include <WinSDKVer.h>
#define _WIN32_WINNT _WIN32_WINNT_MAXVER
//#include "version.h"
int checkProcessRunning(void);
#define OTHER_SCREEN 1
#define NO_SCREEN 0
#include "vetsim.h"
char WVSversion[STR_SIZE];
HFONT hLogFont;
__int64
getBuildDate(void)
{
char date[] = __DATE__; // "Mmm dd yyyy"
char time[] = __TIME__; // "hh:mm:ss"
uint64_t dcode;
uint64_t month;
printf("Build Date %s %s ", date, time);
uint64_t year = atoi(&date[7]);
if (strncmp(&date[0], "Jan", 3) == 0)
{
month = 1;
}
else if (strncmp(&date[0], "Feb", 3) == 0)
{
month = 2;
}
else if (strncmp(&date[0], "Mar", 3) == 0)
{
month = 3;
}
else if (strncmp(&date[0], "Apr", 3) == 0)
{
month = 4;
}
else if (strncmp(&date[0], "May", 3) == 0)
{
month = 5;
}
else if (strncmp(&date[0], "Jun", 3) == 0)
{
month = 6;
}
else if (strncmp(&date[0], "Jul", 3) == 0)
{
month = 7;
}
else if (strncmp(&date[0], "Aug", 3) == 0)
{
month = 8;
}
else if (strncmp(&date[0], "Sep", 3) == 0)
{
month = 9;
}
else if (strncmp(&date[0], "Oct", 3) == 0)
{
month = 10;
}
else if (strncmp(&date[0], "Nov", 3) == 0)
{
month = 11;
}
else if (strncmp(&date[0], "Dec", 3) == 0)
{
month = 12;
}
else
{
month = 0;
}
date[6] = 0;
uint64_t day = atoi(&date[4]);
time[2] = 0;
uint64_t hour = atoi(time);
printf("%Iu %Iu %Iu %Iu\n", year, month, day, hour);
dcode = year * 1000000;
dcode += month * 10000;
dcode += day * 100;
dcode += hour;
return dcode;
}
void
setWVSVersion(void)
{
sprintf_s(WVSversion, STR_SIZE, "%d.%d.%lld", SIMMGR_VERSION_MAJ, SIMMGR_VERSION_MIN, getBuildDate());
}
#ifdef NDEBUG
// Windows Header Files
//#include <stdlib.h>
//#include <string.h>
//#include <tchar.h>
#include <strsafe.h>
#include <afxwin.h>
#include <string>
//#include <tlhelp32.h>
// C RunTime Header Files
//#include <malloc.h>
//#include <memory.h>
#include <shellapi.h>
#ifdef _UNICODE
typedef wchar_t TCHAR;
#else
typedef char TCHAR;
#endif // _UNICODE
int vetsim(void);
#define MAX_LOADSTRING 100
// Global Variables:
HINSTANCE hInst; // current instance
static TCHAR szWindowClass[] = _T("DesktopApp"); // the main window class name
static TCHAR szTitle[] = _T("WinVetSim"); // The title bar text
static HINSTANCE ghInstance = NULL;
// Forward declarations of functions included in this code module:
//ATOM MyRegisterClass(HINSTANCE hInstance);
//BOOL InitInstance(HINSTANCE, int);
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
INT_PTR CALLBACK About(HWND, UINT, WPARAM, LPARAM);
void ErrorExit(LPCTSTR lpszFunction);
HWND mainWindow;
//using namespace System::Windows::Forms;
int APIENTRY wWinMain(_In_ HINSTANCE hInstance,
_In_opt_ HINSTANCE hPrevInstance,
_In_ LPWSTR lpCmdLine,
_In_ int nCmdShow)
{
//UNREFERENCED_PARAMETER(hPrevInstance);
int sts;
MSG msg;
BOOL bRet;
WNDCLASS wc;
sts = checkProcessRunning();
if (sts == 0)
{
MessageBox(0, L"WinVetSim process not found", L"Error!", MB_ICONSTOP | MB_OK);
exit(-1);
}
if (sts != 1)
{
MessageBox(0, L"An instance of WinVetSim is already running.", L"Error!", MB_ICONSTOP | MB_OK);
exit(-1);
}
setWVSVersion();
initializeConfiguration();
#if NO_SCREEN != 1
if (!hPrevInstance)
{
wc.style = 0;
wc.lpfnWndProc = (WNDPROC)WndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = LoadIcon((HINSTANCE)NULL, IDI_APPLICATION);
wc.hCursor = LoadCursor((HINSTANCE)NULL, IDC_ARROW);
wc.hbrBackground = reinterpret_cast<HBRUSH>(COLOR_WINDOW + 1); ; // GetStockObject(WHITE_BRUSH);
wc.lpszMenuName = L"MainMenu";
wc.lpszClassName = L"MainWndClass";
if (!RegisterClass(&wc))
return FALSE;
}
ghInstance = hInstance;
WNDCLASSEX wcex;
memset((void*)&wcex, 0, sizeof(WNDCLASSEX));
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = WndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hInstance;
wcex.hIcon = LoadIcon(hInstance, IDI_APPLICATION);
wcex.hIconSm = LoadIcon(hInstance, IDI_APPLICATION);
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
wcex.lpszMenuName = L"WinVetSim Menu"; // NULL;
wcex.lpszClassName = szWindowClass;
wcex.hIconSm = LoadIcon(wcex.hInstance, IDI_APPLICATION);
if (!RegisterClassEx(&wcex))
{
MessageBox(0, L"Window Registration Failed!", L"Error!", MB_ICONSTOP | MB_OK);
return 1;
}
// The parameters to CreateWindow explained:
// szWindowClass: the name of the application
// szTitle: the text that appears in the title bar
// WS_OVERLAPPEDWINDOW: the type of window to create
// CW_USEDEFAULT, CW_USEDEFAULT: initial position (x, y)
// 500, 100: initial size (width, length)
// NULL: the parent of this window
// NULL: this application does not have a menu bar
// hInstance: the first parameter from WinMain
// NULL: not used in this application
HWND hWnd = CreateWindowEx(
WS_EX_CLIENTEDGE,
szWindowClass,
szTitle,
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT,
700, 500,
NULL, NULL,
hInstance,
NULL );
if (!hWnd)
{
MessageBox(0, L"Window Creation Failed!", L"Error!", MB_ICONSTOP | MB_OK);
return 1;
}
// The parameters to ShowWindow explained:
// hWnd: the value returned from CreateWindow
// nCmdShow: the fourth parameter from WinMain
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);
mainWindow = hWnd;
#endif
//CMenu menu;
//ASSERT(menu.LoadMenu(IDR_MENU1));
//CMenu* pPopup = menu.GetSubMenu(0);
//ASSERT(pPopup != NULL);
//pPopup->TrackPopupMenu(TPM_LEFTALIGN | TPM_RIGHTBUTTON, 0, 0, AfxGetMainWnd());
(void)start_task("VetSim", vetsim );
while ( (bRet = GetMessage(&msg, NULL, 0, 0)) != 0 )
{
if (bRet == -1)
{
// handle the error and possibly exit
}
else
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
return (int)msg.wParam;
}
// FUNCTION: WndProc(HWND, UINT, WPARAM, LPARAM)
//
// PURPOSE: Processes messages for the main window.
//
// WM_PAINT - Paint the main window
// WM_DESTROY - post a quit message and return
HWND hButton, hCombo, hEdit, hList, hScroll, hStatic;
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
PAINTSTRUCT ps;
HDC hdc;
TCHAR greeting[] = _T("Open VetSim Simulator System");
TCHAR leaving[] = _T("Closing WinVetSim Server");
TCHAR buffer[128] = { 0, };
switch (message)
{
case WM_CREATE:
// Scrolling log window — fills the client area below the header text
hEdit = CreateWindowEx(
WS_EX_CLIENTEDGE,
L"EDIT",
NULL,
WS_CHILD | WS_VISIBLE | WS_VSCROLL |
ES_LEFT | ES_MULTILINE | ES_AUTOVSCROLL | ES_READONLY,
5, 75, // x, y — below the three header lines
680, 380, // initial size; resized dynamically in WM_SIZE
hWnd,
NULL,
ghInstance,
NULL
);
if (hEdit)
{
// Use Consolas (monospace) for clean log output
hLogFont = CreateFontW(
-14, 0, 0, 0,
FW_NORMAL,
FALSE, FALSE, FALSE,
DEFAULT_CHARSET,
OUT_DEFAULT_PRECIS,
CLIP_DEFAULT_PRECIS,
CLEARTYPE_QUALITY,
FIXED_PITCH | FF_MODERN,
L"Consolas"
);
if (hLogFont)
SendMessageW(hEdit, WM_SETFONT, (WPARAM)hLogFont, TRUE);
}
break;
case WM_SIZE:
// Keep the log edit control filling the client area below the header
if (hEdit)
{
int clientW = LOWORD(lParam);
int clientH = HIWORD(lParam);
SetWindowPos(hEdit, NULL,
5, 75,
clientW - 10, clientH - 80,
SWP_NOZORDER);
}
break;
case WM_APP_LOG:
{
// Delivered by append_text_to_edit() from any thread via PostMessageW.
// We are on the UI thread here, so all SendMessageW calls to hEdit are safe.
// lParam is a wchar_t* allocated with _wcsdup(); we must free() it.
const wchar_t* text = reinterpret_cast<const wchar_t*>(lParam);
if (text && hEdit)
{
// Append the line with CRLF using EM_REPLACESEL — O(1)
int len = GetWindowTextLengthW(hEdit);
SendMessageW(hEdit, EM_SETSEL, (WPARAM)len, (LPARAM)len);
std::wstring line = text;
line += L"\r\n";
SendMessageW(hEdit, EM_REPLACESEL, FALSE, (LPARAM)line.c_str());
SendMessageW(hEdit, EM_SCROLLCARET, 0, 0);
// Cap line count to prevent unbounded memory growth
int lineCount = (int)SendMessageW(hEdit, EM_GETLINECOUNT, 0, 0);
if (lineCount > LOG_WINDOW_MAX_LINES)
{
int firstChar = (int)SendMessageW(hEdit, EM_LINEINDEX, 0, 0);
int lastChar = (int)SendMessageW(hEdit, EM_LINEINDEX, LOG_WINDOW_TRIM_LINES, 0);
SendMessageW(hEdit, EM_SETSEL, (WPARAM)firstChar, (LPARAM)lastChar);
SendMessageW(hEdit, EM_REPLACESEL, FALSE, (LPARAM)L"");
len = GetWindowTextLengthW(hEdit);
SendMessageW(hEdit, EM_SETSEL, (WPARAM)len, (LPARAM)len);
SendMessageW(hEdit, EM_SCROLLCARET, 0, 0);
}
}
free((void*)text);
return 0;
}
case WM_PAINT:
hdc = BeginPaint(hWnd, &ps);
// Print greeting in the top left corner.
TextOut(hdc,
10,
10,
greeting,
(int)_tcslen(greeting));
swprintf_s(buffer, L"SimMgr Version %hs\n", WVSversion );
TextOut(hdc,
10,
30,
buffer,
(int)_tcslen(buffer));
if (PHP_SERVER_PORT == 80)
{
swprintf_s(buffer, L"Control URL: http://%hs/sim-ii/ii.php\n", PHP_SERVER_ADDR);
}
else
{
swprintf_s(buffer, L"Control URL: http://%hs:%d/sim-ii/ii.php\n", PHP_SERVER_ADDR, PHP_SERVER_PORT);
}
TextOut(hdc,
10,
50,
buffer,
(int)_tcslen(buffer));
// End application-specific layout section.
EndPaint(hWnd, &ps);
break;
case WM_DESTROY:
stopPHPServer();
DeleteObject(hLogFont); // free GDI resource before the message loop exits
PostQuitMessage(0);
break;
case WM_CLOSE:
DestroyWindow(hWnd);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
break;
}
return 0;
}
void ErrorExit(LPCTSTR lpszFunction)
{
// Retrieve the system error message for the last-error code
LPVOID lpMsgBuf;
LPVOID lpDisplayBuf;
DWORD dw = GetLastError();
FormatMessage(
FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS,
NULL,
dw,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
(LPTSTR)&lpMsgBuf,
0, NULL);
// Display the error message and exit the process
lpDisplayBuf = (LPVOID)LocalAlloc(LMEM_ZEROINIT,
(lstrlen((LPCTSTR)lpMsgBuf) + lstrlen((LPCTSTR)lpszFunction) + 40) * sizeof(TCHAR));
StringCchPrintf((LPTSTR)lpDisplayBuf,
LocalSize(lpDisplayBuf) / sizeof(TCHAR),
TEXT("%s failed with error %d: %s"),
lpszFunction, dw, lpMsgBuf);
MessageBox(NULL, (LPCTSTR)lpDisplayBuf, TEXT("Error"), MB_OK);
if ( lpMsgBuf != NULL ) LocalFree(lpMsgBuf);
if ( lpDisplayBuf != NULL ) LocalFree(lpDisplayBuf);
ExitProcess(dw);
/*
argv = CommandLineToArgvW(GetCommandLine(), &argc);
if (argc > 1)
{
for (i = 1; i < argc; i++)
{
if (wcsncmp(argv[i], L"-v", 2) == 0 || wcsncmp(argv[i], L"-V", 2) == 0 ||
wcsncmp(argv[i], L"\\v", 2) == 0 || wcsncmp(argv[i], L"\\V", 2) == 0 ||
wcsncmp(argv[i], L"/v", 2) == 0 || wcsncmp(argv[i], L"/V", 2) == 0 ||
wcsncmp(argv[i], L"--version", 9) == 0 || wcsncmp(argv[i], L"--Version", 9) == 0)
{
ptr = argv[0];
size_t c = 0;
while (c < wcslen(argv[0]))
{
if (argv[0][c] == '\\')
{
ptr = &argv[0][c + 1];
}
c++;
}
// hdr
printf("%S: SimMgr %s\n", ptr, WVSversion );
exit(0);
}
else
{
printf("Unrecognized argumment: \"%S\"\n", argv[i]);
exit(-1);
}
}
}
vetsim();
return 0;
*/
}
#else
#include "vetsim.h"
using namespace std;
int main(int argc, char *argv[] )
{
int i;
char *ptr;
int sts;
sts = checkProcessRunning();
if (sts == 0)
{
MessageBox(0, L"WinVetSim process not found", L"Error!", MB_ICONSTOP | MB_OK);
exit(-1);
}
if (sts != 1)
{
MessageBox(0, L"An instance of WinVetSim is already running.", L"Error!", MB_ICONSTOP | MB_OK);
exit(-1);
}
setWVSVersion();
initializeConfiguration();
if (argc > 1)
{
for (i = 1; i < argc; i++)
{
if (strncmp(argv[i], "-v", 2) == 0 || strncmp(argv[i], "-V", 2) == 0 ||
strncmp(argv[i], "\\v", 2) == 0 || strncmp(argv[i], "\\V", 2) == 0 ||
strncmp(argv[i], "/v", 2) == 0 || strncmp(argv[i], "/V", 2) == 0 ||
strncmp(argv[i], "--version", 9) == 0 || strncmp(argv[i], "--Version", 9) == 0)
{
ptr = argv[0];
size_t c = 0;
while (c < strlen(argv[0]))
{
if (argv[0][c] == '\\')
{
ptr = &argv[0][c + 1];
}
c++;
}
printf("%s: SimMgr %s\n", ptr, WVSversion);
exit(0);
}
else
{
printf("Unrecognized argumment: \"%s\"\n", argv[i]);
exit(-1);
}
}
}
printf("SimMgr %s\n", WVSversion);
vetsim();
return 0;
}
#endif
#include <comdef.h>
int checkProcessRunning(void)
{
PROCESSENTRY32 entry;
entry.dwSize = sizeof(PROCESSENTRY32);
int count = 0;
HANDLE snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, NULL);
if (Process32First(snapshot, &entry) == TRUE)
{
while (Process32Next(snapshot, &entry) == TRUE)
{
_bstr_t b(entry.szExeFile);
const char* c = b;
if (_stricmp(c, "WinVetSim.exe") == 0)
{
//HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, entry.th32ProcessID);
// Do stuff..
// CloseHandle(hProcess);
count++;
}
}
}
CloseHandle(snapshot);
return count;
}
void
initializeConfiguration(void)
{
// Set configurable parameters to defaults
localConfig.port_pulse = DEFAULT_PORT_PULSE;
localConfig.port_status = DEFAULT_PORT_STATUS;
localConfig.php_server_port = DEFAULT_PHP_SERVER_PORT;
sprintf_s(localConfig.php_server_addr, "%s", DEFAULT_PHP_SERVER_ADDRESS);
sprintf_s(localConfig.log_name, "%s", DEFAULT_LOG_NAME);
//char publicPath[64];
const char htmlPath[32] = DEFAULT_HTML_PATH;
// char* pathp = &publicPath;
char* pValue;
size_t pathl;
errno_t et;
char errorstr[256];
errno_t err = _dupenv_s(&pValue, &pathl, "PUBLIC");
if (err)
{
et = strerror_s(errorstr, errno);
printf("_dupenv_s: %s\n", errorstr );
return;
}
sprintf_s(localConfig.html_path, "%s\\%s", pValue, htmlPath);
free(pValue);
printf("Default html path is %s\n", localConfig.html_path);
// Allow parameters to be overridedn from registry
getKeys();
}