Code>something about "import DLL"

 

You may import any Windows Dynamic Link Library (DLL) into your EA/Script, but, in my experience, it is easy to do that for the SIMPLE type like "int", but hard for "string". Watch this:

#import "user32.dll"

        int MessageBoxA( int, string, string, int );

#import


const string MyText = "Hello";

const string MyTitle = "Test";


void OnStart()

{

        MessageBoxA( 0, MyText, MyTitle, 0 );

}


Test it, you may see that only one character ( the first character of string to show ) is presented, where do the rest go ? The answer is, the type you declared inside "import", "string", is not suitable for the export of the DLL and the calling of your code, try this:

#import "user32.dll"
        int MessageBoxA( int, char&, char&, int );
#import

void OnStart()
{
        char Txt[6];
        char Ttl[1];

        Txt[0] = 'H'; Txt[1] = 'e'; Txt[2] = 'l'; Txt[3] = 'l'; Txt[4] = 'o'; Txt[5] = '\0';
        Ttl[0] = '\0';

        MessageBoxA( 0, Txt[0], Ttl[0], 0 );
}

Note that "string" is changed to be "char&" now, and null terminator ('\0') is applied to the end of the character array, that is also called as "C style string".

Now, it should work properly, with luck. Anyway, it works well in my system, a messagebox with "Hello" is there.


However, it is really a bit clumsy, so I find out another smarter way...

#import "user32.dll"
        int MessageBoxA( int, uchar&[], uchar&[], int );
#import

const string Text = "Hello";
const string Title = "Test";

void OnStart()
  { 
   uchar Txt[];
   uchar Ttl[];

   StringToCharArray( Text, Txt, 0, -1 );
   StringToCharArray( Title, Ttl, 0, -1 );
  
   MessageBoxA( 0, Txt, Ttl, 0 );
  }


Now "uchar&[]" replaces of "char&". Type of "uchar" works for ASCII character which is converted from Unicode String that MetaTrader system used, and, the "&[]" means that it is a pointer for the "uchar" array.


The key is, using "character array", and a pointer which points to that arry, but not "string", actually,  that is the way of Windows API.

 
Didn't you try MessageBoxW?
 
lippmaje:
Didn't you try MessageBoxW?
Not yet. But it should be same...I think.
 
Tang Sun:
Not yet. But it should be same...I think.
Why do you think so, did you check it?
 

Previous sample toke MessageBoxA function which works for "traditional" character -- ASCII , this time, try MessageBoxW, for Unicode character.

...
int MessageBoxW( int, ushort&[], ushort&[], int );
...

void OnStart()
  {
...
   ushort Txt[];
   ushort Tl[];

  StringToShortArray( Text, Txt, 0, -1);
  StringToShortArray( Title, Tl, 0, -1);

  MessageBoxW( 0, Txt, Tl, 0 );
  }

As you see, ushort replaces  uchar , now it works well for Unicode String. So, be careful of type of character.

Reason: