Damn old Kernel32.dll: FindFirstFileW

 

Hi,

I am trying to get the list of files in a nonMT4-Folder for which I need Windows' kernel32.dll functions.

Well there is an old thread to start: https://www.mql5.com/en/forum/103578 and https://www.mql5.com/de/articles/1543 for pre b600+

I just tried to change what has to be changed and replaced the buffer-function by the build-in-functions, but I still don't get any name... :(

//+------------------------------------------------------------------+
//|                                        test_WinFindFileFirst.mq4 |
//|                        Copyright 2014, MetaQuotes Software Corp. |
//|                                              http://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2014, MetaQuotes Software Corp."
#property link      "http://www.mql5.com"
#property version   "1.00"
#property strict
//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
#define TYP short
#import "kernel32.dll"
int  FindFirstFileW(string path, TYP& answer[]);
bool FindNextFileW(int handle, TYP& answer[]);
bool FindClose(int handle);
#import

void OnStart()
  {
//---
   string EAname="";    // EA name
   int EAcounter = 0;   // EAs counter   
   TYP W[80];  
   string myPath =  TerminalPath() + "\\MQL4\\experts\\*.mq4"; 
   int handle = FindFirstFileW( myPath,W);
   FindClose(handle);

   // use one of the following two lines which does not cause the compiler to moan.
   //EAname = CharArrayToString(W,0,80,CP_UTF8);
   EAname = ShortArrayToString(W,0,80);
   DebugBreak();
   return;
  }
//+------------------------------------------------------------------+
   

As I want to make the experiments easy for those who want I set

#define TYP int

I changed and tried: uchar, char, short and int.

And I don't see any expert-name in the variable EAname.

Where am I mistaken?

FindFirstFile/FindNextFile
FindFirstFile/FindNextFile
  • www.mql5.com
I have been trying to use the FindFirstFile, and FindNextFile, but I cannot seem to get them to work...
 
Carl Schreiber:

Hi,

I am trying to get the list of files in a nonMT4-Folder for which I need Windows' kernel32.dll functions.

Well there is an old thread to start: https://www.mql5.com/en/forum/103578 and https://www.mql5.com/de/articles/1543 for pre b600+

I just tried to change what has to be changed and replaced the buffer-function by the build-in-functions, but I still don't get any name... :(

As I want to make the experiments easy for those who want I set

#define TYP int

I changed and tried: uchar, char, short and int.

And I don't see any expert-name in the variable EAname.

Where am I mistaken?

I have been using WIN32_FIND_DATA structure in place of your TYP:

#define MAX_PATH 260
struct WIN32_FIND_DATA {
  DWORD    dwFileAttributes;
#ifdef IS_64_SYSTEM
  DWORD    padding_1;
#endif 
  FILETIME ftCreationTime;
  FILETIME ftLastAccessTime;
  FILETIME ftLastWriteTime;
  DWORD    nFileSizeHigh;
  DWORD    nFileSizeLow;
  DWORD    dwReserved0;
  DWORD    dwReserved1;
  TCHAR    cFileName[MAX_PATH];
  TCHAR    cAlternateFileName[14];
};

So I would guess the cFileName is not located at index 0 through 79 of your (short or whatever) array, but somewhat further (probably index 22 through 281 of ushort array).

 

Thank you!!

This works:

//+------------------------------------------------------------------+
//|                                        test_WinFindFileFirst.mq4 |
//|                        Copyright 2014, MetaQuotes Software Corp. |
//|                                              http://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2014, MetaQuotes Software Corp."
#property link      "http://www.mql5.com"
#property version   "1.00"
#property strict
//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+

#define MAX_PATH 260

struct FILETIME
{
  uint dwLowDateTime;
  uint dwHighDateTime;
}; 

struct WIN32_FIND_DATA {
  int     dwFileAttributes;  //DWORD    dwFileAttributes;
#ifdef IS_64_SYSTEM
  int    padding_1;  //DWORD    padding_1;
#endif 
  FILETIME ftCreationTime;
  FILETIME ftLastAccessTime;
  FILETIME ftLastWriteTime;
  uint     nFileSizeHigh;  //DWORD    nFileSizeHigh;
  uint     nFileSizeLow;  //DWORD    nFileSizeLow;
  uint     dwReserved0;  //DWORD    dwReserved0;
  uint     dwReserved1;  //DWORD    dwReserved1;
  short    cFileName[MAX_PATH];      //TCHAR    cFileName[MAX_PATH];
  short    cAlternateFileName[14];   //TCHAR    cAlternateFileName[14];
};

#import "kernel32.dll"
int  FindFirstFileW(string path, WIN32_FIND_DATA& answer[]);
bool FindNextFileW(int handle, WIN32_FIND_DATA& answer[]);
bool FindClose(int handle);
#import

void OnStart()
  {
//---
   WIN32_FIND_DATA W[1];  
   string myPath =  TerminalPath() + "\\MQL4\\experts\\*.mq4"; //"Z:\\SlaveOrders\\*.txt";
   int handle = FindFirstFileW( myPath,W);
   FindClose(handle);
   string n1 = ShortArrayToString(W[0].cFileName,0,WHOLE_ARRAY);
   DebugBreak();
   return;
  }
//+------------------------------------------------------------------+

Reason: