MQL5 dll test (MSVC8, OpenWatcom)

Microsoft Visual Studio 8 (2005)

Microsoft Platform SDK for Windows Server 2003

Download: http://code.google.com/p/betterea/downloads/detail?name=vc2005_PSDK.tar.gz&can=2&q=

Open Watcom. Download: http://openwatcom.mirror.fr/

///////////////////////////////////////////////////////////////////////////////////////

http://www.willus.com/mingw/yongweiwu_stdcall.html

MSVC and MinGW DLLs

http://www.mingw.org/wiki/MSVC_and_MinGW_DLLs

How can an MSVC program call a MinGW DLL, and vice versa? 

Assume we have a testdll.h, testdll.c, and testmain.c. In the first case, we will compile testdll.c with MinGW, and let the MSVC-compiled testmain call it. You should use 

 

gcc -shared -o testdll.dll testdll.c \ 

    -Wl,--output-def,testdll.def,--out-implib,libtestdll.a  

to produce the DLL and DEF files. MSVC cannot use the MinGW library, but since you have already the DEF file you may easily produce one by the Microsoft LIB tool:  

lib /machine:i386 /def:testdll.def  

Once you have testdll.lib, it is trivial to produce the executable with MSVC: 

 

cl testmain.c testdll.lib  

Now for MinGW programs calling an MSVC DLL. We have two methods. One way is to specify the LIB files directly on the command line after the main program. For example, after 

 

cl /LD testdll.c  

use  

gcc -o testmain testmain.c testdll.lib  

The other way is to produce the .a files for GCC. For __cdecl functions (in most cases), it is simple: you only need to apply the reimp tool from Anders Norlander (since his web site is no longer available, you may choose to download here a version enhanced by Jose Fonseca): 

 

reimp testdll.lib 

gcc -o testmain testmain.c -L. -ltestdll  

However, for __stdcall functions, the above method does not work. For MSVC will prefix an underscore to __stdcall functions while MinGW will not. The right way is to produce the DEF file using the pexports tool included in the mingw-utils package and filter off the first underscore by sed: 

 

pexports testdll.dll | sed "s/^_//" > testdll.def  

Then, when using dlltool to produce the import library, add `-U' to the command line:  

dlltool -U -d testdll.def -l libtestdll.a  

And now, you can proceed in the usual way:  

gcc -o testmain testmain.c -L. -ltestdll  

Hooray, we got it. 

///////////////////////////////////////////////////////////////////////////////////////

//+------------------------------------------------------------------+

//|                                                 MQL5 DLL Samples |

//|                   Copyright 2001-2010, MetaQuotes Software Corp. |

//|                                        http://www.metaquotes.net |

//+------------------------------------------------------------------+

#include "stdafx.h"

//+------------------------------------------------------------------+

//| Passing and receving of simple variables                         |

//+------------------------------------------------------------------+

_DLLAPI int __stdcall fnCalculateSpeed(int &res1,double &res2)

  {

   int    res_int=0;

   double res_double=0.0;

   int    start=GetTickCount();

//--- simple math calculations

   for(int i=0;i<=10000000;i++)

     {

      res_int+=i*i;

      res_int++;

      res_double+=i*i;

      res_double++;

     }

//--- set calculation results

   res1=res_int;

   res2=res_double;

//--- return calculation time

   return(GetTickCount()-start);

  }

//+------------------------------------------------------------------+

//| Filling the array with values                                    |

//+------------------------------------------------------------------+

_DLLAPI void __stdcall fnFillArray(int *arr,const int arr_size)

  {

//--- check input variables

   if(arr==NULL || arr_size<1) return;

//--- fill array with values

   for(int i=0;i<arr_size;i++) arr[i]=i;

  }

//+------------------------------------------------------------------+

//| The substring replacement of the text string                     |

//| the string is passed as direct reference to the string content   |

//+------------------------------------------------------------------+

_DLLAPI void __stdcall fnReplaceString(wchar_t *text,wchar_t *from,wchar_t *to)

  {

   wchar_t *cp;

//--- parameters checking

   if(text==NULL || from==NULL || to==NULL) return;

   if(wcslen(from)!=wcslen(to))             return;

//--- search for substring

   if((cp=wcsstr(text,from))==NULL)         return;

//--- replace it 

   memcpy(cp,to,wcslen(to)*sizeof(wchar_t));

  }

//+------------------------------------------------------------------+

//| Call for the crush                                               |

//+------------------------------------------------------------------+

_DLLAPI void __stdcall fnCrashTest(int *arr)

  {

//--- wait for receipt of a zero reference to call the exception

   *arr=0;

  }

//+------------------------------------------------------------------+