Search code examples
algorithmic-tradingmetatrader4mt4

Does ShellExecuteW(...) work only once in MetaTrader 4 or not?


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 );
   }

Solution

  • 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.