Run script from MQL5 indicator code

 

Hi,

This code used to work in MT4 and it was working in MT5 but not anymore.

It's the code that execute another program ex:Script from the code.


Is there anyone who know how to make it work in MQL5?

#import "user32.dll"
   int RegisterWindowMessageW(string MessageName);
   int SendMessageW (int hwnd, int msg, int wparam, char &Name[]);
   void keybd_event(int VirtualKey, int ScanCode, int Flags, int ExtraInfo);   
#import
//*------------------------------------------------------------------*
//STARTS A METATRADER PROGRAM
int StartProgram(int hWnd, string ProgramName, string ProgramType="Expert", bool AutomaticallyAcceptDefaults=true)
{  
   char pn[256];
   StringToCharArray(ProgramName , pn);
   
   int InternalMessageNumber = RegisterWindowMessageW("MetaTrader4_Internal_Message");
   
   int msg_number = 14;
   if(ProgramType=="Expert") msg_number = 14;
   if(ProgramType=="Indicator") msg_number = 15;
   if(ProgramType=="Script") msg_number = 16;
   
   int result = SendMessageW (hWnd, InternalMessageNumber, msg_number, pn);
   
   return(result);   
}
//*------------------------------------------------------------------*
 

did you find the solution? 

I have the same problem 

thanks

 
Asa Social #:

did you find the solution? 

I have the same problem 

thanks

you responded to a 5 year msg.

 

Not quite sure what you want to do,

But here are code for useing Windows API user32.dll and shell32 to get an messagebox and execute external application / notepad...


// Import the MessageBoxW function from user32.dll
#import "user32.dll"
int MessageBoxW(int hWnd, string lpText, string lpCaption, int uType);
#import

// Import the ShellExecuteW function from shell32.dll (Unicode version)
#import "shell32.dll"
int ShellExecuteW(int hWnd, string lpOperation, string lpFile, string lpParameters, string lpDirectory, int nShowCmd);
#import

// Windows API constants for button types and return values
#define MB_YESNO  4      // Displays Yes and No buttons.
#define IDYES     6      // Returned if the user clicks Yes.
#define IDNO      7      // Returned if the user clicks No.

//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart()
{
   // Create a message to display
   string msg = "Do you want to launch Notepad?";
   
   // Call the MessageBoxW function with MB_YESNO to display Yes/No buttons.
   int result = MessageBoxW(0, msg, "Launch Notepad", MB_YESNO);
   
   // Check the return value to see which button was clicked
   if(result == IDYES)
   {
      // Launch Notepad.exe using ShellExecuteW
      int execResult = ShellExecuteW(0, "open", "notepad.exe", "", "", 1);
      
      if(execResult > 32)
         Print("Notepad opened successfully.");
      else
         Print("Failed to open Notepad. Error code: ", execResult);
   }
   else if(result == IDNO)
   {
      Print("User chose not to launch Notepad.");
   }
   else
   {
      Print("Unexpected return value: ", result);
   }
}

and an URL, Remember to add to to trusted site in settings in MQL5.

// Import the MessageBoxW function from user32.dll
#import "user32.dll"
int MessageBoxW(int hWnd, string lpText, string lpCaption, int uType);
#import

// Import the ShellExecuteW function from shell32.dll (Unicode version)
#import "shell32.dll"
int ShellExecuteW(int hWnd, string lpOperation, string lpFile, string lpParameters, string lpDirectory, int nShowCmd);
#import

// Windows API constants for button types and return values
#define MB_YESNO  4      // Displays Yes and No buttons.
#define IDYES     6      // Returned if the user clicks Yes.
#define IDNO      7      // Returned if the user clicks No.

//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart()
{
   // Create a message to display
   string msg = "Do you want to run the external command?";
   
   // Call the MessageBoxW function with MB_YESNO to display Yes/No buttons.
   int result = MessageBoxW(0, msg, "Execute External Program", MB_YESNO);
   
   // Check the return value to see which button was clicked
   if(result == IDYES)
   {
      // Use ShellExecuteW to open the URL with Unicode strings
      int execResult = ShellExecuteW(0, "open", "https://www.mql5.com", "", "", 1);
      
      if(execResult > 32)
         Print("URL opened successfully.");
      else
         Print("Failed to open URL. Error code: ", execResult);
   }
   else if(result == IDNO)
   {
      Print("User clicked NO. Stop trading.");
   }
   else
   {
      Print("Unexpected return value: ", result);
   }
}