-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutil.cpp
More file actions
executable file
·218 lines (181 loc) · 5.98 KB
/
Copy pathutil.cpp
File metadata and controls
executable file
·218 lines (181 loc) · 5.98 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
/**@file util.cpp
*
* This is part of keylogger, demo project to hook key event of a process
* and do periodic screenshoot of screen.
*
* @author Oky Firmansyah <mail@okyfirmansyah.net>.
*
* @date Created : Apr 03, 2017 okyfirmansyah
*/
#include <tchar.h>
#include <windows.h>
#include <winbase.h>
#include <string>
#include <functional>
#include <tlhelp32.h>
#include <memory>
#include <atomic>
#include "log.h"
#include <time.h>
#include <ole2.h>
#include <olectl.h>
/** implementation of some helper functions
*/
using namespace std;
/** used by getHwndFromProcessId
*/
static BOOL CALLBACK getHwndFromProcessIdCB(HWND hwnd, LPARAM lParam)
{
std::function<BOOL(HWND)>* func=reinterpret_cast<std::function<BOOL(HWND)>*>(lParam);
return (*func)(hwnd);
}
/** get HWND from a PID
*/
HWND getHwndFromProcessId(DWORD processId)
{
HWND gHwnd;
//child routine
std::function<BOOL(HWND)> func=[&processId, &gHwnd]
(HWND hwnd)->BOOL
{
DWORD lpdwProcessId;
GetWindowThreadProcessId(hwnd,&lpdwProcessId);
if(lpdwProcessId==processId)
{
gHwnd=hwnd;
return FALSE;
}
return TRUE;
};
//iterate windows, each will call func()
EnumWindows(getHwndFromProcessIdCB, (LPARAM)&func);
return gHwnd;
}
/** find first process that having name procName,
* if found, execute func(entry) and stop
*/
void iterateProcess(const std::function<bool(PROCESSENTRY32* entry)>& func)
{
PROCESSENTRY32 entry;
entry.dwSize = sizeof(PROCESSENTRY32);
HANDLE snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
if (Process32First(snapshot, &entry) == TRUE)
{
while (Process32Next(snapshot, &entry) == TRUE)
{
if(func(&entry)==true)
break;
}
}
CloseHandle(snapshot);
}
/** find first process that having name procName,
* if found, execute func(entry) and stop
*/
bool findFirstProcess(string procName, const std::function<void(PROCESSENTRY32*entry)>& func)
{
bool found=false;
iterateProcess([&found,&func,&procName](PROCESSENTRY32*entry)->bool
{
if (procName.compare(entry->szExeFile) == 0)
{
found=true;
func(entry);
return true;//break here
}
return false;
});
return found;
}
/** find process that having name procName,
* each encounter will call func(entry)
*/
bool findProcess(string procName, const std::function<void(PROCESSENTRY32*entry)>& func)
{
bool found=false;
iterateProcess([&found,&func,&procName](PROCESSENTRY32*entry)->bool
{
if (procName.compare(entry->szExeFile) == 0)
{
found=true;
func(entry);
}
return false;//always look for next process, even if already found
});
return found;
}
/** dump HBITMA to bmp file(save .jpg for later)
*/
bool saveBitmap(string filename, HBITMAP bmp, HPALETTE pal)
{
bool result = false;
PICTDESC pd;
pd.cbSizeofstruct = sizeof(PICTDESC);
pd.picType = PICTYPE_BITMAP;
pd.bmp.hbitmap = bmp;
pd.bmp.hpal = pal;
LPPICTURE picture;
HRESULT res = OleCreatePictureIndirect(&pd, IID_IPicture, false,
reinterpret_cast<void**>(&picture));
if (!SUCCEEDED(res))
return false;
LPSTREAM stream;
res = CreateStreamOnHGlobal(0, true, &stream);
if (!SUCCEEDED(res))
{
picture->Release();
return false;
}
LONG bytes_streamed;
res = picture->SaveAsFile(stream, true, &bytes_streamed);
HANDLE file = CreateFile(filename.c_str(), GENERIC_WRITE, FILE_SHARE_READ, 0,
CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0);
if (!SUCCEEDED(res) || !file)
{
stream->Release();
picture->Release();
return false;
}
HGLOBAL mem = 0;
GetHGlobalFromStream(stream, &mem);
LPVOID data = GlobalLock(mem);
DWORD bytes_written;
result = !!WriteFile(file, data, bytes_streamed, &bytes_written, 0);
result &= (bytes_written == static_cast<DWORD>(bytes_streamed));
GlobalUnlock(mem);
CloseHandle(file);
stream->Release();
picture->Release();
return result;
}
/** take screenshot, save it to the path\YYYYMMDD_HHMMSS.bmp
*/
bool takeScreenShot(string path)
{
// set filename
time_t timer;
char buffer[100];
time(&timer);
struct tm* timeinfo = localtime (&timer);
//strftime (buffer,sizeof(buffer),"[%F %R]",timeinfo);
snprintf(buffer, sizeof(buffer),"%.4d%.2d%.2d_%.2d%.2d%.2d.bmp", 1900+timeinfo->tm_year,
1+timeinfo->tm_mon,
timeinfo->tm_mday,
timeinfo->tm_hour,
timeinfo->tm_min,
timeinfo->tm_sec);
string filename=path+buffer;
HDC hdcSource = GetDC(NULL);
HDC hdcMemory = CreateCompatibleDC(hdcSource);
int width = GetDeviceCaps(hdcSource, HORZRES);
int height = GetDeviceCaps(hdcSource, VERTRES);
HBITMAP hBitmap = CreateCompatibleBitmap(hdcSource, width, height);
HBITMAP hBitmapOld = (HBITMAP)SelectObject(hdcMemory, hBitmap);
BitBlt(hdcMemory, 0, 0, width, height, hdcSource, 0, 0, SRCCOPY);
hBitmap = (HBITMAP)SelectObject(hdcMemory, hBitmapOld);
DeleteDC(hdcSource);
DeleteDC(hdcMemory);
HPALETTE hpal = NULL;
if(saveBitmap(filename, hBitmap, hpal)) return true;
return false;
}