How to make a Metatrader Terminal on VPS use a c# DLL?

2.1k views Asked by At

Status:

I've written a DLL in C#, which is ment to be called from a code written in a MQL4 ( basically a C-alike language ) and sends back some data ( unmanaged to managed and back ).

In general, the DLL sends queries to a MySQL-host and when needed, it returns back some data. I used the NuGet package "Unmanaged Exports". I used this DLL with the FOREX trading MetaTrader Terminal program, running on Windows 8.1 x64 and everything worked perfectly. The C# code was compiled in x86 since the MetaTrader Terminal runs only on x86.


Goal:

Now I am interested in using the same DLL and call it using the same code but this time I need the code to run from a VPS.

This VPS runs on Windows Server 2008 R2 SP1 x64.

Other than that everything is the same:
same version of MetaTrader Terminal,
I even have installed VS 2013 ( same version as I used on my laptop ) on this VPS
and compiled the C# code with no problem.

I also installed Microsoft Visual C++ 2010 Redistributable, hoping this will help but I'm still here...

There is also no firewall which could be blocking the access to the host.

What could be preventing the DLL to run on the VPS?

Thanks!!


Problem isolation:

I wrote a short C# program on the VPS in order to check the DLL.
It ( DLL ) works great.

I know that the MetaTrader Terminal does recognize the DLL because otherwise I would receive an error regarding that basic problem.

So the problem must be between the MetaTrader Terminal and the DLL.

1

There are 1 answers

0
user3666197 On

Based on inputs given,

a colision appears on otherwise working MT4/DLL cooperation scheme and out of question, a firewall is not to be blamed for. DLL mechanics does not incorporate operations a firewall takes care about.

So as to avoid any Win Server settings, that are on VPS typically out of your control,
move your DLL-file(s) into the safe-most location,
recent changes to New-MQL4.56789 have made a default location unsafe
- so do not rely on placing the file right to the MT4 ~Terminal Directory~/MQL4/Library as you are used to on your localhost

Beware the nested DLL calls

Call of Imported Functions
To import functions during the execution of a mql4-program, the client terminal uses early binding. This means that if a program has call of an imported function, the corresponding module (ex4 or dll) is loaded during the program load. MQL4 and DLL libraries are executed in the thread of a calling module.

It is not recommended to use the fully specified name of the module to be loaded like Drive:\Directory\FileName.Ext. The MQL4 libraries are loaded from the terminal_dir\MQL4\Libraries folder. If the library hasn't been found, then the client terminal performs an attempt to load it from terminal_dir\experts folder.

The system libraries (DLL) are loaded by the operating system rules. If the library is already loaded (for example, another Expert Advisor, and even from another client terminal, running in parallel), then it uses requests to the library already loaded. Otherwise, it performs a search in the following sequence:
- Directory, from which the module importing dll was started. The module here is an Expert Advisor, a script, an indicator or EX4 library;
- Directory terminal_data_directory\MQL4\Libraries (TERMINAL_DATA_PATH\MQL4\Libraries);
- Directory, from which the MetaTrader 4 client terminal was started;
- System directory;
- Windows directory;
- Current directory;
- Directories listed in the PATH system variable.

If the DLL library uses another DLL in its work, the first one cannot be loaded in case when there is no second DLL.

Last resort

In case all efforts fail, a sure method to make a VPS-hosted solution work independently of the VPS-provider constraints and so as to minimise impacts from a risk of theirs potential non-reponsiveness, install the whole circus inside your-controlled VM ( typically a minimalistic wXP configured Virtual Guest Machine with the whole working solution, or you can "en-can" a working localhost installation ( not your whole notebook, but a test-machine ) )

The only adaptation needed then is the VPS-host installation of Hypervisor system ( VmWare WorkStation or a VM-Player ) plus a coordinated IP-adressing, which should not be an issue to match free IP-s/port#-s as needed.


How about a WINE thin-wrapped operations inside linux?

While tested and working smooth on Projects, I would not start with another approach alike linux/WINE thin-wrapped MetaTrader Terminal 4 operations ( which in facts embeds .EXE into a DLL-abstraction served by non-Win O/S behind a surrounding crowd of WINE/linux adaptors ) because the primary task is to eliminate root source of DLL-collision(s).