Download MetaTrader 5

how EA can open a file in dll

To add comments, please log in or register
ctycheer
33
ctycheer 2013.09.11 02:25 

hello. I meet a trouble about dll.

I make a dll in C++. the dll open a file for get  data.

in my C++ project ,I can use the dll for opening a file in the Metatrader 5 data path to get data.

But. I  can't do the same things in Metatrader5. I make a scripts in Metatrader5 ,the scripts use the dll to open a file to get data. But it's not work.  My OS is  Windows7.

who could tell me why?

how can I change data between dll and EA 

Alain Verleyen
Moderator
31091
Alain Verleyen 2013.09.11 07:46  
ctycheer:

hello. I meet a trouble about dll.

I make a dll in C++. the dll open a file for get  data.

in my C++ project ,I can use the dll for opening a file in the Metatrader 5 data path to get data.

But. I  can't do the same things in Metatrader5. I make a scripts in Metatrader5 ,the scripts use the dll to open a file to get data. But it's not work.  My OS is  Windows7.

who could tell me why?

how can I change data between dll and EA 

https://www.mql5.com/en/articles/18
How to Exchange Data: A DLL for MQL5 in 10 Minutes
How to Exchange Data: A DLL for MQL5 in 10 Minutes
  • 2010.01.27
  • MetaQuotes Software Corp.
  • www.mql5.com
Now not so many developers remember how to write a simple DLL, and what are special features of different system binding. Using several examples, I will try to show the entire process of the simple DLL's creation in 10 minutes, as well as to discuss some technical details of our binding implementation. I will show the step-by-step process of DLL creation in Visual Studio with examples of exchanging different types of variables (numbers, arrays, strings, etc.). Besides I will explain how to protect your client terminal from crashes in custom DLLs.
ctycheer
33
ctycheer 2013.09.11 10:08  

Thank you very much for the answer!

I find the article and make some sample dll successful.

But  my trouble is the dll can not open a file if it is used in meta trader5. Likely opening a file in dll is prohibited

Alain Verleyen
Moderator
31091
Alain Verleyen 2013.09.11 11:52  
ctycheer:

Thank you very much for the answer!

I find the article and make some sample dll successful.

But  my trouble is the dll can not open a file if it is used in meta trader5. Likely opening a file in dll is prohibited

I suppose the DLL work in the sandbox of MT5, so you can't open file outside some directories. I don't try though.
ctycheer
33
ctycheer 2013.09.11 13:07  
angevoyageur:
I suppose the DLL work in the sandbox of MT5, so you can't open file outside some directories. I don't try though.

I followed the article https://www.mql5.com/en/docs/files/fileopen

so I put the file in the subfolder MQL5\files.

if not using the dll, the scripts can open the file.

however using the dll in the scripts can't open the same file. That's very strange!  I don't know why?

Documentation on MQL5: File Functions / FileOpen
Documentation on MQL5: File Functions / FileOpen
  • www.mql5.com
File Functions / FileOpen - Documentation on MQL5
Jian Chen
Moderator
3646
Jian Chen 2013.09.11 14:05  
ctycheer:

I followed the article https://www.mql5.com/en/docs/files/fileopen

so I put the file in the subfolder MQL5\files.

if not using the dll, the scripts can open the file.

however using the dll in the scripts can't open the same file. That's very strange!  I don't know why?

I just made a test DLL and it could open file successfully.

It may caused by the relative path issue.

For example, you open an file for writing. If you don't give the file name an absolute path (test.txt but not d:\test.txt), the file is generated at the MT5 installation directory. If you specified the absolute path, the file will be created successfully.

For the same reason, if you need to open the file for reading, you also need to specify its path correctly. 

 

P.S.  In my test DLL, I was using "fopen" to open files. 

Documentation on MQL5: File Functions / FileOpen
Documentation on MQL5: File Functions / FileOpen
  • www.mql5.com
File Functions / FileOpen - Documentation on MQL5
ctycheer
33
ctycheer 2013.09.15 02:38  
forex2start:

I just made a test DLL and it could open file successfully.

It may caused by the relative path issue.

For example, you open an file for writing. If you don't give the file name an absolute path (test.txt but not d:\test.txt), the file is generated at the MT5 installation directory. If you specified the absolute path, the file will be created successfully.

For the same reason, if you need to open the file for reading, you also need to specify its path correctly. 

 

P.S.  In my test DLL, I was using "fopen" to open files. 

Hello,I don't know why my dlll still don't work even if I use "fopen".

My OS is windows7 ,vs2008 

here is my files,

DLL FILE

testmqlfill.h

#ifdef __cplusplus
extern "C"{
#endif
char* cty_openfile(char* filename);
int cty_file_exists(char* filename);

#ifdef __cplusplus
}
#endif

 testmqlfill.cpp

#include <stdio.h>
#include <io.h>
#include "testmqlfile.h"

char* cty_openfile(char* filename)
{
        FILE *fp; 
        char* k;
        fp = fopen(filename,"r");
        
        k = "success";
        if(fp==NULL)
        {
                k = filename;
                return k;
        }
        fclose(fp);

        return k;
        
}

int cty_file_exists(char* filename)
{
        return _access(filename,0);
}

 testmqlfile.def

LIBRARY "testmql5file"
EXPORTS
        cty_openfile @1
        cty_file_exists @2

 the dll can work normallly  in a test  C++ Project

 

next is my scripts in Metatrader5

//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
#import "testmql5file.dll"
string cty_openfile(string model_file_name );
#import
void OnStart()
  {
//---
   string i;
   string r;
   i = "C:\\Users\\cty\\AppData\\Roaming\\MetaQuotes\\Terminal\\D0E8209F77C8CF37AD8BF550E51FF075\\MQL5\\Libraries\\cty.txt";
   r = cty_openfile(i);
   
   printf("%s",r);
  }
//+------------------------------------------------------------------+

 the dll I put in "C:\\Users\\cty\\AppData\\Roaming\\MetaQuotes\\Terminal\\D0E8209F77C8CF37AD8BF550E51FF075\\MQL5\\Libraries"

 

if you run the scripts, you will find it can't open the file, just return the file name 

Jian Chen
Moderator
3646
Jian Chen 2013.09.15 03:20  
#ifdef __cplusplus
extern "C"{
#endif
char* cty_openfile(char* filename);
int cty_file_exists(char* filename);

#ifdef __cplusplus
}
#endif

Maybe you should use a UNICODE version of code, that is, use "wchar_t" instead of "char". 

And you had better use "__stdcall" mode to implement your functions. 

For example:

#ifdef __cplusplus
extern "C"{
#endif
wchar_t* __stdcall cty_openfile(wchar_t* filename);
int __stdcall cty_file_exists(wchar_t* filename);

#ifdef __cplusplus
}
#endif
ctycheer
33
ctycheer 2013.09.15 06:22  
forex2start:

Maybe you should use a UNICODE version of code, that is, use "wchar_t" instead of "char". 

And you had better use "__stdcall" mode to implement your functions. 

For example:

hello,thank you very much.

 I try your method and used the __stdcall, but the dll also doesn't work.

I can't use the  "wchar_t",because it couldn't be a parameter in the function "fopen"

ctycheer
33
ctycheer 2013.09.15 06:23  
forex2start:

I just made a test DLL and it could open file successfully.

It may caused by the relative path issue.

For example, you open an file for writing. If you don't give the file name an absolute path (test.txt but not d:\test.txt), the file is generated at the MT5 installation directory. If you specified the absolute path, the file will be created successfully.

For the same reason, if you need to open the file for reading, you also need to specify its path correctly. 

 

P.S.  In my test DLL, I was using "fopen" to open files. 

So could you show me the code you say can open the file? 
Jian Chen
Moderator
3646
Jian Chen 2013.09.15 09:00  
ctycheer:
So could you show me the code you say can open the file? 

You may use _wfopen function to open file with "wchar_t *" type filename.

And this is my code:

// TestDLL.cpp : Defines the exported functions for the DLL application.
//

#include "stdafx.h"
#include <stdio.h>

extern "C" __declspec(dllexport) void __stdcall TestFile(wchar_t *szFile)
{
        if(szFile!=NULL && *szFile)
        {
                FILE *fp = _wfopen(szFile, L"w");
                if(fp==NULL)
                {
                        //fp = fopen("D:\\Program Files\\MetaTrader 5\\MQL5\\Files\\Hello1.txt", "w");
                        fp = fopen("D:\\Hello1.txt", "w");
                }
                if(fp)
                {
                        fprintf(fp, "Hello!\r\n");
                        fclose(fp);
                }
        }
}

 And it is a simple VC++ DLL project, no .def file required.

12
To add comments, please log in or register