Skip to content

Instantly share code, notes, and snippets.

@Richienb
Created January 3, 2020 11:55
Show Gist options
  • Save Richienb/eb1b7467b5705a70e6f10fd53e0b4fc8 to your computer and use it in GitHub Desktop.
Save Richienb/eb1b7467b5705a70e6f10fd53e0b4fc8 to your computer and use it in GitHub Desktop.
Get the currently open windows in C++ - Node.js addon
#include <node.h>
#include <windows.h>
#include <iostream>
#include <list>
#include <iterator>
using namespace std;
namespace winds {
using namespace v8;
void Method(const FunctionCallbackInfo<Value>&args) {
Isolate* isolate = args.GetIsolate();
Local<Array> windows = Array::New(isolate);
for (HWND window = GetTopWindow(NULL); window != NULL; window = GetNextWindow(window, GW_HWNDNEXT)) {
if (!IsWindowVisible(window)) continue;
int length = GetWindowTextLength(window);
if (length == 0) continue;
char* title = new char[length+1];
GetWindowText(window, title, length+1);
RECT rect;
if (GetWindowRect(window, &rect)) {
int width = rect.right - rect.left;
int height = rect.bottom - rect.top;
int minSize = 50;
if (height < minSize) continue;
if (width < minSize) continue;
if (title == "Program Manager") continue;
Local<String> windowName = String::NewFromUtf8(isolate, title);
windows->Set(windows->Length(), windowName);
}
}
args.GetReturnValue().Set(windows);
}
void Initialize(Local<Object> exports) {
NODE_SET_METHOD(exports, "getwinds", Method);
}
NODE_MODULE(NODE_GYP_MODULE_NAME, Initialize);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment