There are a variety of reasons why you might need to use DLLs in Meta Trader. You might have existing code you don’t want to port, you might want to use a different language, you might have a proprietary library that you do not control, or you might simply want to use native Windows libraries to speed things up.
In this article I will share how I used a native Windows DLL in a Meta Trader Custom Indicator to display a message box. The same methods would apply to Expert Advisors. Even other platforms would follow similar steps.
The reason I had to do this was because MQL does not allow calling the built in MessageBox function from within a custom indicator. The reason given in the MQL reference is that “indicators are executed in the interface thread and shouldn’t slow it down”
Now this is a very valid reason, so you should not do that unless you really know what you are doing. In my case I wanted to call it only during the initialization of the custom indicator when init is failed, hence it would not block the interface thread.
First thing you should be aware of is that using DLLs require it to be allowed by selecting the “Allow DLL imports” checkbox on the Common tab of the Custom Indicator dialog.
Then you need to find the location and function declaration for the function you want to use.
With a little bit of search online, you can find that the native Windows “MessageBox” function is in “user32.dll”. We will be using “MessageBoxW” function, which is the unicode equivalent.
To use a function from a dll file, we need to define it in our mql4 file. The reference for “MessageBoxW” function has the following definition:
You can find details about what each function parameter does within the reference document linked above. In short:
hWnd: the handle for the parent window of the message box, can be set to null for no parent. When no parent is set the message box will have a window of its own independent of the Meta Trader window it was called from.
lpText: the message to be displayed
lpCaption: the dialog title
uType: The contents of the dialog box. You can find all the options in the reference document. For this tutorial I will be using the MB_ICONERROR type, which displays an error dialog. There are several options you can choose from, with different icons and buttons.
The data types for fuction arguments might look confusing at first, but with a little bit of search online you can find what each of them mean and the correspoding types in mql4.
HWND: This is simply an integer type that refers to the parent window. In this tutorial we will be using the window handle to the chart.
LPCWSTR: This is a unicode string type, in mql it is simply string
UINT: unsigned integer, uint in mql
For simplicity I defined these types before the function definition. The end result is:
Once we defined the function we can use it in our code:
WindowHandle simply returns the system handle for the chart with the given symbol and period.
And with that you will have a message box showing whatever information you like. You can use the same steps for calling other native windows functions or load other functions from custom DLLs.