I am trying to create EA using CSharp instead of C++.
I have problem running managed code with FT3.
This is my CSharp (managed) code:
Code: Select all
using System.IO;
namespace ManagedServer
{
public static class ManagedServer
{
public static void DoSth ()
{
var writer = new StreamWriter("R:\\DoSthManaged.txt");
writer.WriteLine("DoSthManaged");
writer.Flush();
writer.Close();
}
public static int Sum (int a, int b)
{
return a + b;
}
}
}
I wrapped managed code with mixed mode C++ code:
Code: Select all
#include "stdafx.h"
#pragma managed
void DoSthManagedWrapper()
{
ManagedServer::ManagedServer::DoSth();
}
int SumManagedWrapper(int a, int b)
{
return ManagedServer::ManagedServer::Sum(a, b);
}
#pragma unmanaged
#define CALL_CONV __stdcall
void CALL_CONV DoSthNativeWrapper()
{
DoSthManagedWrapper();
}
int CALL_CONV SumNativeWrapper(int a, int b)
{
return SumManagedWrapper(a, b);
}
This is .def file for mixed wrapper:
Code: Select all
EXPORTS DoSthNativeWrapper
SumNativeWrapper
I created test program:
Code: Select all
#include "stdafx.h"
#include <iostream>
#include "windows.h"
#include "winbase.h"
#define IMPORT __declspec(dllimport)
#define CALL_CONV __stdcall
typedef IMPORT void (CALL_CONV *DoSthNativeWrapperAddressType)();
typedef IMPORT int (CALL_CONV *SumNativeWrapperAddressType)(int, int);
wchar_t *ConvertCharArrayToLPCWSTR(const char* charArray)
{
wchar_t* wString=new wchar_t[4096];
MultiByteToWideChar(CP_ACP, 0, charArray, -1, wString, 4096);
return wString;
}
int _tmain(int argc, _TCHAR* argv[])
{
std::cout << "Start" << std::endl;
wchar_t* mixedWrapperLibName = ConvertCharArrayToLPCWSTR("MixedWrapper.dll");
HMODULE mixedWrapperLibHandle = LoadLibrary(mixedWrapperLibName);
std::cout << mixedWrapperLibHandle << std::endl;
free(mixedWrapperLibName);
DoSthNativeWrapperAddressType doSthNativeWrapperAddress = (DoSthNativeWrapperAddressType)GetProcAddress(mixedWrapperLibHandle,"DoSthNativeWrapper");
std::cout << doSthNativeWrapperAddress << std::endl;
doSthNativeWrapperAddress();
std::cout << "DoSth done !" << std::endl;
SumNativeWrapperAddressType sumNativeWrapperAddress = (SumNativeWrapperAddressType)GetProcAddress(mixedWrapperLibHandle,"SumNativeWrapper");
std::cout << sumNativeWrapperAddress << std::endl;
std::cout << sumNativeWrapperAddress(3,5) << std::endl;
FreeLibrary(mixedWrapperLibHandle);
std::cout << "End" << std::endl;
system("pause");
return 0;
}
Output shows it works:
Start
56920000
56921050
DoSth done !
56921060
8
End
Finally I created EA for FT3:
Code: Select all
#include "stdafx.h"
#include "StrategyInterfaceUnit.h"
#include "TechnicalFunctions.h"
#define IMPORT __declspec(dllimport)
#define CALL_CONV __stdcall
typedef IMPORT void (CALL_CONV *DoSthNativeWrapperAddressType)();
typedef IMPORT int (CALL_CONV *SumNativeWrapperAddressType)(int, int);
PChar Currency = NULL;
int Timeframe;
wchar_t *ConvertCharArrayToLPCWSTR(const char* charArray)
{
wchar_t* wString=new wchar_t[4096];
MultiByteToWideChar(CP_ACP, 0, charArray, -1, wString, 4096);
return wString;
}
EXPORT void __stdcall InitStrategy()
{
StrategyShortName("FT3Simple");
StrategyDescription("Simple");
RegOption("Currency", ot_Currency, &Currency);
ReplaceStr(Currency , "EURUSD");
RegOption("Timeframe", ot_TimeFrame, &Timeframe);
Timeframe = PERIOD_M15;
Print("Before Test !!!");
wchar_t* mixedWrapperLibName = ConvertCharArrayToLPCWSTR("F:\\works_FX_2017\\MixedWrapper\\Debug\\MixedWrapper.dll");
Print("1");
HMODULE mixedWrapperLibHandle = LoadLibrary(mixedWrapperLibName);
Print("2");
char buff1[50];
sprintf_s(buff1, "lib handle = %x", mixedWrapperLibHandle);
Print(buff1);
Print("3");
DoSthNativeWrapperAddressType doSthNativeWrapperAddress = (DoSthNativeWrapperAddressType)GetProcAddress(mixedWrapperLibHandle,"DoSthNativeWrapper");
Print("4");
char buff2[50];
sprintf_s(buff2, "doSthNativeWrapperAddress = %x", doSthNativeWrapperAddress);
Print(buff2);
Print("5");
SumNativeWrapperAddressType sumNativeWrapperAddress = (SumNativeWrapperAddressType)GetProcAddress(mixedWrapperLibHandle,"SumNativeWrapper");
Print("6");
char buff3[50];
sprintf_s(buff3, "sumNativeWrapperAddress = %x", sumNativeWrapperAddress);
Print(buff3);
Print("7");
doSthNativeWrapperAddress();
Print("8");
char buff4[50];
sprintf_s(buff4, "sum = %d", sumNativeWrapperAddress(3,5));
Print(buff4);
Print("9");
free(mixedWrapperLibName);
Print("10");
FreeLibrary(mixedWrapperLibHandle);
Print("After Test !!!");
Print("InitStrategy Done !!!");
}
Unfortunately EA does not work:
If I make DoSthNativeWrapper() method empty
Code: Select all
void CALL_CONV DoSthNativeWrapper()
{
//DoSthManagedWrapper();
}
results are the same.
I have read that exception code e0434352 may mean many diffrent things.
Any idea?