Скачать MetaTrader 5

Вопрос к разработчикам - проблемы передачи переменных типа String,ShortString и PChar из dll функций написанных на Делфи

Авторизуйтесь или зарегистрируйтесь, чтобы добавить комментарий
Forex Trader
114268
Forex Trader 2005.12.18 07:18 
Здравствуйте. Написал простенькую dll-ку на делфи:
library Proba;

{ Important note about DLL memory management: ShareMem must be the
  first unit in your library's USES clause AND your project's (select
  Project-View Source) USES clause if your DLL exports any procedures or
  functions that pass strings as parameters or function results. This
  applies to all strings passed to and from your DLL--even those that
  are nested in records and classes. ShareMem is the interface unit to
  the BORLNDMM.DLL shared memory manager, which must be deployed along
  with your DLL. To avoid using BORLNDMM.DLL, pass string information
  using PChar or ShortString parameters. }

uses
  ShareMem,
  SysUtils,
  Classes;

{$R *.res}

function GetMyName(Index: Integer):PChar;//String,ShortString...
StdCall;
begin
   if Index=0 Then begin
   Result:='Мячик'
   end else begin
   Result:='Шарик'
   end;
end;

Exports GetMyName;

begin
end.



Потом создал в MQL4 простенького советника:


//+------------------------------------------------------------------+
//| Proba.mq4 |
//| Copyright © 2005, MetaQuotes Software Corp. |
//| http://www.metaquotes.net |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2005, MetaQuotes Software Corp."
#property link "http://www.metaquotes.net"


#import "Proba.dll"
string GetMyName(int Ind);
#import


//+------------------------------------------------------------------+
//| expert initialization function |
//+------------------------------------------------------------------+
int init()
{
if(!IsDllsAllowed())
{
Print("Вызов из библиотек (DLL) не возможен. Эксперт не может выполняться.");
return(0);
}

string buffer=GetMyName(0);
Print(buffer);
Print("Блок init выполнен!");


return(0);
}
//+------------------------------------------------------------------+
//| expert deinitialization function |
//+------------------------------------------------------------------+
int deinit()
{
//----

//----
return(0);
}
//+------------------------------------------------------------------+
//| expert start function |
//+------------------------------------------------------------------+
int start()
{
//----

//----
return(0);
}
//+------------------------------------------------------------------+


Переменные типа String и ShrotString вообще не возвращаются из функции, вываливается окно с извинениями от MetaQuotes и терминал закрывается. Переменные типа PChar возвращаются:

2005.12.18 07:11:06 Proba EURUSD,H1: initialized
2005.12.18 07:11:06 Proba EURUSD,H1: Блок init выполнен!
2005.12.18 07:11:06 Proba EURUSD,H1: Мячик
2005.12.18 07:11:02 Proba EURUSD,H1: loaded successfully


... но ошибка уже вываливается после закрытия терминала:
[img]
C:\Error.gif
[/img]

There has been a critical error
Time : 2005.12.18 07:12
Program : Client Terminal
Version : 4.00 (build: 186, 17 Nov 2005)
Owner : MetaQuotes Software Corp. (MetaTrader)
OS : Windows 2003 Server 5.2 (Build 3790)
Processors : 1, type 586, level 6
Memory : 1048052/679484 kb
Exception : C0000005
Address : 00000000
Access Type : read
Access Addr : 00000000

Registers : EAX=00000003 CS=77fc23b4 EIP=7FFD9000 EFLGS=00000DCC
: EBX=00253204 SS=0028 ESP=00253204 EBP=77FC24A8
: ECX=00000068 DS=25313c ESI=77FC23BC FS=77f53300
: EDX=000000E8 ES=77f53159 EDI=00253030 GS=ffffffff

Stack Trace : 77FC24A8 77FC24A8 77FC24A8 77FC24A8
: 77FC24A8 77FC24A8 77FC24A8 77FC24A8
: 77FC24A8 77FC24A8 77FC24A8 77FC24A8
: 77FC24A8 77FC24A8 77FC24A8 77FC24A8
Modules :
1 : 00400000 00283000 D:\Program Files\MetaQuotes4Clean\terminal.exe
2 : 01EC0000 0000B000 D:\Program Files\Borland\BDS\3.0\Bin\borlndmm.dll
3 : 10000000 00004000 D:\Program Files\Punto Switcher\correct.dll
4 : 70AD0000 000E6000 D:\WINDOWS\WinSxS\X86_Microsoft.Windows.Common-Controls_6595b64144ccf1df_6.0.100.0_x-ww_8417450B\COMCTL32.dll
5 : 71AE0000 00008000 D:\WINDOWS\System32\wshtcpip.dll
6 : 71B20000 00043000 D:\WINDOWS\system32\mswsock.dll
7 : 71B70000 00033000 D:\WINDOWS\system32\uxtheme.dll
8 : 71BF0000 00008000 D:\WINDOWS\system32\WS2HELP.dll
9 : 71C00000 00018000 D:\WINDOWS\system32\WS2_32.dll
10 : 72D60000 00008000 D:\WINDOWS\system32\msacm32.drv
11 : 72D70000 00009000 D:\WINDOWS\system32\wdmaud.drv
12 : 73D20000 000F3000 D:\WINDOWS\system32\MFC42.DLL
13 : 76280000 00005000 D:\WINDOWS\system32\MSIMG32.dll
14 : 762B0000 00047000 D:\WINDOWS\system32\comdlg32.dll
15 : 76AA0000 0002C000 D:\WINDOWS\system32\WINMM.dll
16 : 770E0000 0007D000 D:\WINDOWS\system32\OLEAUT32.dll
17 : 77160000 00124000 D:\WINDOWS\system32\ole32.dll
18 : 77290000 00049000 D:\WINDOWS\system32\SHLWAPI.dll
19 : 77380000 007DD000 D:\WINDOWS\system32\SHELL32.dll
20 : 77B60000 00007000 D:\WINDOWS\system32\midimap.dll
21 : 77B70000 00014000 D:\WINDOWS\system32\MSACM32.dll
22 : 77BA0000 00054000 D:\WINDOWS\system32\msvcrt.dll
23 : 77C00000 00044000 D:\WINDOWS\system32\GDI32.dll
24 : 77C50000 000A4000 D:\WINDOWS\system32\RPCRT4.dll
25 : 77D00000 0008F000 D:\WINDOWS\system32\USER32.dll
26 : 77DA0000 00090000 D:\WINDOWS\system32\ADVAPI32.dll
27 : 77E40000 000F4000 D:\WINDOWS\system32\kernel32.dll
28 : 77F40000 000BA000 D:\WINDOWS\system32\ntdll.dll

Call stack :
77FC2010:0498 [77FC24A8] NtAddAtom [D:\WINDOWS\system32\ntdll.dll]
77FC2010:0498 [77FC24A8] NtAddAtom [D:\WINDOWS\system32\ntdll.dll]
77FC2010:0498 [77FC24A8] NtAddAtom [D:\WINDOWS\system32\ntdll.dll]
77FC2010:0498 [77FC24A8] NtAddAtom [D:\WINDOWS\system32\ntdll.dll]
77FC2010:0498 [77FC24A8] NtAddAtom [D:\WINDOWS\system32\ntdll.dll]
77FC2010:0498 [77FC24A8] NtAddAtom [D:\WINDOWS\system32\ntdll.dll]
77FC2010:0498 [77FC24A8] NtAddAtom [D:\WINDOWS\system32\ntdll.dll]
77FC2010:0498 [77FC24A8] NtAddAtom [D:\WINDOWS\system32\ntdll.dll]
77FC2010:0498 [77FC24A8] NtAddAtom [D:\WINDOWS\system32\ntdll.dll]
77FC2010:0498 [77FC24A8] NtAddAtom [D:\WINDOWS\system32\ntdll.dll]
77FC2010:0498 [77FC24A8] NtAddAtom [D:\WINDOWS\system32\ntdll.dll]
77FC2010:0498 [77FC24A8] NtAddAtom [D:\WINDOWS\system32\ntdll.dll]
77FC2010:0498 [77FC24A8] NtAddAtom [D:\WINDOWS\system32\ntdll.dll]
77FC2010:0498 [77FC24A8] NtAddAtom [D:\WINDOWS\system32\ntdll.dll]
77FC2010:0498 [77FC24A8] NtAddAtom [D:\WINDOWS\system32\ntdll.dll]
77FC2010:0498 [77FC24A8] NtAddAtom [D:\WINDOWS\system32\ntdll.dll]
77FC2010:0498 [77FC24A8] NtAddAtom [D:\WINDOWS\system32\ntdll.dll]
77FC2010:0498 [77FC24A8] NtAddAtom [D:\WINDOWS\system32\ntdll.dll]
77FC2010:0498 [77FC24A8] NtAddAtom [D:\WINDOWS\system32\ntdll.dll]
77FC2010:0498 [77FC24A8] NtAddAtom [D:\WINDOWS\system32\ntdll.dll]
77FC2010:0498 [77FC24A8] NtAddAtom [D:\WINDOWS\system32\ntdll.dll]
77FC2010:0498 [77FC24A8] NtAddAtom [D:\WINDOWS\system32\ntdll.dll]
77FC2010:0498 [77FC24A8] NtAddAtom [D:\WINDOWS\system32\ntdll.dll]
77FC2010:0498 [77FC24A8] NtAddAtom [D:\WINDOWS\system32\ntdll.dll]
77FC2010:0498 [77FC24A8] NtAddAtom [D:\WINDOWS\system32\ntdll.dll]
77FC2010:0498 [77FC24A8] NtAddAtom [D:\WINDOWS\system32\ntdll.dll]
77FC2010:0498 [77FC24A8] NtAddAtom [D:\WINDOWS\system32\ntdll.dll]
77FC2010:0498 [77FC24A8] NtAddAtom [D:\WINDOWS\system32\ntdll.dll]
77FC2010:0498 [77FC24A8] NtAddAtom [D:\WINDOWS\system32\ntdll.dll]
77FC2010:0498 [77FC24A8] NtAddAtom [D:\WINDOWS\system32\ntdll.dll]
77FC2010:0498 [77FC24A8] NtAddAtom [D:\WINDOWS\system32\ntdll.dll]
77FC2010:0498 [77FC24A8] NtAddAtom [D:\WINDOWS\system32\ntdll.dll]

Forex Trader
114268
Forex Trader 2005.12.18 07:58  
Здравствуйте. Написал простенькую dll-ку на делфи:
library Proba;

{ Important note about DLL memory management: ShareMem must be the
  first unit in your library's USES clause AND your project's (select
  Project-View Source) USES clause if your DLL exports any procedures or
  functions that pass strings as parameters or function results. This
  applies to all strings passed to and from your DLL--even those that
  are nested in records and classes. ShareMem is the interface unit to
  the BORLNDMM.DLL shared memory manager, which must be deployed along
  with your DLL. To avoid using BORLNDMM.DLL, pass string information
  using PChar or ShortString parameters. }

uses
  ShareMem,
  SysUtils,
  Classes;

{$R *.res}

function GetMyName(Index: Integer):PChar;//String,ShortString...
StdCall;
begin
   if Index=0 Then begin
   Result:='Мячик'
   end else begin
   Result:='Шарик'
   end;
end;

Exports GetMyName;

begin
end.



Потом создал в MQL4 простенького советника:


//+------------------------------------------------------------------+
//| Proba.mq4 |
//| Copyright © 2005, MetaQuotes Software Corp. |
//| http://www.metaquotes.net |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2005, MetaQuotes Software Corp."
#property link "http://www.metaquotes.net"


#import "Proba.dll"
string GetMyName(int Ind);
#import


//+------------------------------------------------------------------+
//| expert initialization function |
//+------------------------------------------------------------------+
int init()
{
if(!IsDllsAllowed())
{
Print("Вызов из библиотек (DLL) не возможен. Эксперт не может выполняться.");
return(0);
}

string buffer=GetMyName(0);
Print(buffer);
Print("Блок init выполнен!");


return(0);
}
//+------------------------------------------------------------------+
//| expert deinitialization function |
//+------------------------------------------------------------------+
int deinit()
{
//----

//----
return(0);
}
//+------------------------------------------------------------------+
//| expert start function |
//+------------------------------------------------------------------+
int start()
{
//----

//----
return(0);
}
//+------------------------------------------------------------------+


Переменные типа String и ShrotString вообще не возвращаются из функции, вываливается окно с извинениями от MetaQuotes и терминал закрывается. Переменные типа PChar возвращаются:

2005.12.18 07:11:06 Proba EURUSD,H1: initialized
2005.12.18 07:11:06 Proba EURUSD,H1: Блок init выполнен!
2005.12.18 07:11:06 Proba EURUSD,H1: Мячик
2005.12.18 07:11:02 Proba EURUSD,H1: loaded successfully


... но ошибка уже вываливается после закрытия терминала:
[img]
C:\Error.gif
[/img]

There has been a critical error
Time : 2005.12.18 07:12
Program : Client Terminal
Version : 4.00 (build: 186, 17 Nov 2005)
Owner : MetaQuotes Software Corp. (MetaTrader)
OS : Windows 2003 Server 5.2 (Build 3790)
Processors : 1, type 586, level 6
Memory : 1048052/679484 kb
Exception : C0000005
Address : 00000000
Access Type : read
Access Addr : 00000000

Registers : EAX=00000003 CS=77fc23b4 EIP=7FFD9000 EFLGS=00000DCC
: EBX=00253204 SS=0028 ESP=00253204 EBP=77FC24A8
: ECX=00000068 DS=25313c ESI=77FC23BC FS=77f53300
: EDX=000000E8 ES=77f53159 EDI=00253030 GS=ffffffff

Stack Trace : 77FC24A8 77FC24A8 77FC24A8 77FC24A8
: 77FC24A8 77FC24A8 77FC24A8 77FC24A8
: 77FC24A8 77FC24A8 77FC24A8 77FC24A8
: 77FC24A8 77FC24A8 77FC24A8 77FC24A8
Modules :
1 : 00400000 00283000 D:\Program Files\MetaQuotes4Clean\terminal.exe
2 : 01EC0000 0000B000 D:\Program Files\Borland\BDS\3.0\Bin\borlndmm.dll
3 : 10000000 00004000 D:\Program Files\Punto Switcher\correct.dll
4 : 70AD0000 000E6000 D:\WINDOWS\WinSxS\X86_Microsoft.Windows.Common-Controls_6595b64144ccf1df_6.0.100.0_x-ww_8417450B\COMCTL32.dll
5 : 71AE0000 00008000 D:\WINDOWS\System32\wshtcpip.dll
6 : 71B20000 00043000 D:\WINDOWS\system32\mswsock.dll
7 : 71B70000 00033000 D:\WINDOWS\system32\uxtheme.dll
8 : 71BF0000 00008000 D:\WINDOWS\system32\WS2HELP.dll
9 : 71C00000 00018000 D:\WINDOWS\system32\WS2_32.dll
10 : 72D60000 00008000 D:\WINDOWS\system32\msacm32.drv
11 : 72D70000 00009000 D:\WINDOWS\system32\wdmaud.drv
12 : 73D20000 000F3000 D:\WINDOWS\system32\MFC42.DLL
13 : 76280000 00005000 D:\WINDOWS\system32\MSIMG32.dll
14 : 762B0000 00047000 D:\WINDOWS\system32\comdlg32.dll
15 : 76AA0000 0002C000 D:\WINDOWS\system32\WINMM.dll
16 : 770E0000 0007D000 D:\WINDOWS\system32\OLEAUT32.dll
17 : 77160000 00124000 D:\WINDOWS\system32\ole32.dll
18 : 77290000 00049000 D:\WINDOWS\system32\SHLWAPI.dll
19 : 77380000 007DD000 D:\WINDOWS\system32\SHELL32.dll
20 : 77B60000 00007000 D:\WINDOWS\system32\midimap.dll
21 : 77B70000 00014000 D:\WINDOWS\system32\MSACM32.dll
22 : 77BA0000 00054000 D:\WINDOWS\system32\msvcrt.dll
23 : 77C00000 00044000 D:\WINDOWS\system32\GDI32.dll
24 : 77C50000 000A4000 D:\WINDOWS\system32\RPCRT4.dll
25 : 77D00000 0008F000 D:\WINDOWS\system32\USER32.dll
26 : 77DA0000 00090000 D:\WINDOWS\system32\ADVAPI32.dll
27 : 77E40000 000F4000 D:\WINDOWS\system32\kernel32.dll
28 : 77F40000 000BA000 D:\WINDOWS\system32\ntdll.dll

Call stack :
77FC2010:0498 [77FC24A8] NtAddAtom [D:\WINDOWS\system32\ntdll.dll]
77FC2010:0498 [77FC24A8] NtAddAtom [D:\WINDOWS\system32\ntdll.dll]
77FC2010:0498 [77FC24A8] NtAddAtom [D:\WINDOWS\system32\ntdll.dll]
77FC2010:0498 [77FC24A8] NtAddAtom [D:\WINDOWS\system32\ntdll.dll]
77FC2010:0498 [77FC24A8] NtAddAtom [D:\WINDOWS\system32\ntdll.dll]
77FC2010:0498 [77FC24A8] NtAddAtom [D:\WINDOWS\system32\ntdll.dll]
77FC2010:0498 [77FC24A8] NtAddAtom [D:\WINDOWS\system32\ntdll.dll]
77FC2010:0498 [77FC24A8] NtAddAtom [D:\WINDOWS\system32\ntdll.dll]
77FC2010:0498 [77FC24A8] NtAddAtom [D:\WINDOWS\system32\ntdll.dll]
77FC2010:0498 [77FC24A8] NtAddAtom [D:\WINDOWS\system32\ntdll.dll]
77FC2010:0498 [77FC24A8] NtAddAtom [D:\WINDOWS\system32\ntdll.dll]
77FC2010:0498 [77FC24A8] NtAddAtom [D:\WINDOWS\system32\ntdll.dll]
77FC2010:0498 [77FC24A8] NtAddAtom [D:\WINDOWS\system32\ntdll.dll]
77FC2010:0498 [77FC24A8] NtAddAtom [D:\WINDOWS\system32\ntdll.dll]
77FC2010:0498 [77FC24A8] NtAddAtom [D:\WINDOWS\system32\ntdll.dll]
77FC2010:0498 [77FC24A8] NtAddAtom [D:\WINDOWS\system32\ntdll.dll]
77FC2010:0498 [77FC24A8] NtAddAtom [D:\WINDOWS\system32\ntdll.dll]
77FC2010:0498 [77FC24A8] NtAddAtom [D:\WINDOWS\system32\ntdll.dll]
77FC2010:0498 [77FC24A8] NtAddAtom [D:\WINDOWS\system32\ntdll.dll]
77FC2010:0498 [77FC24A8] NtAddAtom [D:\WINDOWS\system32\ntdll.dll]
77FC2010:0498 [77FC24A8] NtAddAtom [D:\WINDOWS\system32\ntdll.dll]
77FC2010:0498 [77FC24A8] NtAddAtom [D:\WINDOWS\system32\ntdll.dll]
77FC2010:0498 [77FC24A8] NtAddAtom [D:\WINDOWS\system32\ntdll.dll]
77FC2010:0498 [77FC24A8] NtAddAtom [D:\WINDOWS\system32\ntdll.dll]
77FC2010:0498 [77FC24A8] NtAddAtom [D:\WINDOWS\system32\ntdll.dll]
77FC2010:0498 [77FC24A8] NtAddAtom [D:\WINDOWS\system32\ntdll.dll]
77FC2010:0498 [77FC24A8] NtAddAtom [D:\WINDOWS\system32\ntdll.dll]
77FC2010:0498 [77FC24A8] NtAddAtom [D:\WINDOWS\system32\ntdll.dll]
77FC2010:0498 [77FC24A8] NtAddAtom [D:\WINDOWS\system32\ntdll.dll]
77FC2010:0498 [77FC24A8] NtAddAtom [D:\WINDOWS\system32\ntdll.dll]
77FC2010:0498 [77FC24A8] NtAddAtom [D:\WINDOWS\system32\ntdll.dll]
77FC2010:0498 [77FC24A8] NtAddAtom [D:\WINDOWS\system32\ntdll.dll]



Я извиняюсь, вопрос видимо отчасти снят, код библиотеки должен быть:
library Proba;

uses SysUtils;


{$R *.res}

function GetMyName(Index: Integer):PChar;StdCall;
begin
   if Index=0 Then begin
   Result:='Мячик'
   end else begin
   Result:='Шарик'
   end;
end;

Exports GetMyName;

begin
end.



Только так MQL4 можно передать строку. Однако, строку нультерминальную, а как же String и ShortString?

MetaQuotes Software Corp.
Модератор
182455
MetaQuotes Software Corp. 2005.12.18 17:39  
В MQL4 только сишные строки. Других не будет.
Авторизуйтесь или зарегистрируйтесь, чтобы добавить комментарий