输入函数 (#import)

函数从MQL5编译模板(*.ex5 文件)和执行系统文件模板(文件 *.dll)通过。模板名称被指定在#import指令中。能够正确形成输入函数的编译器调用和组织适当的参数传送,需要带有完整的函数描述部分。函数描述会立即按照#import “模板名称”执行。新的#import命令完成引入输入函数描述部分。

#import "file_name"
    func1 define;
    func2 define;
    ...
    funcN define;
#import

输入函数可以有几个名称。相同名称不同的模块的函数可以同时输入。输入函数名与嵌入函数名一致。范围解析操作决定需要调用哪个函数。

搜索 #import 关键字后指定文件的命令描述在调用导入函数

因为引入函数是在模块外面被编写,编译器无法检查通过参量的正确性。因此,为避免运行错误, 它必须精确地描述传送到输入函数的参数的组成和命令。传到输入函数的参数(从EX5,和从DLL-模块)可以有默认值。

以下在输入函数中不能用作参数:

类,字符串数组或者包括字符串或者动态数组的复合对象不能作为参数传送到DLL输入函数。

示例:

#import "stdlib.ex5"
string ErrorDescription(int error_code);
int    RGB(int red_value,int green_value,int blue_value);
bool   CompareDoubles(double number1,double number2);
string DoubleToStrMorePrecision(double number,int precision);
string IntegerToHexString(int integer_number);
#import "ExpertSample.dll"
int    GetIntValue(int);
double GetDoubleValue(double);
string GetStringValue(string);
double GetArrayItemValue(double &arr[],int,int);
bool   SetArrayItemValue(double &arr[],int,int,double);
double GetRatesItemValue(double &rates[][6],int,int,int);
#import

在mql5 程序执行期间输入函数,需要使用前期绑定。 这就意味着ex5程序加载时启动资料库。

不建议使用Drive:\Directory\FileName.Ext加载模块的权限定名。MQL5资料库会从 terminal_dir\MQL5\Libraries文件夹中加载。

如果导入函数的调用版本不同于32位和64位Windows版本,那么这两种版本都应被导入,并且正确的函数版本应使用_IsX64变量显式调用。

例如:

#import "user32.dll"
//--- 对32位系统
int    MessageBoxW(uint hWnd,string lpText,string lpCaption,uint uType);
//--- 对64位系统
int    MessageBoxW(ulong hWnd,string lpText,string lpCaption,uint uType);
#import
//+------------------------------------------------------------------+
//|  MessageBox_32_64_bit 使用适当的MessageBoxW()版本                  |
//+------------------------------------------------------------------+
int MessageBox_32_64_bit()
  {
   int res=-1;
   //--- 如果我们使用64位Windows
   if(_IsX64)
     {
      ulong hwnd=0;
      res=MessageBoxW(hwnd,"64-bit MessageBoxW call example","MessageBoxW 64 bit",MB_OK|MB_ICONINFORMATION);
     }
   else  // 我们使用32位Windows
     {
      uint hwnd=0;
      res=MessageBoxW(hwnd,"32-bit MessageBoxW call example","MessageBoxW 32 bit",MB_OK|MB_ICONINFORMATION);
     }
   return (res);
  }
//+------------------------------------------------------------------+
//| 脚本程序起始函数                                                   |
//+------------------------------------------------------------------+
void OnStart()
  {
//---
   int ans=MessageBox_32_64_bit();
   PrintFormat("MessageBox_32_64_bit returned %d",ans);
  }

从 .NET库导入函数

若要使用.NET程序库函数,只需导入DLL本身,而不必定义特定的函数。MetaEditor自动导入所有可以使用的函数:

  • 简单结构(POD,普通旧数据) ― 仅包含简单数据类型的结构。
  • 有参数的公共静态函数,在这里只有简单类型和POD结构或其数组可被使用。

若要从程序库调用函数,简单导入即可:

#import "TestLib.dll"
//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart()
  {
   int x=41;
   TestClass::Inc(x);
   Print(x);
  }

TestClass的Inc函数C#代码如下:

public class TestClass
  {
   public static void Inc(ref int x)
     {
      x++;
     }
  }

作为执行结果,脚本返回值为42。

另见

包括文件