installed pty
[VSoRC/.git] / node_modules / node-pty / src / win / conpty_console_list.cc
1 /**
2  * Copyright (c) 2019, Microsoft Corporation (MIT License).
3  */
4
5 #include <nan.h>
6 #include <windows.h>
7
8 static NAN_METHOD(ApiConsoleProcessList) {
9   if (info.Length() != 1 ||
10       !info[0]->IsNumber()) {
11     Nan::ThrowError("Usage: getConsoleProcessList(shellPid)");
12     return;
13   }
14
15   const SHORT pid = info[0]->Uint32Value(Nan::GetCurrentContext()).FromJust();
16
17   if (!FreeConsole()) {
18     Nan::ThrowError("FreeConsole failed");
19   }
20   if (!AttachConsole(pid)) {
21     Nan::ThrowError("AttachConsole failed");
22   }
23   auto processList = std::vector<DWORD>(64);
24   auto processCount = GetConsoleProcessList(&processList[0], processList.size());
25   if (processList.size() < processCount) {
26       processList.resize(processCount);
27       processCount = GetConsoleProcessList(&processList[0], processList.size());
28   }
29   FreeConsole();
30
31   v8::Local<v8::Array> result = Nan::New<v8::Array>();
32   for (DWORD i = 0; i < processCount; i++) {
33     Nan::Set(result, i, Nan::New<v8::Number>(processList[i]));
34   }
35   info.GetReturnValue().Set(result);
36 }
37
38 extern "C" void init(v8::Local<v8::Object> target) {
39   Nan::HandleScope scope;
40   Nan::SetMethod(target, "getConsoleProcessList", ApiConsoleProcessList);
41 };
42
43 NODE_MODULE(pty, init);