Download MetaTrader 5

qestion about passing the array of string into DLL

To add comments, please log in or register
Documentation is available in CHM and PDF. Download it in any convenient form!
FuliangQ
52
FuliangQ 2010.08.14 13:05 

hi,

  i encounter a question about passing string's array into a DLL ,

  first i declare the funtion in .mqh as follows : void f(string& array[]) ;  , but this failed .

  and then i  try to do this by declare the function : void f(string& str) ;

  with a for loop , i push string array into dll as follows :

                .......

               string str[5]= {"sddsa" , "ddass" , "dsdfasdwe" , "wwer2" , "e23"} ;

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

                    f(str[i]) ;

  but   in the function f , i didn't get the right value of the str[i] , and  i print the address of str :   

                addr0  = 038C8D48

                addr1  = 038C8D54

                addr2  = 038C8D60

                addr3  = 038C8D6C

                addr4  = 038C8D78 

so , my question is how to access the element in a string's array .

 

thanks in advance . 

Rashid Umarov
Admin
11995
Rashid Umarov 2010.08.14 13:51  
FuliangQ
52
FuliangQ 2010.08.14 15:58  

 

yes , i have read , and i know i can pass a string with the form f(string& str) ;

but i found i can not pass the array of strings , eff f(string& sting[]) ;

so if i can not  access the string's array of MT5 in DLL  .

thanks . 

Andrey Voytenko
23107
Andrey Voytenko 2010.08.14 19:11  

In this section MQL5 Help is written:

Classes, string arrays or complex objects that contain strings and/or dynamic arrays of any types cannot be passed as a parameter to functions imported from DLL.

But you can use array of char for ANSI string declaration.

MQL5 script:

//+------------------------------------------------------------------+
//|                                                Array_strings.mq5 |
//|                        Copyright 2010, MetaQuotes Software Corp. |
//|                                              http://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2010, MetaQuotes Software Corp."
#property link      "http://www.mql5.com"
#property version   "1.00"

struct char_array
{
   char a[256];
   char b[256];
   char c[256];
};

char_array my_ar;


#import "mylib.dll"
   void f(char_array &my_ar); //pass structure which contains the ANSI strings
#import

//+------------------------------------------------------------------+
void OnStart()
//+------------------------------------------------------------------+
  {
   // initialize strings
   string str1="first string";
      StringToCharArray(str1, my_ar.a);
   str1="second string";     
      StringToCharArray(str1, my_ar.b);
   str1="third string";    
      StringToCharArray(str1, my_ar.c);
   
   
   func(my_ar);// print and change first string (my_ar.a) in MQL code 
   f(my_ar);   // change first string (my_ar.a) in DLL code 
   
   Print(CharArrayToString(my_ar.a));
     
  }

//+------------------------------------------------------------------+
void func(char_array &my_ar)
//+------------------------------------------------------------------+
{
   string str;
   str = CharArrayToString(my_ar.a);
   Print(str);                    // print string  
   str = str + " + add string";    // change string 
   StringToCharArray(str,my_ar.a); // save changes
}

DLL code

#include <windows.h>

struct char_array
{
   char a[256];
   char b[256];
   char c[256];
};

void __stdcall f(char_array &my_ar)
 {
         strcpy(&my_ar.a[0], "my new string");// change string
 }

 

 

 

FuliangQ
52
FuliangQ 2010.08.15 07:21  
avoitenko:

In this section MQL5 Help is written:

But you can use array of char for ANSI string declaration.

MQL5 script:

DLL code

thank you very much , good !
To add comments, please log in or register