MetaTrader 4 external DLL Crashes When Attempting to use printf fopen fprintf etc.

 
Hi, I'm an experienced developer attempting to use MT4 with an external DLL for the first time. Following the excellent instructions here http://www.forexfactory.com/showthread.php?t=333471 I have been able to compile and install the ExpertSample DLL that ships with MT4. Unfortunately any attempts to invoke the DLL caused a crash:
ExportFunctions EURUSD M5: function 'GetIntValue' call from 'ExpertSample.dll' critical error c0000005 at 610D6734


I quickly established that the problem is the printf() statement, if I remove it then the problem goes away and the call to GetIntValue() and other ExpertSample functions work ok.

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
MT4_EXPFUNC int __stdcall GetIntValue(const int ipar)
  {
   printf("GetIntValue takes %d\n",ipar);    // For Some Reason this is causing a crash when called from MT4.
   return(ipar);
  }


Thinking, well, it is unclear what stdout (the normal C/C++ output stream) represents in the context of a DLL called from the MT4 terminal. I attempted to use fopen() and fprintf() to write output to my own log file instead of printf. Unfortunately any attempt to invoke stdio functions from within the DLL crash. I even tried launching terminal.exe piping the output >terminal.exe > stdout.txt but that made no difference.

I've tried this compiling ExpertSample.cpp as a C++ program and as a simple C program (complies OK with just a few minor tweaks). And in both cases the results are the same. The DLL is fine, unless any I/O functions are used and then it crashes.

The only thing that might be a little unusual is that I'm compiling and linking using the GCC compiler with the Eclipse IDE instead of the more usual Visual Studio / Microsoft complier.

So I'm a bit baffled at the moment and grateful for any help.

I've not included any code, as this problem manifests itself with ExpertSample.cpp unmodified. Platform is the OANDA-MetaTrader Version: 4.00 Build 445 runing on a 32bit Windows 7 machine.

 

I use fprintf and fopen functions in my DLL without any problems. I write to files and read from files just fine. I use C++ with Visual Studio Windows 7 64bit.

There are a couple configuration steps in Visual Studio in order for the DLL to properly work with MT4. Like you say, I bet using the GCC compiler with Eclipse IDE is not setting up all you need in the correct way.

My two cents...

 
pbuk: Problem Solved.
The problem is to do with the difference between the GCC compilers provided with Cygwin and MinGW. The GCC that ships with Cygwin essentially links against its own posix libraries whereas MinGW links against the native Win32 libraries. As I already had Cygwin installed and Eclipse Configured for Cygwin GCC I was linking against the posix libraries without realising. Once this was corrected everthing seems fine.
 
It seems i keep extolling the virtues of 7bit's excellent posts, but i'd be interested if the OP could try Lazurus and report back, as i'm thinking of making the transition from Visual Studio C++ for a DLL i created to Pascal.
Reason: