We've all tried it. There's something we want to do in a DLL but Metatrader is still stuck in the 19th century. For my inquiry, I give you this:
Run that and get this:
Unhandled exception 0xE0434352
Change the DLL to this:
..And it works flawlessly.
So the question is, why? Is this something I'm doing wrong? I don't think so.
So, anyone got any ideas?
"Unhandled exception 0xE0434352" means that you have unhandled exception in your managed code.
Add try/catch block inside the WriteMe() function and check if you can write to the file.
Your file ClientMessage.txt is potentially going to be written into the working directory of the MT4 executable. This can be the \Program Files area, and attempts to write there will fail unless the MT4 executable is running with escalated privileges.
So, you may simply have an unhandled .NET exception arising from a file access error. Try explicitly writing ClientMessage.txt to an unprotected area of disk.
Ok. It's not where the DLL writes the file. The '@' symbol specifies to write the file where the DLL was launched from. In this case, the MQL/Libraries folder. I've changed this to my documents / downloads / etc. Doesn't matter.
It's the using statement. Metatrader bombs on that statement. Why? Has anyone gotten a using statement to work in a C# DLL for Metatrader? I thought this worked in previous builds.
As far as I am aware, the @ simply indicates a verbatim string literal. It is not going to control where the file is written.
Your code works for me in a copy of MT4 installed outside \Program Files, and crashes if MT4 is installed inside \Program Files.
If I run MT4 from a location such as C:\MyMT4, i.e. such that the executable is C:\MyMT4\terminal.exe, then the file ClientMessage.txt is created at C:\MyMT4\ClientMessage.txt. This is obviously going to cause problems if MT4 is inside a protected area of disk such as \Program Files.
If I change your code as follows ...
... then the exception which is logged when I use the DLL from a copy of MT4 within Program Files is, very unsurprisingly, as follows: