Find File problems.

 

HI,


I would need a Function to find a file in the Libraries folder.

My challenge is the following:

- The only code I found is related to the "Files" folder from MQL5.
- I must know the correct path of the started MT5 instance. Example: if User has installed as Admin or uses /portable switch, he has other folder (like: C:\program files\Metatrader5\MQL5\ .... ) than normal user ( like:  ... app\roaming\3426645675\...... )

So i must ensure, i can find file in the correct folder.


Summary:


The function should be able to find and return a true / false if a file in the MQL5\Libraries Folder (like: MQL5\Libraries\dummy.dll) depending on the upper descripted mandatories.

Thank you for your help in advance.

 

File operations can be performed only in two directories; corresponding paths can be obtained using the request for TERMINAL_DATA_PATH and TERMINAL_COMMONDATA_PATH properties.

ENUM_TERMINAL_INFO_STRING

Identifier

Description

Type

TERMINAL_LANGUAGE

Language of the terminal

string

TERMINAL_COMPANY

Company name

string

TERMINAL_NAME

Terminal name

string

TERMINAL_PATH

Folder from which the terminal is started

string

TERMINAL_DATA_PATH

Folder in which terminal data are stored

string

TERMINAL_COMMONDATA_PATH

Common path for all of the terminals installed on a computer

string


Please see

https://www.mql5.com/en/docs/constants/environment_state/terminalstatus

https://www.mql5.com/en/docs/files

Documentation on MQL5: File Functions
Documentation on MQL5: File Functions
  • www.mql5.com
File Functions - Reference on algorithmic/automated trading language for MetaTrader 5
 

Thank you for your reply.

 

If I understand it right, I can´t use this kind of procedure to find f.e. a DLL in the depending Libraries folder ?

I tried meanwhile different things I found in the web to get access w/o limitations, but completely without success.

 

Would there be a possible way ? Like using kernel32.dll ? My try also failed when searching for a dll in Folder. Just a simple txt file could have been found (or not) Lasterror: 5019 (ERR_FILE_NOT_EXIST) , while searching for a DLL resulted a 5002 (ERR_WRONG_FILENAME) error.

But I am not an experienced programmer with using of wininint.dll / kernel32.dll or something like that.

So if an expert may has an Idea, how I can check on an existing DLL in Libraries Folder (which is to be loaded from Indicator / EA), then .. please don´t hesitate to write a solution.

Thanks in advance.

 

It would depend on the location of the library.

Also

TERMINAL_DLLS_ALLOWED

Permission to use DLL

bool


But limited to

File operations can be performed only in two directories; corresponding paths can be obtained using the request for TERMINAL_DATA_PATH and TERMINAL_COMMONDATA_PATH properties.

For security reasons, work with files is strictly controlled in the MQL5 language. Files with which file operations are conducted using MQL5 means cannot be outside the file sandbox.

There are two directories (with subdirectories) in which working files can be located:

ENUM_TERMINAL_INFO_STRING

Identifier

Description

Type

TERMINAL_LANGUAGE

Language of the terminal

string

TERMINAL_COMPANY

Company name

string

TERMINAL_NAME

Terminal name

string

TERMINAL_PATH

Folder from which the terminal is started

string

TERMINAL_DATA_PATH

Folder in which terminal data are stored

string

TERMINAL_COMMONDATA_PATH

Common path for all of the terminals installed on a computer

string

For a better understanding of paths, stored in properties of TERMINAL_PATH, TERMINAL_DATA_PATH and TERMINAL_COMMONDATA_PATH parameters, it is recommended to execute the script, which will return these values for the current copy of the client terminal, installed on your computer

Example: Script returns information about the client terminal paths

//+------------------------------------------------------------------+
//|                                          Check_TerminalPaths.mq5 |
//|                        Copyright 2009, MetaQuotes Software Corp. |
//|                                              https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "2009, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart()
  {
//---
   Print("TERMINAL_PATH = ",TerminalInfoString(TERMINAL_PATH));
   Print("TERMINAL_DATA_PATH = ",TerminalInfoString(TERMINAL_DATA_PATH));
   Print("TERMINAL_COMMONDATA_PATH = ",TerminalInfoString(TERMINAL_COMMONDATA_PATH));
  }

Maybe you can try to use the import directive/ For example:

#import "wininet.dll"

I'm not sure what exactly you want to do but you can use the search function to find examples of importing system dll's (dummy.dll ???)

Please see article:

https://www.mql5.com/en/articles/73

Using WinInet.dll for Data Exchange between Terminals via the Internet
Using WinInet.dll for Data Exchange between Terminals via the Internet
  • 2010.06.17
  • //www.mql5.com/en/users/sergeev">
  • www.mql5.com
This article describes the principles of working with the Internet via the use of HTTP requests, and data exchange between terminals, using an intermediate server. An MqlNet library class is presented for working with Internet resources in the MQL5 environment. Monitoring prices from different brokers, exchanging messages with other traders without exiting the terminal, searching for information on the Internet – these are just some examples, reviewed in this article.
 

Hi,

 

Importing an DLL is not a Problem, and working with it.

 

Just a simple scenario what can happen:

My EA / Indicator imports the DLL and on the Init there are several functions which are called. Now you can have different scenarios inside:
- User has not DLL Import allowed (how to capture this is no problem)
- User has DLL Import allowed ! Then I need to find out, if even if the Import is allowed, the file exists, or the call function would cause an Error. The Reason behind this is, to ensure, that even if the File doesn´t exists, that my EA / Indicator can work properly with restrictions, like bypassing the functions of the DLL.


I hope you got me now. 

 
marquez:

Hi,

 

Importing an DLL is not a Problem, and working with it.

 

Just a simple scenario what can happen:

My EA / Indicator imports the DLL and on the Init there are several functions which are called. Now you can have different scenarios inside:
- User has not DLL Import allowed (how to capture this is no problem)
- User has DLL Import allowed ! Then I need to find out, if even if the Import is allowed, the file exists, or the call function would cause an Error. The Reason behind this is, to ensure, that even if the File doesn´t exists, that my EA / Indicator can work properly with restrictions, like bypassing the functions of the DLL.


I hope you got me now. 

I am afraid the imports are loaded immediately after the start, rather than with the first function call. So you have no option to handle the missing file.
 
Ovo Cz:
I am afraid the imports are loaded immediately after the start, rather than with the first function call. So you have no option to handle the missing file.

Oh man,


Thank you for your reply. But this answer was not what I expected. And no other Trick with using Windows DLL Functions from Kernel32 or something like that ? I can´t believe, that I am the only one who wants to prevent crashes in coding on this way.

I also tried already to use Globalvars instead of a DLL, but don´t ask me how, I really don´t know what my tester did, and they can´t reproduce it, but they found a way to crash my Code. And this is the main reason, why I am still depending on DLLs and coded functions in DLLs.

 
marquez:

Oh man,


Thank you for your reply. But this answer was not what I expected. And no other Trick with using Windows DLL Functions from Kernel32 or something like that ? I can´t believe, that I am the only one who wants to prevent crashes in coding on this way.

I also tried already to use Globalvars instead of a DLL, but don´t ask me how, I really don´t know what my tester did, and they can´t reproduce it, but they found a way to crash my Code. And this is the main reason, why I am still depending on DLLs and coded functions in DLLs. 

Actually the MT4 up to the build 509 did use the lazy library loading, but it is not the case of the current MT4 & MT5 any longer.

I think you are looking for something paralel to this C++ sample to avoid the faulty import, but invoking a function by its pointer is (probably) out of scope of MQL.

typedef void (WINAPI *PGNSI)(LPSYSTEM_INFO);

// Call GetNativeSystemInfo if supported or GetSystemInfo otherwise.

PGNSI pGNSI;
SYSTEM_INFO si;

ZeroMemory(&si, sizeof(SYSTEM_INFO));

pGNSI = (PGNSI) GetProcAddress(
   GetModuleHandle(TEXT("kernel32.dll")), 
   "GetNativeSystemInfo");
if(NULL != pGNSI)
   pGNSI(&si);
else GetSystemInfo(&si);

 
What you are asking for is a kind of plugin system, which can be attached/detached dynamically. You should redesign your product into several independent expert advisers/scripts, and they should register themselves - for example in the global variables - so that other parts could know which data/functions are available. You may use chart events for intercommunication for example.
 
Another workaround is to have a default stub library (MQL) which does nothing but distributed with the product and ensures there is no link errors. This library can be replaced by actual library bound to DLL. I'm not sure how you would suggest your users to install it - is it ok to ask them to do it manually?
 
Stanislav Korotky:
What you are asking for is a kind of plugin system, which can be attached/detached dynamically. You should redesign your product into several independent expert advisers/scripts, and they should register themselves - for example in the global variables - so that other parts could know which data/functions are available. You may use chart events for intercommunication for example.

The Idea with Globalvars I already had, and tried. But please don´t ask me how. I did some simple Code to test with Globalvars, and the tester got it crashing. They couldn´t reproduce the error, and I by myself also had no way to reproduce the error, otherwise I still would have switched to use globalvars instead of using a DLL.

 I still discussed the Issue with the Globalvars in a German Forum without success.


Can you maybe explain something more about the stub method you told about ?


And yes, they installed manually. I don´t use any kind of Installers. This could cause unwanted issues if the installer doesn´t detects the right Folder. That why I explained how to install and finding the right folder for their needs.
Reason: