bleh
[mqlkit.git] / DLLSample / ExpertSample.cpp
bloba766feb9b9889bb9cbab2ba8d723d7df01fac08b
1 //+------------------------------------------------------------------+
2 //| Sample DLL for MQL4 |
3 //| Copyright © 2004-2006, MetaQuotes Software Corp. |
4 //| http://www.metaquotes.net |
5 //+------------------------------------------------------------------+
6 #define WIN32_LEAN_AND_MEAN // Exclude rarely-used stuff from Windows headers
7 #include <windows.h>
8 #include <stdlib.h>
9 #include <stdio.h>
10 //----
11 #define MT4_EXPFUNC __declspec(dllexport)
12 //+------------------------------------------------------------------+
13 //| |
14 //+------------------------------------------------------------------+
15 #pragma pack(push,1)
16 struct RateInfo
18 unsigned int ctm;
19 double open;
20 double low;
21 double high;
22 double close;
23 double vol;
25 #pragma pack(pop)
26 //----
27 struct MqlStr
29 int len;
30 char *string;
32 static int CompareMqlStr(const void *left,const void *right);
33 //+------------------------------------------------------------------+
34 //| |
35 //+------------------------------------------------------------------+
36 BOOL APIENTRY DllMain(HANDLE hModule,DWORD ul_reason_for_call,LPVOID lpReserved)
38 //----
39 switch(ul_reason_for_call)
41 case DLL_PROCESS_ATTACH:
42 case DLL_THREAD_ATTACH:
43 case DLL_THREAD_DETACH:
44 case DLL_PROCESS_DETACH:
45 break;
47 //----
48 return(TRUE);
50 //+------------------------------------------------------------------+
51 //| |
52 //+------------------------------------------------------------------+
53 MT4_EXPFUNC int __stdcall GetIntValue(const int ipar)
55 printf("GetIntValue takes %d\n",ipar);
56 return(ipar);
58 //+------------------------------------------------------------------+
59 //| |
60 //+------------------------------------------------------------------+
61 MT4_EXPFUNC double __stdcall GetDoubleValue(const double dpar)
63 printf("GetDoubleValue takes %.8lf\n",dpar);
64 return(dpar);
66 //+------------------------------------------------------------------+
67 //| |
68 //+------------------------------------------------------------------+
69 MT4_EXPFUNC char* __stdcall GetStringValue(char *spar)
71 printf("GetDoubleValue takes \"%s\"\n",spar);
72 return(spar);
74 //+------------------------------------------------------------------+
75 //| |
76 //+------------------------------------------------------------------+
77 MT4_EXPFUNC double __stdcall GetArrayItemValue(const double *arr,const int arraysize,const int nitem)
79 //----
80 if(arr==NULL)
82 printf("GetArrayItemValue: NULL array\n");
83 return(0.0);
85 if(arraysize<=0)
87 printf("GetArrayItemValue: wrong arraysize (%d)\n", arraysize);
88 return(0.0);
90 if(nitem<0 || nitem>=arraysize)
92 printf("GetArrayItemValue: wrong item number (%d)\n", nitem);
93 return(0.0);
95 //----
96 return(arr[nitem]);
98 //+------------------------------------------------------------------+
99 //| |
100 //+------------------------------------------------------------------+
101 MT4_EXPFUNC BOOL __stdcall SetArrayItemValue(double *arr,const int arraysize,const int nitem,const double value)
103 //----
104 if(arr==NULL)
106 printf("GetArrayItemValue: NULL array\n");
107 return(FALSE);
109 if(arraysize<=0)
111 printf("GetArrayItemValue: wrong arraysize (%d)\n", arraysize);
112 return(FALSE);
114 if(nitem<0 || nitem>=arraysize)
116 printf("GetArrayItemValue: wrong item number (%d)\n", nitem);
117 return(FALSE);
119 //----
120 arr[nitem]=value;
121 return(TRUE);
123 //+------------------------------------------------------------------+
124 //| |
125 //+------------------------------------------------------------------+
126 MT4_EXPFUNC double __stdcall GetRatesItemValue(const RateInfo* rates,const int rates_total,const int shift,const int nrate)
128 //----
129 if(rates==NULL)
131 printf("GetRatesItemValue: NULL array\n");
132 return(0.0);
134 //----
135 if(rates_total<0)
137 printf("GetRatesItemValue: wrong rates_total number (%d)\n", rates_total);
138 return(0.0);
140 //----
141 if(shift<0 || shift>=rates_total)
143 printf("GetRatesItemValue: wrong shift number (%d)\n", shift);
144 return(0.0);
146 //----
147 if(nrate<0 || nrate>5)
149 printf("GetRatesItemValue: wrong rate index (%d)\n", nrate);
150 return(0.0);
152 //----
153 int nitem=rates_total-1-shift;
154 switch(nrate)
156 case 0: return double(rates[nitem].ctm);
157 case 1: return rates[nitem].open;
158 case 2: return rates[nitem].low;
159 case 3: return rates[nitem].high;
160 case 4: return rates[nitem].close;
161 case 5: return rates[nitem].vol;
163 //----
164 return(0.0);
166 //+------------------------------------------------------------------+
167 //| |
168 //+------------------------------------------------------------------+
169 MT4_EXPFUNC int __stdcall SortStringArray(MqlStr *arr,const int arraysize)
171 //----
172 if(arr==NULL)
174 printf("SortStringArray: NULL array\n");
175 return(-1);
177 if(arraysize<=0)
179 printf("SortStringArray: wrong arraysize (%d)\n", arraysize);
180 return(-1);
182 //----
183 qsort(arr,arraysize,sizeof(MqlStr),CompareMqlStr);
184 //----
185 return(arraysize);
187 //+------------------------------------------------------------------+
188 //| |
189 //+------------------------------------------------------------------+
190 MT4_EXPFUNC int __stdcall ProcessStringArray(MqlStr *arr,const int arraysize)
192 int len1,len2;
193 //----
194 if(arr==NULL)
196 printf("ProcessStringArray: NULL array\n");
197 return(-1);
199 if(arraysize<=0)
201 printf("ProcessStringArray: wrong arraysize (%d)\n", arraysize);
202 return(-1);
204 //----
205 for(int i=0; i<arraysize-1; i++)
207 if(arr[i].string==NULL) len1=0;
208 else len1=strlen(arr[i].string);
209 if(arr[i+1].string==NULL) len2=0;
210 else len2=strlen(arr[i+1].string);
211 //---- uninitialized string
212 if(arr[i+1].string==NULL) continue;
213 //---- destination string is uninitialized and cannot be allocated within dll
214 if(arr[i].string==NULL) continue;
215 //---- memory piece is less than needed and cannot be reallocated within dll
216 if(arr[i].len<len1+len2) continue;
217 //---- final processing
218 strcat(arr[i].string,arr[i+1].string);
220 //----
221 return(arraysize);
223 //+------------------------------------------------------------------+
224 //| |
225 //+------------------------------------------------------------------+
226 int CompareMqlStr(const void *left,const void *right)
228 MqlStr *leftstr=(MqlStr *)left;
229 MqlStr *rightstr=(MqlStr *)right;
230 //----
231 if(leftstr->string==NULL) return(-1);
232 if(rightstr->string==NULL) return(1);
233 //----
234 return(strcmp(leftstr->string,rightstr->string));
236 //+------------------------------------------------------------------+