Why is there a difference in EA's/Indicators regarding DDL usage?

 

I have a client who use a dll to validate user details with a server, and so far we've only used it on indicators (for about a year). When I tried to use the validation with an EA instead (exactly the same code), it simply doesn't work. While trying to find the reason why it is so, i created a snippet and used the exact same code in one EA, one script and one indicator. Only the indicator return the correct values.

Why does this differ? Is this a known problem?

(Addition info: the dll is called from a .mqh file, and allow DLL is of course turned on. I verified that all three finds the DLL, and the same info is passed to the DLL, but only the indicator returns the correct value. I've tried each of them seperately, so there is no conflict when accessing the DLL)


Any kind of ideas/help is greatly appreciated!

 

please post the source code of your dll. How are we supposed to know what your dll does (and how it does it) and why it fails if you don't post the source?


The only obvious difference is that when called from an indicator all function calls always come from MT4's main thread while in scripts and EAs every single call comes from some random other thread. Maybe (wild guess) your compiler (or you) does not create code to hook into DLL_THREAD_ATTACH and initialize the treading related stuff of its runtime library (or other libraries that are used) for every thread created outside your dll and then you try to use things that are only possible with properly initialized threading or only from within the main thread or from the thread that initialized this library.

 

Thank you for your reply 7bit,

7bit:

please post the source code of your dll. How are we supposed to know what your dll does (and how it does it) and why it fails if you don't post the source?f

I don't have access to the source code, nor do I have the rights to post the DLL as this belongs to the client. I was just asking for know differences between EA's and indicators for handling DLL's.

I think your reply provided me with enough information to solve this. I appreciate your help.

 
SysInv:

Thank you for your reply 7bit,

7bit:

I don't have access to the source code, nor do I have the rights to post the DLL as this belongs to the client. I was just asking for know differences between EA's and indicators for handling DLL's.

I think your reply provided me with enough information to solve this. I appreciate your help.

7bit is right that the only significant difference between use of a DLL from an EA versus an indicator is the threading model. This brings to mind two specific things which might be the cause of your problem, in addition to 7bit's DLL_THREAD_ATTACH suggestion:

1. An assumption in the DLL that CoInitialize() has been called on the thread. This is likely to be true of the main MT4 UI thread which is used for indicators, but I'm pretty sure it's not true for the temporary threads on which an EA runs.

2. A DLL running from an indicator in the main MT4 UI thread has to be careful not to lock the MT4 user interface, e.g. don't call the Win32 Sleep() API function. If the DLL is doing communication with a remote server, it will either have to be doing its own multi-threading internally or it will have to be using some sort of callback mechanism to avoid locking the UI. The way in which the DLL is setting this up may be making an assumption about only ever being called on a single thread etc. Specifically, the DLL may be using a callback mechanism which assumes that the thread which creates the callback will continue to exist indefinitely.

Reason: