How to shutdown another MT4 instance.

 

Hello.

I know how to open another MT4 instance:

#import  "shell32.dll"  
   int ShellExecuteA(int hWnd, string Operation, string File, string Parameters, string Directory, int ShowCmd);
#import
the dll guideline is here

So, when I call it this way whatever MT4 I want is opened by finding its path.

int hndl = ShellExecuteA(0, "open", "terminal.exe", NULL, "valid path folder", 3);
if(hndl > 32)
{
   another MT4 instance already successfully opened...
}

Ok. I need to add to this my script a call to (imported) functions which will shutdown that MT4 instance.

Hope it is clear.

Simon

 

Certainly not the proper way (I mean it is a hacking), but what about this:

#import "kernel32.dll"
    int WinExec(string NameEx, int dwFlags);
#import

int start() {
   WinExec("taskkill /im terminal.exe /fi \"WINDOWTITLE eq 578225: OANDA*\"", 0);
   return(0);
}
 
erzo:

Certainly not the proper way (I mean it is a hacking), but what about this:


My now experience: it returns 33, but no instance is closed.

I even tried with

int GetCurrentThreadId();
bool TerminateThread(int ThrHndl, int dwExitCode);

Should it be a security and access rights issue?

 
SimonTrader:


I even tried with

int GetCurrentThreadId();
bool TerminateThread(int ThrHndl, int dwExitCode);

It is not a Thread issue, you should use Process functions instead. Try CreateProcess that gives a handle, and TerminateProcess that can terminate the process by handle. (I tried only ExitProcess that shuts down the current terminal in my test).
 
Here is a similar topic: https://www.mql5.com/en/forum/107527
 
erzo:
Here is a similar topic: https://www.mql5.com/en/forum/107527

Many thanks erzo.
 

Why so complicated?

SendMessage(hWnd, WM_CLOSE, 0, 0);

pp

 

paulepanke idea is good.

Here's another one :

int      DestroyWindow(int hWnd);

... which you can find in WinUser32.mqh

 
paulepanke:

Why so complicated?

SendMessage(hWnd, WM_CLOSE, 0, 0);

pp

onewithzachy:

paulepanke idea is good.

Here's another one :

int      DestroyWindow(int hWnd);

... which you can find in WinUser32.mqh

Could you explain please, how to get a window handle to Another MT4 instance? The ShellExecuteA does not provide it:

...
The return value is cast as an HINSTANCE for backward compatibility with 16-bit Windows applications. 
It is not a true HINSTANCE, however. It can be cast only to an int and compared to either 32 or the following error codes below.
...
Or should be created a separate window berofe calling ShellExecuteA, and pass the handle to it? Could you show a working code for DestroyWindow or WM_CLOSE that really shuts down the newly started another MT4, because I want to learn how it works. Thank you!
 

You would rarely use ShellExecute() for starting another process because of it's overhead. Usually you use ShellExecute() for doing something with files, ie. printing, editing and so on. Files you don't know the application to handle them. Windows looks up the linked application in the registry and launches that application together with your file as a parameter. You can use it for "Open"ing an exe-file but CreateProcess() is the recommended way to go.

You have to consider there may be multiple running MT4 instances. First you have to decide wich of them to shutdown. You iterate through the top level windows to detect all the running MT4 instances and identify their module names (ie. the executable's file name). After that you have the handle of the top level window of the application you want to exit. Now you send WM_CLOSE.

If you started the process by yourself you already know the module name and can easily find the applications main window. This is basic windows programming.

pp

 

Sending WM_CLOSE to a window in effect is the same as clicking the little cross in the top right corner of the application's main window title bar. The app might ask you to save unsaved changes (ie. modified files etc.) before it shuts down. To give a complete example this forum is the wrong place. It's not only calling ShellExecute() or CreateProcess(), you have to wait until the app initialized itself, creates all their windows and threads and enters idle state. From this point on it will respond to your shutdown request in a defined way. Everything else leads to unpredictable crashes.

You might consider the brutal way to go by simply killing the process (as the Task Manager does) but that's not recommended either. Again, to advanced for this forum. http://www.codeproject.com/Articles/18962/Kill-Application-Using-Win32-APIs

pp

Reason: