-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfocus.cpp
64 lines (51 loc) · 1.75 KB
/
focus.cpp
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
//
// Licence: public domain - unrestricted usage
//
#include <iostream>
#include <windows.h>
#include <psapi.h>
#include <fcntl.h>
#include <io.h>
int main()
{
WCHAR program[MAX_PATH];
DWORD activeProcId = 0;
DWORD lastActiveProcId = 0;
_setmode(_fileno(stdout), _O_U16TEXT);
while (true) {
Sleep(100);
activeProcId = 0;
HWND hWnd = GetForegroundWindow();
if (hWnd != NULL) {
GetWindowThreadProcessId(hWnd, &activeProcId);
if (activeProcId == 0) {
wprintf(L"GetWindowThreadProcessId had error %u\n", GetLastError());
continue;
}
HANDLE hProc = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, activeProcId);
if (hProc == NULL) {
wprintf(L"OpenProcess had error %u\n", GetLastError());
continue;
}
DWORD rc = GetModuleFileNameExW((HMODULE)hProc, NULL, program, MAX_PATH);
if (rc == 0) {
wprintf(L"GetModuleFileNameExW had error %u\n", GetLastError());
CloseHandle(hProc);
continue;
}
CloseHandle(hProc);
}
if (activeProcId != lastActiveProcId) {
WCHAR date[256];
time_t now = time(0);
struct tm t;
localtime_s(&t, &now);
wcsftime(date, sizeof(date)/sizeof(WCHAR), L"%a %d %B %T", &t);
if (activeProcId == 0)
wprintf(L"No foreground application | %s\n", date);
else
wprintf(L"%u:%s | %s\n", activeProcId, program, date);
lastActiveProcId = activeProcId;
}
}
}