fileclose problem.

 

i met a problem, at same condition, some time it comes, and some time it didn't come.

here is the code.

i use ea create this sample first.

i create a dll file, which has a function which read some data form a sample.txt.

code like this

//ea code
//...import the dll function readfromfile()



int handle = fileopen("example.txt")
if(handle>0)
{
   filewrite(handle,"test");

}

fileclose(handle);//now it should closed the file.and the after this dll should can read from this file.

readfromfile(); // dll throw out a error. can't open file 

// the error is throwed here.   

/*
FILE *pf = fopen("experts\\files\\example.txt","r");
if(pf==NULL)
{
MessageBox(NULL,"can\'t open example.txt");
exit(0);
}

*/

everytime the message shows the dll can't open file, i checked the directory, the example.txt it is there, but the dll can't open it.

i think must be the fileclosed havn't closed it in time.

how to fix this?? thanks for any advices.

 

In what language have you written your DLL? if it is Matlab or C#, you can't call their functions. Only C++ code you can call (and native ones). I'm very weak in C++, I founded one code that uses C++ methods in MQL4, but editing is a problem (for me...).

 
flourishing:

FILE *pf = fopen("experts\\files\\example.txt","r");

everytime the message shows the dll can't open file, i checked the directory, the example.txt it is there, but the dll can't open it.

i think must be the fileclosed havn't closed it in time.

how to fix this?? thanks for any advices.

Firstly, I'd try calling the Windows API function GetLastError() after the fopen(). That should tell you exactly what the file problem is - because fopen() is basically a wrapper around CreateFile(), and that sets the Win32 API last error.


However, my guess is that "experts\\files\\example.txt" is wrong. When I try calling a DLL from MetaTrader the current directory is set to My Documents, not \Program Files\<metatrader instance>. I think you'll need to pass the location of MetaTrader into the DLL, using MQL's TerminalPath() function, and then build a full file path rather than a relative one.

 
jjc wrote >>

Firstly, I'd try calling the Windows API function GetLastError() after the fopen(). That should tell you exactly what the file problem is - because fopen() is basically a wrapper around CreateFile(), and that sets the Win32 API last error.

However, my guess is that "experts\\files\\example.txt" is wrong. When I try calling a DLL from MetaTrader the current directory is set to My Documents, not \Program Files\<metatrader instance>. I think you'll need to pass the location of MetaTrader into the DLL, using MQL's TerminalPath() function, and then build a full file path rather than a relative one.

thank you .guys. especially jjc.

your advice is good, i will check lasterror. but the path problem, i have tested too. if use dll write a file, the path is program files\metatrader instance. in my code is

\\experts\\files\\example.txt. i have tested that too, if i don't call open file from ea, dll call always works fine, so must be the file function problem in ea .

thank you very much.

stradz, c# dll is not a really dll, it is depend on .net framework. so if you write dll for mt4 use, you must write it with c++ or c, some one also use delphi too. hope this can help you.

 
flourishing wrote >>

thank you .guys. especially jjc.

your advice is good, i will check lasterror. but the path problem, i have tested too. if use dll write a file, the path is program files\metatrader instance. in my code is

\\experts\\files\\example.txt. i have tested that too, if i don't call open file from ea, dll call always works fine, so must be the file function problem in ea .

thank you very much.

stradz, c# dll is not a really dll, it is depend on .net framework. so if you write dll for mt4 use, you must write it with c++ or c, some one also use delphi too. hope this can help you.

yes, Delphi is great :-) in my personal opinion of course!

Reason: