-
Notifications
You must be signed in to change notification settings - Fork 0
/
getActiveProgram.cpp
84 lines (69 loc) · 1.97 KB
/
getActiveProgram.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#include <stdlib.h>
#include <iostream>
#include <node.h>
#include <windows.h>
#include "libloaderapi.h"
#include <psapi.h>
#include <tchar.h>
#include <stdio.h>
#pragma comment(lib, "psapi.lib")
namespace demo {
using v8::FunctionCallbackInfo;
using v8::Isolate;
using v8::Local;
using v8::Object;
using v8::String;
using v8::Value;
std::string utf8_encode(const std::wstring &wstr) {
if(wstr.empty()) {
return std::string();
}
int size_needed = WideCharToMultiByte(CP_UTF8, 0, &wstr[0], (int)wstr.size(), nullptr, 0, nullptr, nullptr);
std::string strTo(size_needed, 0);
WideCharToMultiByte(CP_UTF8, 0, &wstr[0], (int)wstr.size(), &strTo[0], size_needed, nullptr, nullptr);
return strTo;
}
void GetActiveProgramName(const FunctionCallbackInfo<Value>& args) {
HWND fgWin = GetForegroundWindow();
std::wstring title(GetWindowTextLength(fgWin) + 1, L'\0');
GetWindowTextW(fgWin, &title[0], title.size()); //note: C++11 only
/**
* Return
*/
Isolate* isolate = args.GetIsolate();
args.GetReturnValue().Set(
String::NewFromUtf8(
isolate,
utf8_encode(title).c_str()
).ToLocalChecked()
);
}
void GetActiveProgramExe(const FunctionCallbackInfo<Value>& args) {
TCHAR szAppPath[MAX_PATH];
HWND hFG = GetForegroundWindow();
DWORD dwId;
HANDLE hProc;
GetWindowThreadProcessId(hFG, &dwId);
hProc = OpenProcess(PROCESS_QUERY_LIMITED_INFORMATION, FALSE, dwId);
if (hProc) {
DWORD dwSize = ARRAYSIZE(szAppPath);
if (!QueryFullProcessImageName(hProc, 0, szAppPath, &dwSize))
CloseHandle(hProc);
}
/**
* Return
*/
Isolate* isolate = args.GetIsolate();
args.GetReturnValue().Set(
String::NewFromUtf8(
isolate,
szAppPath
).ToLocalChecked()
);
}
void Initialize(Local<Object> exports) {
NODE_SET_METHOD(exports, "getActiveProgramName", GetActiveProgramName);
NODE_SET_METHOD(exports, "getActiveProgramExe", GetActiveProgramExe);
}
NODE_MODULE(NODE_GYP_MODULE_NAME, Initialize)
} // namespace demo