how to use user32 dll in mql5 to enable auto trading ability

 
 I need help for using user32 dll for enable or disable algo trading button in mql5, I am trying to find solution which out using hold ctrl or short cuts, like that send code by PostMessageA, I have designed it for mt4 and work well, but same code doesn't work in mql5, Can any body please help me with that matter ?
 

Here is a working solution:

// Enable auto-trading button
if(TerminalInfoInteger(TERMINAL_TRADE_ALLOWED) == 0) Simulate_Ctrl_E();

// Disable auto-trading button
if(TerminalInfoInteger(TERMINAL_TRADE_ALLOWED) == 1) Simulate_Ctrl_E();

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
#define KEYEVENTF_KEYUP    0x0002
#include <WinAPI\winapi.mqh>
#include <VirtualKeys.mqh>

//+------------------------------------------------------------------+
//| Simulate 'Ctrl+E' keystroke                                      |
//+------------------------------------------------------------------+
// toggle the Automated trading button
void Simulate_Ctrl_E()
  {
   keybd_event(VK_CONTROL,0x9d,0,0);
   keybd_event((uchar)VkKeyScanW('E'),0x92,0,0);
   keybd_event((uchar)VkKeyScanW('E'),0x92,KEYEVENTF_KEYUP,0);
   keybd_event(VK_CONTROL,0x9d,KEYEVENTF_KEYUP,0);
  }
//+------------------------------------------------------------------+

 
amrali:

Here is a working solution:

thank you sir
its a great help
but I need a solution whiteout keyboard
I want somethin like what we have in mt4 but in mt5
no keyboard or control + needed
do you have solution like this please ?

 

Unfortunately, no!

 
Ashkan Hazegh Nikrou: but I need a solution whiteout keyboard I want somethin like what we have in mt4 but in mt5 no keyboard or control + needed do you have solution like this please ?

What do you mean "without keyboad"?

The MT4 solutions that I know of also uses simulated keyboard events to enable/disable the Auto Trading button!

What @amrali provided is the MT5 version for the Algo Button, but it uses the same concept as the MT4 solution.

EDIT: Just noticed that there is indeed another solution for MT4 using "MT4_WMCMD_EXPERTS". However, I don't see why you can't use the Key Event method in your case. Can you explain why?

 
Fernando Carreiro:

What do you mean "without keyboad"?

The MT4 solutions that I know of also uses simulated keyboard events to enable/disable the Auto Trading button!

What @amrali provided is the MT5 version for the Algo Button, but it uses the same concept as the MT4 solution.

EDIT: Just noticed that there is indeed another solution for MT4 using "MT4_WMCMD_EXPERTS". However, I don't see why you can't use the Key Event method in your case. Can you explain why?

Because key event work on whole windows softwares, for example if expert work on meta trader and user open web browser, when EA call key event, then this key event effect on his browser.

 
Ashkan Hazegh Nikrou: Because key event work on whole windows softwares, for example if expert work on meta trader and user open web browser, when EA call key event, then this key event effect on his browser.

The following solution works on both MT4 and MT5 (This is a Script with conditional compilation for both platforms):

#property strict

#ifdef __MQL5__
   #include <WinAPI\winapi.mqh>
   #define MT_WMCMD_EXPERTS   32851   
#else
   #define HANDLE       int
   #define PVOID        int
   #import "user32.dll"
      HANDLE   GetAncestor(  HANDLE hwnd, uint flags);
      int      PostMessageW( HANDLE hwnd, uint Msg, PVOID param, PVOID param );
   #import
   #define MT_WMCMD_EXPERTS   33020
#endif

#define WM_COMMAND 0x0111
#define GA_ROOT    2

void OnStart()
{
   AlgoTradingStatus( true ); // Enable Algo Trading
}

void AlgoTradingStatus( bool Enable )
{
   bool Status = (bool) TerminalInfoInteger( TERMINAL_TRADE_ALLOWED );

   if( ( Enable && Status ) || ( !Enable && !Status ) ) return;
      
   HANDLE
      hChart      = (HANDLE) ChartGetInteger( ChartID(), CHART_WINDOW_HANDLE ),
      hMetaTrader = GetAncestor( hChart, GA_ROOT );
      
   PostMessageW(hMetaTrader, WM_COMMAND, MT_WMCMD_EXPERTS, 0 );
}
EDIT: This uses the same technique used on MT4 without keyboard events, by using Custom Windows Messaging for MetaTrader.
 
Fernando Carreiro:

The following solution works on both MT4 and MT5 (This is a Script with conditional compilation for both platforms):

EDIT: This uses the same technique used on MT4 without keyboard events, by using Custom Windows Messaging for MetaTrader.

thank you sir
I tested it as script and expert advisor
and its working perfectly
that is what I needed
its great

 
Ashkan Hazegh Nikrou:

thank you sir
I tested it as script and expert advisor
and its working perfectly
that is what I needed
its great

You are welcome!