• Home
  • Forums
  • Trades
  • News
  • Calendar
  • Market
  • Brokers
  • Login
  • Join
  • User/Email: Password:
  • 9:13am
Menu
  • Forums
  • Trades
  • News
  • Calendar
  • Market
  • Brokers
  • Login
  • Join
  • 9:13am
Sister Sites
  • Metals Mine
  • Energy EXCH
  • Crypto Craft

Options

Bookmark Thread

First Page First Unread Last Page Last Post

Print Thread

Similar Threads

Wininet.dll & user32.dll. Can we use it in Macintosh MT4/MT5? 0 replies

How to read last alert strings using winapi in my mt4? 3 replies

Multiple EAs using single DLL with DLL Windows - Using Delphi 9 replies

Reading strings in DLL 0 replies

Passing strings from indicator to EA? 1 reply

  • Platform Tech
  • /
  • Reply to Thread
  • Subscribe

Returning strings from DLL

  • Post #1
  • Quote
  • First Post: Edited at 10:03am Jul 6, 2009 9:55am | Edited at 10:03am
  •  not.likely
  • | Joined Aug 2008 | Status: Member | 4 Posts
Hi all,

I am hoping someone can help solve this one: How do I return a string to MT4 from a dll function?

You cannot allocate memory space in MQL so I can easily crash MT4 by sending back a long string. If you allocate memory in the dll the result is a memory leak?

I'd be really grateful for some assistance and a small sample function.


TIA edit: Could someone also show how to return a single string and an array of strings? Thanks.
  • Post #2
  • Quote
  • Jul 6, 2009 10:26am Jul 6, 2009 10:26am
  •  Skyhook
  • | Joined Sep 2006 | Status: Elevating the way to success | 585 Posts
I know it's not the cleanest of setups, or most efficient, but you could have the dll output to a log file and have the MT4 code check the log file. Like I said, not the cleanest setup, but still doable.
 
 
  • Post #3
  • Quote
  • Edited at 11:20am Jul 6, 2009 11:18am | Edited at 11:20am
  •  Kenz987
  • Joined Aug 2006 | Status: Member | 737 Posts
search for 4xcoder's post on internal files - pipes. Good example. you have to convert it to an integer array and he shows you how to do that.
ps: look here
http://www.forexfactory.com/showthread.php?t=125117
 
 
  • Post #4
  • Quote
  • Jul 6, 2009 12:22pm Jul 6, 2009 12:22pm
  •  not.likely
  • | Joined Aug 2008 | Status: Member | 4 Posts
Thanks for that link... but it doesn't show the whole answer, is it possible to efficiently pass strings (arrays of char) and arrays of strings to/from a DLL for MT4?
 
 
  • Post #5
  • Quote
  • Jul 6, 2009 12:40pm Jul 6, 2009 12:40pm
  •  Kenz987
  • Joined Aug 2006 | Status: Member | 737 Posts
convert the string to an int array with CopyToBuffer,
send it to MT4, where
you convert it back to a string with StringFromBuffer
he gives you the code. it works.
 
 
  • Post #6
  • Quote
  • Jul 7, 2009 2:55am Jul 7, 2009 2:55am
  •  rangebound
  • Joined Aug 2006 | Status: Member | 236 Posts
You dont say what language you're using for your DLL so a direct example isnt easy. The following applies to all though :

Mt4 will quite happily allocate up to 64Kb for a Null terminated string so all your DLL has to do is pass back a pointer to (the address of) the first character of your string.

The string can contain any data as long as it abides by the Null terminated rule ... IE the first zero byte will determine the end of the string as far as MT4 is concerned.

so simple statements like

#import "your.dll"
string getdllstring();

string tstr = getdllstring();

will have mt4 locally allocate memory and copy the string.

More important is how your DLL recieves strings from Mt4

Again Mt4 will send your DLL a pointer to the first character of a string ...
so to pass a 20Kb string to your DLL

#import "your.dll"
void sendstrtodll(string s);

string tstr;

for (int x=0;x<20*1024;x++)
{ tstr = tstr +"X"

sendstrtodll(tstr);

your DLL MUST now allocate the (unknown amount of) memory
in which to copy to the string to. The size can obtained using standard Null terminted string routines in your DLLs native language EG


len = strlen(s);

or by copying the pointer passed from Mt4 and iterating till a zero byte is found.

As far as arrays of string goes simply do something like :

String manystrings[];
int totstrings;

string tstr;

totstrings = 0;
tstr = getdllstring();
while (tstr !="")
{
totstrings++;
arrayresize(manystrings,totstrings);
manystrings[totstrings-1]= tstr;
tstr = getdllstring();
}

of course you could add parameters to the getdllstring function and add other functions if your dll uses an array of strings rather than a linked list.

eg.

int totstrings = getdllnumofstrings();
arrayresize(manystrings,totstrings-1);

for (int y=0;y<totstrings;y++)
{
manystrings[y] = getdllstring(y);
}
 
 
  • Post #7
  • Quote
  • Jul 7, 2009 11:35am Jul 7, 2009 11:35am
  •  not.likely
  • | Joined Aug 2008 | Status: Member | 4 Posts
Merci rangebound, I must have forgotten to mention a C/C++ dll.
 
 
  • Post #8
  • Quote
  • Jul 10, 2010 3:02am Jul 10, 2010 3:02am
  •  dinhero
  • | Joined Jan 2010 | Status: Member | 128 Posts
Quoting Kenz987
Disliked
convert the string to an int array with CopyToBuffer,
send it to MT4, where
you convert it back to a string with StringFromBuffer
he gives you the code. it works.
Ignored
This would be so great ! but where to find those function : CopyToBuffer ??
 
 
  • Post #9
  • Quote
  • Jul 10, 2010 3:38am Jul 10, 2010 3:38am
  •  dinhero
  • | Joined Jan 2010 | Status: Member | 128 Posts
Quoting rangebound
Disliked
You dont say what language you're using for your DLL so a direct example isnt easy. The following applies to all though :

Mt4 will quite happily allocate up to 64Kb for a Null terminated string so all your DLL has to do is pass back a pointer to (the address of) the first character of your string.

The string can contain any data as long as it abides by the Null terminated rule ... IE the first zero byte will determine the end of the string as far as MT4 is concerned.

so simple statements like

#import "your.dll"
string getdllstring();

string...
Ignored
THIS IS INTERESTING AND CONSISTENT !

do you have any complete sample code ? (I know it is asking a lot !)

But I will just try this, and post the code if it works !

Thanks a lot !

PS : DELPHIIIIIIIIIIIIIIIIIi
 
 
  • Post #10
  • Quote
  • Aug 1, 2010 10:30am Aug 1, 2010 10:30am
  •  dwmcqueen
  • | Joined Oct 2005 | Status: Member | 11 Posts
I have a question - I am trying to return an array of strings. I believe I saw somewhere that an array of strings is returned as a defined MqlStr object, not a null terminated address. Is this correct? Or any examples for returning an array of strings.

Secndly, looking at the conversion of a string to an int - it looks like that would allow for a better memory allocation (in case the length of the string is unknown when initializing the return in MT4) - is this correct? Is this a preferred way to return a string (instead of null terminated since that requires an initialization to a set length)?
 
 
  • Post #11
  • Quote
  • Last Post: Mar 18, 2017 3:56am Mar 18, 2017 3:56am
  •  thachvan
  • | Joined Mar 2017 | Status: Junior Member | 1 Post
This thread is very old but it appears on the first page of Google search result when searching about getting string from DLL. So I'd like to post my solution here to make it be easy for others. It's very simple.

* C++:

#define MT4EXPORT extern "C" __declspec(dllexport)

MT4EXPORT void getText(char* buffer)
{
char* text = "ABCD";
strcpy(buffer, text);
}

* MQL:

#import "MT4 DLL.dll"
void getText(char&[]);
#import

int OnInit()
{
char buffer[1024];
getText(buffer);
string text=CharArrayToString(buffer);
Print(text); // ABCD

return(INIT_SUCCEEDED);
}
 
 
  • Platform Tech
  • /
  • Returning strings from DLL
  • Reply to Thread
0 traders viewing now
Top of Page
  • Facebook
  • Twitter
About FF
  • Mission
  • Products
  • User Guide
  • Media Kit
  • Blog
  • Contact
FF Products
  • Forums
  • Trades
  • Calendar
  • News
  • Market
  • Brokers
  • Trade Explorer
FF Website
  • Homepage
  • Search
  • Members
  • Report a Bug
Follow FF
  • Facebook
  • Twitter

FF Sister Sites:

  • Metals Mine
  • Energy EXCH
  • Crypto Craft

Forex Factory® is a brand of Fair Economy, Inc.

Terms of Service / ©2022