console output using shellexecuteW

1k Views Asked by At

I'm using ShellExecuteW from shell32.dll:

int value= ShellExecuteW(0, "open", "C:\test.bat", strParameters, "", 1);

The batch file runs a java app which seems to open but returns an error and quickly the console window closes.

I want to capture the error from the console. I've tried adding the following re-directions at the end of my command in the batch file:

> output.txt 

2> output.txt 

> output.txt 2>&1

1> output.txt 2>&1

| output.txt

I would expect these common commands to work but none of them result in anything being written in output.txt. What could I be doing wrong here?

I'm using Metatrader5 (MQL5 language) to call shellexecuteW from.

Thankyou for your replies.

1

There are 1 best solutions below

0
On

First you should put the bat file in Terminal path in "File" folder because of mql5 security policy, Secondly put these codes in your script to use "ShellExecuteW" function correctly.

#import "shell32.dll"
        int ShellExecuteW(int hwnd,string operation,string file,string parameters,string directory,int showCmd);
#import
int result;
    string strParameters = "";
    string filename="test.bat";
    string Batfile_path=TerminalInfoString(TERMINAL_COMMONDATA_PATH)+"\\Files\\"+filename;
    Print("Batfile_path:",Batfile_path);
  result = ShellExecuteW(0, "open", Batfile_path, strParameters, "", 0);

   if(result <= 32)Print("Shell Execute for running bat file Failed: ", result);