DLL unload on EA uninitialization cause Metatrader loose conection to broker

 

Hello,


I'm building a DLL written in MSVC++ (through Visual Studio Express 2010) and have a problem when the EA is uninitialized.

On DLL unload, MetaTrader terminal loose instantly the connection to broker and I have to stop/start the application to get it work again. Even the login on same account won't work.


Does anybody had had this kind of problem and found a solution or a workaround ?


Here is the DllMain code I'm using, thanks for your help :

BOOL APIENTRY DllMain(HINSTANCE hModule, DWORD ul_reason_for_call, LPVOID lpReserved) {
//+------------------------------------------------------------------+
//| Main entry point of DLL                                          |
//+------------------------------------------------------------------+
  BOOL ret = TRUE;
  string str;

//----
  switch(ul_reason_for_call)
  {
    case DLL_PROCESS_ATTACH:
      errno_t error;
      size_t sz;
      size_t len;
      LPSTR filename;

      sz = MAX_PATH;
      gpfLog = NULL;
      ghInstance = hModule;
      filename = new char [MAX_PATH];
      len = GetModuleFileNameA(hModule, filename, sz);
      str = filename;
      delete [] filename;

      if (len > 0 && len < sz) {
        len = str.rfind(KEYPATH);
        if (len >= 0) {
          gsDataFile = str.substr(0, len);
          gsLogFile = str.substr(0, len);
          gsDataFile.append("files\\XpertSniper.dat");
          gsLogFile.append("logs\\XpertSniper.log");
              error = fopen_s(&gpfLog, gsLogFile.c_str(), "w");
          if (error) {
            str = "DLL Initialization : cannot open log file ";
            str.append(gsLogFile);
            str.append("\n");
            ErrorMsg(str);
            ret = FALSE;
          } else {
            fprintf(gpfLog, "data file=%s\n", gsDataFile.c_str());
            fprintf(gpfLog, "log file=%s\n", gsLogFile.c_str());
          }
        } else {
          str = "DLL Initialization : Cannot find folder libraries in ";
          str.append(filename);
          str.append("\n");
          ErrorMsg(str);
          ret = FALSE;
        }
      } else {
        str = "DLL Initilization : Cannot retrieve DLL path\n";
        ErrorMsg(str);
        ret = FALSE;
      }
      break;
    case DLL_THREAD_ATTACH:
      ret=TRUE;
      break;
    case DLL_THREAD_DETACH:
      ret=TRUE;
      break;
    case DLL_PROCESS_DETACH:
      if (gpfLog)
        fclose(gpfLog);
      break;
  }
//----
  return(ret);
}
 
furynick:

Hello,


I'm building a DLL written in MSVC++ (through Visual Studio Express 2010) and have a problem when the EA is uninitialized.

On DLL unload, MetaTrader terminal loose instantly the connection to broker and I have to stop/start the application to get it work again. Even the login on same account won't work.


Does anybody had had this kind of problem and found a solution or a workaround ?


Here is the DllMain code I'm using, thanks for your help :


If found the DllMain is not in cause, after commenting out exported functions one by one it may be the (improper) use of strcmp in one function (even if it's not used within the EA) which cause disconnect when DLL unloads.


Very strange ... but workarounded with use of string.compare method.

Reason: