The new MQL4 syntax - page 4

 
Correct, unles there is a very good reason, only bool, int, double and string are in my code. I would not bother with -1 in indexes, otherwise I needed to cast every integer that comes closer to this index.
 
RaptorUK:

uchar - Unsigned Character, why would you use this for a loop ? it doesn't make sense to me . . . use an int. You will be working with ulongs, that is what a new datetime is . . . and when you typecast without thinking about it in future you will get warned . . . deal with the warning or ignore it. Don't just hope for the best though, do as you are doing now, learn and understand

The stuff you posted from stackoverflow makes sense to me, I think it is good advice.

uchar was just an example as my question was related to the use of small variable types. uchar is a 8 bit positive value from 0 to 255 so for a loop of 0 to 100 you could use the 8 bit uchar instead of a 32 bit integer, if you wanted to.
 
SDC:
uchar was just an example as my question was related to the use of small variable types. uchar is a 1 byte positive value from 0 to 255 so for a loop 0 to 100 you could use the 1 byte uchar.
Yes you could . . . but why would you ? it will work but why does it make logical sense to use a variable type intended for character values for a loop counter ?

If you need to save a value returned from a function and the function in question returns a uchar then use a uchar variable to save the returned value . . . with the original mql4 this was a non issue, it will more of an issue with the new mql4.
 

When I asked the question I didnt know why I would want to that is why I asked the question lol...

I didnt know if an 8 bit variable type would process faster than a 32 bit or if it would be slower or just the same, I didnt know if an 8 bit value would use less RAM or less disk space. seeing as we now have those small variable types available to us I was just interested to know the pros and cons of using them when they would fit the requirements of the code block, vs just sticking with 32 bit integers across the board.

I thought maybe a 32 bit OS could process four 8bit values at the same time but I didnt know. Apparently not. This explains why a 64 bit OS is not really much faster than a 32 bit except for the fact it can access more RAM. I had often wondered about that.

 

I have a question, which I have not found solution for.

How to use void& ? I mean, if I need to send any pointer to a DLL. In the help file there are functions, which use this void type, but if I place it anywhere in the source, it won't compile. I tried to use template as a workaround, but template is prohibited within #import statement.

I managed construction for single types, but I am not able to implement passing void& array pointer to the DLL, unless I specify the explicit type.

int  FileReadArray(
   int    file_handle,               // File handle
   void&  array[],                   // Array to record
   int    start=0,                   // start array position to write
   int    count=WHOLE_ARRAY          // count to read
);
 
 

Is there any limitation for passing array of structures to a DLL?

Suppose MT4Structure is a simple structure.

Having imported the kernel32.dll, a single structure works just fine:

bool ReadFile(
    /*_In_         HANDLE*/ int hFile,
    /*_Out_        LPVOID*/ MT4Structure& lpBuffer,
    /*_In_         DWORD*/ uint nNumberOfBytesToRead,
    /*_Out_opt_    LPDWORD*/ uint& lpNumberOfBytesRead[],
    /*_Inout_opt_  LPOVERLAPPED*/int lpOverlapped
);

but I am not able to get into work accepting the array of structures. The compiler won't compile with MT4Structure&[] if sent as a parameter to this declaration:

bool ReadFile(int, MT4Structure&[], uint, uint&[],int);
 
Ovo:

Is there any limitation for passing array of structures to a DLL?

I can't see a problem with something like this:

struct TestStruct {
   int Integer;
   uchar Byte;
   double Real;
};

#import "ExampleDllWhichReceivesStructureArray.dll"
   void Test(TestStruct &[]);
#import 

It works as expected (provided that you allow for different default structure-alignment in MT4 and the DLL).

(BTW, I'm not actually sure what the internal representation of a MQL4 bool now is, but I'd prefer to declare Win32 functions as returning int. Win32 functions return a BOOL rather than a C++ bool. A BOOL is a Windows macro for a 4-byte integer, whose value is 1/0, whereas a C++ bool is a single byte. If you tell MT4 to expect a single byte of return-value from functions when they are actually returning four bytes then you could - though it's very unlikely - end up with stack corruption.)

 
Ovo:

How to use void& ? I mean, if I need to send any pointer to a DLL.

I'm not quite sure what your question is. For example, the documentation of the Win32 API uses void* to mean "this accepts any type of pointer to anything; the Win32 function doesn't care what your block of memory is".

 
gchrmt4:

I'm not quite sure what your question is. For example, the documentation of the Win32 API uses void* to mean "this accepts any type of pointer to anything; the Win32 function doesn't care what your block of memory is".


Exactly. But it seems it is not quite possible with the MQL4, and using a template is not allowed within #import block. So using additional structure with winapi turns to manual update of the #import declarations every time a new type is needed. Which is rather uncomfortable for me, since my intention was managing the imports in one place.
 
Ovo:

Exactly. But it seems it is not quite possible with the MQL4, and using a template is not allowed within #import block. So using additional structure with winapi turns to manual update of the #import declarations every time a new type is needed. Which is rather uncomfortable for me.
Still not sure if I understand but... I don't think you can declare the MQL4 import as void*. You have to use a specific data type in the import, and you can then only use the DLL import with that data type. If you want to use the same DLL function with different data types, then you have a problem unless there is a workaround such as https://www.mql5.com/en/forum/148934
Reason: