Download MetaTrader 5
To add comments, please log in or register
Take active part in the life of the community. Improve your rating!
Route206
211
Route206 2015.10.11 08:20 

Hello i'm looking to run a BAT file from a script (and upon successful testing ultimately from an EA).

I've looked at https://www.mql5.com/en/forum/139191 which i found very helpful but...

#property strict
#import "shell32.dll"
int ShellExecuteA(int hwnd,string Operation,string File,string Parameters,string Directory,int ShowCmd);
#import

//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart()
  {
Alert("this is a test");
ShellExecuteA(0,"Open","c:\users\route206\desktop\test.bat","","",1);
Alert(GetLastError());  
  }
//+------------------------------------------------------------------+

 

is not working.

I do allow for a DLL import when starting. First i get the 'unrecognized character escape sequence' upon compiling, upon which i added a slash, ie: c:\\users\\route206\\desktop\\test.bet. That DID compile, but not success upon running.
Later when i put the file on c:\ (so now not requiring any slashes) the script does again compile, but again no success.  
In both cases i get the the 'this is a test' Alert, but the execution of the BAT file fails.

GetLastError() give me "0".

I noticed the post i'm using is an older post, so maybe things have changed since?

any help is appreciated, thanks in advance. 

Hopefully Mr. Roeder is again seeing my mistake(s) quickly? 

Carl Schreiber
5959
Carl Schreiber 2015.10.11 09:08  

1) You have to import and use "ShellExecuteW" since b600+!

2) GetLastError() gives you only the Mql4 Errors, you have to look for Windows-Errors!

peter.MT4Web
158
peter.MT4Web 2015.10.11 10:54  

Start the bat File with:


ShellExecuteW(NULL,"open","c:\\users\\route206\\desktop\\test.bat",NULL,NULL,1);
Route206
211
Route206 2015.10.12 07:06  
Thank you both!

That seemed to do the trick.
I was done fiddling around with task scheduler; this is much better!
Route206
211
Route206 2015.10.26 15:16  
#import "shell32.dll"
int ShellExecuteW(int hwnd,string Operation,string File,string Parameters,string Directory,int ShowCmd);
#import

ShellExecuteW(NULL,"open","c:\\users\\route206\\desktop\\test.bat",NULL,NULL,1);

is it correct that the above only works on 64bit machines? 

I'm now trying on a 32bit - no joy..  :-?
any suggestions? 

JC
1395
JC 2015.10.26 15:34  
Route206:

is it correct that the above only works on 64bit machines? 

No, it's not restricted to 64-bit machines. (The difference between Ansi A and Unicode W versions of Win32 functions has nothing to do with 32-bit vs 64-bit.)

Your example works for me (obviously changing the "route206" to my Windows username). But the location of the Windows desktop for the current user isn't necessarily C:\Users\whatever\Desktop. It varies quite widely depending on the version of Windows and various other factors. I suspect that you are now trying it on an old 32-bit computer running something like Windows XP. I think that the normal desktop location on XP is something like C:\Documents And Settings\whatever\Desktop.

Route206
211
Route206 2015.10.29 21:00  

I'll try it again, but i'm pretty sure the location is correct. It's a Win7 machine.. Will let you know and thanks: always good to know someone got it to work.. :-) 

whroeder1
13641
whroeder1 2015.10.30 12:56  
ShellExecuteW(NULL,"open","c:\\users\\route206\\desktop\\test.bat",NULL,NULL,1);
  1. The first argument is an int, use 0 not NULL.
  2. The last two are strings. Try passing empty strings ("") not NULLS.
/
To add comments, please log in or register