DLL Input Parameters

 

Hi, I'm trying to call an external DLL I wrote and am having trouble getting some parameter types to work. In my DLL, I have the following function:


MT4_EXPFUNC void __stdcall DPOOptimize(const RateInfo* rates, const int intNRates, double dblLatestSpread, int intTotalSampleCount, int intSamplesToAnalyze,  
		bool blnAutoGenLwrBrkLim, bool blnAutoGenUprBrkLim, 
                int* chosenN, double* chosenUpperBreakout, double* chosenLowerBreakout, double* profit, int* decision) {

	// ... contents omitted ... //
  }


To call this function from my EA, I am using the following function definition:


#import "cuda_fx.dll"
   void DPOOptimize(double rates[][6], int intNRates, double dblLatestSpread, int intTotalSampleCount, int intSamplesToAnalyze,  
		bool blnAutoGenUprBrkLim, bool blnAutoGenLwrBrkLim,
		int chosenN[], double chosenUpperBreakout[], double chosenLowerBreakout[], double profit[], int decision[]);


Questions/Problems:


1. Do I really have to use arrays in the EA for the final 5 arguments of the DLL function? Each array only contains one value and I am using them to act as pointers by just using the array name when calling the function (e.g. 'chosenN' instead of 'chosenN[0]') which seems to work but makes the code a bit messy.


2. The boolean parameters do not show up in the DLL correctly. They are always false in the DLL no matter what value I use in the EA. Does anyone know why this is?


Thanks for your help,


John

 
pazzelli:

1. Do I really have to use arrays in the EA for the final 5 arguments of the DLL function?

Yes. MQL can't send non-array parameters by reference (or doesn't do it properly). However, you don't have to declare the parameters as arrays in the DLL, only in the MQL code. See https://www.mql5.com/en/forum/118434 for an earlier discussion of this.


2. The boolean parameters do not show up in the DLL correctly. They are always false in the DLL no matter what value I use in the EA. Does anyone know why this is?

Almost certainly because of the difference between bool and BOOL. A bool is a datatype which only exists in C++, not C, and occupies 1 byte. A BOOL is a datatype declared in the Win32 API, and is simply an int - i.e. 4 bytes. Most Win32 software represents boolean values as BOOL rather than bool in order to maintain compatibility with the Win32 API. For example, MQL passes boolean values to DLLs as BOOL, not bool.


Broadly speaking, your function is expecting to receive 1-byte parameters but is instead getting 4-byte parameters on the stack. See https://www.mql5.com/en/forum/118710 for an earlier discussion of a similar problem.

 
jjc:

Yes. MQL can't send non-array parameters by reference (or doesn't do it properly). However, you don't have to declare the parameters as arrays in the DLL, only in the MQL code. See https://www.mql5.com/en/forum/118434 for an earlier discussion of this.


Almost certainly because of the difference between bool and BOOL. A bool is a datatype which only exists in C++, not C, and occupies 1 byte. A BOOL is a datatype declared in the Win32 API, and is simply an int - i.e. 4 bytes. Most Win32 software represents boolean values as BOOL rather than bool in order to maintain compatibility with the Win32 API. For example, MQL passes boolean values to DLLs as BOOL, not bool.


Broadly speaking, your function is expecting to receive 1-byte parameters but is instead getting 4-byte parameters on the stack. See https://www.mql5.com/en/forum/118710 for an earlier discussion of a similar problem.


Thank you so much, this is so incredibly helpful! I did try searching for related forum posts earlier but I couldn't tell that my questions were answered there based on the forum post subject lines (sorry for asking duplicate questions).


It's too bad about the bool/BOOL thing, though I suspected it would be storage size related as I've seen problems like this in the past. I've never done any Win32 API programming and wasn't expecting a boolean to be stored as 4 bytes. Since my DLL makes extensive use of the one-byte bool type, unless you know of a better way, I guess I'll convert the DLL function header to use BOOL and then just cast it to bool internally before using it.


Thanks again for your prompt and kind assistance.

 
pazzelli:

It's too bad about the bool/BOOL thing [...] Since my DLL makes extensive use of the one-byte bool type, unless you know of a better way, I guess I'll convert the DLL function header to use BOOL and then just cast it to bool internally before using it.

That should work, but can you not just do a search-and-replace to turn bool into BOOL? You could even do "#define bool int"...

 
jjc:

That should work, but can you not just do a search-and-replace to turn bool into BOOL? You could even do "#define bool int"...

Yes I may actually do that. My original thinking was to do the type casting because the DLL uses CUDA algorithms for high performance computing and the algorithms are very sensitive to changes in space requirements (both the algorithm design and performance could change as a result). But since the boolean values aren't really used in the high performance sections of the code, I'll probably do the search/replace instead.


Thanks again for your help!

 

is there any docs on acceptable DLL function parameters?

get stack damage when using long data type.

Reason: