Download MetaTrader 5
To add comments, please log in or register
Let your idle PCs do something useful. Join MQL5 Cloud Network!
sergery
281
sergery 2014.02.06 09:22 

Hello,

CopyFileA or CopyFileW not working

any advice?

//+------------------------------------------------------------------+
//|                                                testCopyFileW.mq4 |
//|                        Copyright 2013, MetaQuotes Software Corp. |
//|                                              https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2013, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict

#import "kernel32.dll"
     // bool CopyFileA(string lpExistingFileName,string lpNewFileName,bool bFailIfExists);
    bool CopyFileW(string lpExistingFileName,string lpNewFileName,bool bFailIfExists);
#import


//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart()
  {
//---   
    //--- test1 CopyFileW
    string filename ="CopyFileW.txt";
    string pathin = StringConcatenate(TerminalInfoString(TERMINAL_PATH),"\\MQL4\\Files\\",filename);
    string pathout = StringConcatenate(TerminalInfoString(TERMINAL_PATH),"\\MQL4\\",filename);     
  
    Print(pathin);
    Print(pathout);
  
     int handle = FileOpen(filename,FILE_WRITE); // create file CopyFileW.txt
     
     if(handle!=-1)
     {
         if(!CopyFileW(pathin,pathout,false))
            Print("CopyFileW error#",GetLastError()); //got error# 4051             
          FileClose(handle);  
      } 
         
  }
//+------------------------------------------------------------------+
Alain Verleyen
Moderator
28701
Alain Verleyen 2014.02.06 15:45  
sergery:

Hello,

CopyFileA or CopyFileW not working

any advice?

You have several errors.

You got error #4051 from mql5, but there is no error for mql5 and you don't use ResetLastError().

When you are using Winapi DLL call, you have to use WinApi GetLastError() to get the error.

So try this code :

#import "kernel32.dll"
     // bool CopyFileA(string lpExistingFileName,string lpNewFileName,bool bFailIfExists);
    bool CopyFileW(string lpExistingFileName,string lpNewFileName,bool bFailIfExists);
    int GetLastError(void);
#import


//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart()
  {
//---   
    //--- test1 CopyFileW
    string filename ="CopyFileW.txt";
    string pathin = StringConcatenate(TerminalInfoString(TERMINAL_PATH),"\\MQL4\\Files\\",filename);
    string pathout = StringConcatenate(TerminalInfoString(TERMINAL_PATH),"\\MQL4\\",filename);     
  
    Print(pathin);
    Print(pathout);
  
     int handle = FileOpen(filename,FILE_WRITE); // create file CopyFileW.txt
     
     if(handle!=-1)
     {
         ResetLastError();
         if(!CopyFileW(pathin,pathout,false))
            Print("CopyFileW error#",kernel32::GetLastError()," mql5 error#",::GetLastError()); //got error# 4051 
      } 
         
  }
There are still 2 errors, but with this code, you should be able to find them.
darksideofthemoon
57
darksideofthemoon 2014.02.06 17:41  
sergery:

Hello,

CopyFileA or CopyFileW not working

any advice?


In your example you have to close the opened file handle before you can copy. Or rather, note the difference here in respect to the new build: " Shared use mode should be specified explicitly using FILE_SHARE_READ and/or FILE_SHARE_WRITE "

gchrmt4
356
gchrmt4 2014.02.06 17:42  
2cent:

You have to close the opened file handle before you can copy.
... and TERMINAL_PATH should be TERMINAL_DATA_PATH (unless in /portable mode).
sergery
281
sergery 2014.02.06 18:28  

Thanks to angevoyageur,2cent,gchrmt4,eliot

The correct codes as below:

//

#property copyright "Copyright 2013, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict
#import "kernel32.dll"    
    bool CopyFileW(string lpExistingFileName,string lpNewFileName,bool bFailIfExists);
     int GetLastError(void);
#import
//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart()
  {
//---   
    //--- test1 CopyFileW
    string filename ="CopyFileW.txt";
    string pathin = StringConcatenate(TerminalInfoString(TERMINAL_DATA_PATH),"\\MQL4\\Files\\",filename); // instead of TERMINAL_PATH
    string pathout = StringConcatenate(TerminalInfoString(TERMINAL_DATA_PATH),"\\MQL4\\",filename);     
 
    Print(pathin);
    Print(pathout);
  
    int handle = FileOpen(filename,FILE_WRITE);   // create file CopyFileW.txt    
     FileClose(handle);  // before CopyFile, file shoud be closed .   
     if(handle!=-1)
     {
             ResetLastError();
             if(!CopyFileW(pathin,pathout,false))
            Print("CopyFileW error#",kernel32::GetLastError()," mql5 error#",::GetLastError()); //got error# 4051 
      }          
  }
//+------------------------------------------------------------------+

//

also when create file with FILE_SHARE_READ:

 int handle = FileOpen(filename,FILE_WRITE|FILE_SHARE_READ);   // create file CopyFileW.txt with  FILE_SHARE_READ 
    
     if(handle!=-1)
     {
             ResetLastError();
             if(!CopyFileW(pathin,pathout,false))
             Print("CopyFileW error#",kernel32::GetLastError()," mql5 error#",::GetLastError()); //got error# 4051             
             FileClose(handle);  //
      }          

//

Alain Verleyen
Moderator
28701
Alain Verleyen 2014.02.06 18:29  
2cent:

In your example you have to close the opened file handle before you can copy. Or rather, note the difference here in respect to the new build: " Shared use mode should be specified explicitly using FILE_SHARE_READ and/or FILE_SHARE_WRITE "



I think it's better to close the file.
marcelo marques pereira
75
marcelo marques pereira 2014.02.06 20:10  

HI Guys,

I have been facing almost the same issue using the CopyFileA function : I used to copy a file from c:\1 to c:\2 folder (out ot TerminalPath)

Any idea how I can go through this?

#import "kernel32.dll"
bool CopyFileA (string source_file, string destination_file, bool if_exist);//tava ok, entao mudei para o CopyFileW do sereguei
#import


string  source="C:\\1\\file.txt";    
string  dest="C:\\2\\file.txt";    

if(!CopyFileA(source, dest,true)==false)      
         Print(" Copying error (source):",source," dest:",dest);
          else Print(" OK");
          
          
marcelo marques pereira
75
marcelo marques pereira 2014.02.09 12:55  

Already solved guys using all info above. thank you.

forexwinnernet
3
forexwinnernet 2014.03.18 23:10  

This worked great for me also for a while, but since recompiling under 610 I now get system error 32 - shared access violation.

Still works as a script but when I drop it into an EA function call it fails with error 32. Any ideas?


#import "kernel32.dll"
bool CopyFileW(string lpExistingFileName,string lpNewFileName,bool bFailIfExists);
int GetLastError(void);
#import

void DuplicateFile(string SourcePath, string TargetPath)
{
//---
string pathin = StringConcatenate(SourcePath,FileName);
string pathout = StringConcatenate(TargetPath,FileName);


int handle2 = FileOpen(FileName,FILE_CSV|FILE_WRITE,';');
FileClose(handle2);
if(handle2!=-1)
{
ResetLastError();
if(!CopyFileW(pathin,pathout,false))
Print("CopyFileW error#",kernel32::GetLastError()," mql5 error#",::GetLastError()); //got error# 4051
}
}

string strSource = "C:\\Users\\Administrator\\AppData\\Roaming\\MetaQuotes\\Terminal\\3BD2B5E5A5264AFE17C1E2DDC7D6B381\\MQL4\\Files\\";
DuplicateFile(strSource,"C:\\Users\\Administrator\\AppData\\Roaming\\MetaQuotes\\Terminal\\F354DCCD667FB40D8D6DE5A4406D8652\\MQL4\\Files\\");

qjol
3243
qjol 2014.03.19 05:11  
ForexWinnerNet:

This worked great for me also for a while, but since recompiling under 610 I now get system error 32 - shared access violation.

Still works as a script but when I drop it into an EA function call it fails with error 32. Any ideas?


didn't you read what was written above ?

ERROR_SHARING_VIOLATION

32 (0x20)

The process cannot access the file because it is being used by another process.

/
To add comments, please log in or register