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
- 2010.01.27
- MetaQuotes Software Corp.
- www.mql5.com
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
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.
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?
- www.mql5.com
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.
- www.mql5.com
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
#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
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"
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?
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.
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
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