Repository navigation
Expand file tree
/
Copy pathNodeExecutor.cpp
More file actions
52 lines (44 loc) · 1.87 KB
/
Copy pathNodeExecutor.cpp
File metadata and controls
52 lines (44 loc) · 1.87 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
#include "NodeExecutor.h"
#include <iostream>
#include <sstream>
#include <windows.h>
NodeExecutor::NodeExecutor(const std::string& nodePath) : nodeExePath(nodePath) {}
bool NodeExecutor::executeScript(const std::string& scriptPath) {
// Ensure monitorProcess.js is running
if (!isScriptRunning("monitorProcess.js")) {
std::cout << "Starting monitorProcess.js..." << std::endl;
if (!launchProcess("\"" + nodeExePath + "\" \"monitorProcess.js\"")) {
std::cerr << "Failed to start monitorProcess.js." << std::endl;
return false;
}
Sleep(2000); // Allow initialization
}
else {
std::cout << "monitorProcess.js is already running." << std::endl;
}
// Execute get_pid.js
std::cout << "Executing get_pid.js..." << std::endl;
if (!launchProcess("\"" + nodeExePath + "\" \"get_pid.js\"")) {
std::cerr << "Failed to execute get_pid.js." << std::endl;
return false;
}
// Execute Powershell script (launchArmageddon.js)
std::cout << "Executing launchArmageddon.js..." << std::endl;
return launchProcess("\"" + nodeExePath + "\" \"launchArmageddon.js\"");
}
bool NodeExecutor::launchProcess(const std::string& command) {
STARTUPINFOA si = { sizeof(STARTUPINFOA) };
PROCESS_INFORMATION pi = { 0 };
if (!CreateProcessA(nullptr, const_cast<char*>(command.c_str()), nullptr, nullptr, FALSE, CREATE_NO_WINDOW, nullptr, nullptr, &si, &pi)) {
std::cerr << "Failed to execute Node.js script. Error: " << GetLastError() << std::endl;
return false;
}
CloseHandle(pi.hProcess);
CloseHandle(pi.hThread);
return true;
}
bool NodeExecutor::isScriptRunning(const std::string& scriptName) {
std::string command = "tasklist /FI \"IMAGENAME eq node.exe\" /FO CSV | findstr " + scriptName;
int result = system(command.c_str());
return (result == 0);
}