WorkflowsByLeo/Src/Steps/RunProcess.mqh

106 lines
3.7 KiB
MQL5
Raw Permalink Normal View History

2026-04-05 16:54:36 -05:00
//+------------------------------------------------------------------+
//| RunProcess.mqh |
//| Copyright 2025, Niquel Mendoza. |
//| https://www.mql5.com/es/users/nique_372 |
//+------------------------------------------------------------------+
#property copyright "Copyright 2025, Niquel Mendoza."
#property link "https://www.mql5.com/es/users/nique_372"
#property strict
#ifndef WORKFLOWSBYLEO_STEPS_RUNPROCESS_SYNC_MQH
#define WORKFLOWSBYLEO_STEPS_RUNPROCESS_SYNC_MQH
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
2026-04-05 17:18:23 -05:00
#include <TSN\\ExtraCodes\\Func.mqh>
2026-04-09 06:52:25 -05:00
#include "..\\Final\\Orquestador.mqh"
2026-04-05 16:54:36 -05:00
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
class CWorkStepGenericRunCommand : public CWorkflowStep
{
private:
string m_full_command;
2026-04-05 22:25:22 -05:00
uint m_timeout_ms;
2026-04-05 16:54:36 -05:00
public:
2026-04-07 07:04:20 -05:00
CWorkStepGenericRunCommand(void) : CWorkflowStep("PowerShellCommand", "Generic", false, 0) {}
2026-04-05 16:54:36 -05:00
~CWorkStepGenericRunCommand(void) {}
//---
bool Init(const CYmlNode& parameters) override final;
int Run() override final;
};
2026-04-08 11:55:14 -05:00
2026-04-07 08:55:55 -05:00
//---
2026-04-08 11:55:14 -05:00
WORKFLOWS_FACTORY_DEFINE_CREATOR(CWorkStepGenericRunCommand, GenericRunCommand, "PowerShellCommand", "Generic")
2026-04-05 16:54:36 -05:00
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
bool CWorkStepGenericRunCommand::Init(const CYmlNode& parameters)
{
//---
2026-04-06 09:58:21 -05:00
// AtHomogeo debido a que paraemteres es un ARRAY no obj
2026-04-08 11:55:14 -05:00
string command = m_wf.TranslateStr(parameters["run"], "");
m_timeout_ms = m_wf.TranslateNumber<uint>(parameters["timeout_segundos"], "300") * 1000;
2026-04-05 16:54:36 -05:00
//--- Check
2026-04-05 17:18:23 -05:00
if(command == "")
2026-04-05 16:54:36 -05:00
{
2026-04-06 09:58:21 -05:00
LogError("'run' no puede estar vacio", FUNCION_ACTUAL);
2026-04-05 16:54:36 -05:00
return false;
}
//---
2026-04-05 17:18:23 -05:00
m_full_command = "powershell -NoProfile -NonInteractive -Command \"" + command + "\"";
2026-04-05 16:54:36 -05:00
//---
return true;
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
int CWorkStepGenericRunCommand::Run(void)
{
//--- Varaibels inciales del proceso
PROCESS_INFORMATION pi;
STARTUPINFOW si;
ZeroMemory(si);
si.cb = sizeof(si);
//--- Cracions del proceso
const int created = CreateProcessW(NULL, m_full_command, NULL, NULL, false, 0x08000000, NULL, NULL, si, pi);
if(created == 0)
{
LogError(StringFormat("Fallo CreateProcessW, last err in kernel 32: %u", kernel32::GetLastError()), FUNCION_ACTUAL);
return 1;
}
//--- Esperamos a que termine
2026-04-09 20:49:22 -05:00
const uint wait_result = WaitForSingleObject(pi.hProcess, m_timeout_ms);
if(wait_result == 0x00000102) // WAIT_TIMEOUT
TerminateProcess(pi.hProcess, 1);
2026-04-05 16:54:36 -05:00
//--- Capturamos el codigo de salida
uint exit_code = 0;
GetExitCodeProcess(pi.hProcess, exit_code);
//--- Cerramos handles de proceso e hilo
CloseHandle(pi.hProcess);
CloseHandle(pi.hThread);
//--- En caso de error log y return 1 de fallo
if(exit_code != 0)
{
2026-04-05 17:18:23 -05:00
LogError(StringFormat("Comando fallo exit=%u cmd=%s", exit_code, m_full_command), FUNCION_ACTUAL);
2026-04-05 16:54:36 -05:00
return 1;
}
//--- Exito
return 0;
}
//+------------------------------------------------------------------+
2026-04-05 17:18:23 -05:00
#endif // WORKFLOWSBYLEO_STEPS_RUNPROCESS_SYNC_MQH