run a *.bat from EA

Back to topics list To post a new topic, please log in or register
avatar
63
Route206 2015.10.11 10: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://forum.mql4.com/47824 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? 

 

avatar
2017
gooly 2015.10.11 11: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!

avatar
108
eddie 2015.10.11 12:54 #
 

Start the bat File with:


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


avatar
63
Route206 2015.10.12 09:06 #
 
Thank you both!

That seemed to do the trick.
I was done fiddling around with task scheduler; this is much better!
avatar
63
Route206 2015.10.26 16: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? 

avatar
1318
jjc 2015.10.26 16: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.

avatar
63
Route206 2015.10.29 22: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.. :-) 

avatar
12985
WHRoeder 2015.10.30 13: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.
Back to topics list  

To add comments, please log in or register