-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathLog.cpp
More file actions
103 lines (84 loc) · 2.14 KB
/
Log.cpp
File metadata and controls
103 lines (84 loc) · 2.14 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
#include "Log.h"
send_notepad::send_notepad()
{
m_create = FALSE;
m_hwnd = 0;
m_pi = { 0 };
}
send_notepad::~send_notepad()
{
if (m_pi.dwProcessId && m_pi.hProcess)
{
TerminateProcess(m_pi.hProcess, 0);
}
}
bool send_notepad::DoLogV(const char* fmt, va_list vargs)
{
char varbuf[2048] = { 0 };
char message[4096] = { 0 };
char timebuf[256] = { 0 };
// Format message time
auto t = std::time(nullptr);
tm stm;
localtime_s(&stm, &t);
std::strftime(timebuf, _countof(timebuf), "%Y-%m-%d %H:%M:%S", &stm);
// Format messages
vsprintf_s(varbuf, _countof(varbuf), fmt, vargs);
sprintf_s(message, _countof(message), "%s:%s", timebuf, varbuf);
SendMessageA(m_hwnd, EM_REPLACESEL, 0, (LPARAM)message);
return true;
}
VOID send_notepad::init()
{
if(m_create) return;
WCHAR szDirectory[MAX_PATH] = { 0 };
GetSystemDirectoryW(szDirectory, MAX_PATH);
wcscat_s(szDirectory, L"\\notepad.exe");
STARTUPINFOW si = { sizeof(si) };
HWND hwnd = NULL;
hwnd = FindWindowW(L"Notepad", NULL);
if (hwnd)
{
m_hwnd = FindWindowEx(hwnd, NULL, TEXT("Edit"), NULL);
if (m_hwnd)
{
m_create = TRUE;
}
}
if (!m_hwnd)
{
if (CreateProcessW(NULL, szDirectory, NULL, NULL, FALSE, 0, NULL, NULL, &si, &m_pi))
{
Sleep(1000);
hwnd = FindWindowW(L"Notepad", NULL);
if (hwnd)
{
m_hwnd = FindWindowEx(hwnd, NULL, TEXT("Edit"), NULL);
if (m_hwnd)
{
m_create = TRUE;
}
}
}
}
return VOID();
}
VOID send_notepad::send(char* szText, ...)
{
if (m_create)
{
va_list alist;
bool result = false;
va_start(alist, szText);
result = DoLogV(szText, alist);
va_end(alist);
}
return VOID();
}
VOID send_notepad::clear()
{
if (m_create) {
SendMessageA(m_hwnd, WM_SETTEXT, 0, NULL);
}
return VOID();
}