0

Trying to launch an .exe from MQL4 using ShellExecuteW().

Does this command work only once, or not?

#import "shell32.dll"   // MQL4-syntax-wrapper-Bo[#import]Container
                        // v-------------- caller-side interface to DLL
        int ShellExecuteW( int    hWnd,
                           int    lpVerb,
                           string lpFile,
                           int    lpParameters,
                           int    lpDirectory,
                           int    nCmdShow
                           );
#import                 // MQL4-syntax-wrapper-Eo[#import]Container

if (  cond == true ){
      ShellExecuteW( 0, "open", "D:\\Execute.exe", "", "", 1 );
   }
user3666197
  • 1
  • 6
  • 50
  • 92
NJK
  • 53
  • 2
  • 8

1 Answers1

0

A: a short version

Maybe yes, maybe no.

A: a bit more indepth one [TLDR]

Fact: MT4 Terminal's process-control / process-management is nothing superior, however it allows you to integrate several lines of control over what happens inside ( and outside ... not only via ShellExecuteW(...) ... ) the MT4 Terminal.

MT4 Terminal supports the following processes per graph ( via built-in threads ):

  1. { 0 | 1 } occurences of a singleton instance called MQL4-ExpertAdvisor
  2. { 0 | 1 } occurences of a singleton instance called MQL4-Script
  3. { 0 | 1 ... n } occurences of a functionally restricted instance called MQL4-TechnicalIndicator

The nature of these instances differs in several ways, however in the closest relation to your question, each of the processes have both a mandatory part and a set of arbitrary parts.

Speaking in the original MQL4 ( one may have noticed, that since Build 7xx MQL4 language syntax has moved closer and closer towards MQL5, thus sometimes got labeled as MQL4.5 :o) )

//\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/
//
// MQL4 code ----------------------------------------------------------<BoF>------

// MQL4 code compiler directives' section -----------------------<BoS>

#import ...                // MQL4-syntax-wrapper-Bo[#import]Container
        ..
        .
#import                    // MQL4-syntax-wrapper-Eo[#import]Container

// MQL4 code compiler directives' section -----------------------<EoS>

// ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
//
// MQL4 code section ---------------------------- init() / start() / deinit()
//

int init(){    // this part is being run just once,
               //                        right upon an instance activation
   }

int start(){   // this part is being run just once, for an MQL4-Script
               //                        right upon an instance activation
               //                    run upon an FX-MarketEvent is being observed
               //                             in an MQL4-ExpertAdvisor
               //                             or
               //                             in an MQL4-TechnicalIndicator


   }
...
..
.
//
// MQL4 code ---------------------------------------------------------<EoF>------
//
//\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/

So, the exact location of your deployment code, that (re-)uses the generic DLL-service ( #import-ed for both the compile-time alignment and the dynamic linking upon (re-use) decides how many times the external process has been asked to get invoked.


Nota Bene

There are much smarter ways to integrate MT4 Terminal with external processes or remote processes ( Clouds & Grids ) than just a DLL-based spawn of just another blind & deaf unmanageable <localhost> process.

Typically one needs both process-control and bidirectional communication between / among processes.

Do not hesitate to ask more.

MT4 ( with some additional tools ) can do.

user3666197
  • 1
  • 6
  • 50
  • 92
  • Thanks for the reply. Can you please share any info on how to handle bidirectional communication from MT4? Also, Should I move the #import statement to get the ShellExecute working? – NJK Apr 22 '15 at 06:16
  • Welcome, Niharika. The location of an **`#import`** compiler-directive is not arbitrary. It must be present in the initial part of the MQL4 source code ( ref. MQL4 documentation / help pages for all syntax related details & the respective directives ). The issue is, where your `ShellExecuteW(...)` et al DLL-service call ( invocation ) is being located, not the `#import` directive itself. – user3666197 Apr 22 '15 at 13:39
  • **n.b.:** be carefull with control mechanisms for spawning external processes. To understand the issue, just imagine a trivial loop `for ( int nInstances = 0; nInstances < 10; nInstances++ ){ ShellExecuteW( ... , "cmd.exe", ... ); Print( "Loop spawned ", nInstances, " already..." ) }` where **10** would become **100** or **1000** or **10000** ... so be carefull. – user3666197 Apr 22 '15 at 20:00
  • I am still not able to get this working:( I am calling ShellExecuteW(..) from EA depending on the value I get from the Custom Indicator as below indicatorReturn = iCustom(NULL,0,"IndicatorSample",0,0); Condition to call .exe if(indicatorReturn != Empty){ Call shellexecuteW }.. – NJK Apr 24 '15 at 06:12
  • How do I run .exe (sutoit script ) as administrator ( in Win8.1 OS ) from ShellExecuteW ? – NJK Apr 24 '15 at 06:32
  • Start to isolate the problem. First get a simplest process to be launched ( be it a **notepad.exe** or **cmd.exe** ), thus proving, your DLL-call is correctly setup. Right? The caller-side logic keep as simple as possible --- i.e. call it just once, from **int init() { ShellExecuteW( ... ); }**, ok? – user3666197 Apr 24 '15 at 19:09