Problem running managed code with FT3

How to create strategies and indicators
Message
Author
tpr
Posts: 4
Joined: Mon Mar 17, 2014 12:00 pm

Problem running managed code with FT3

#1 Postby tpr » Tue Jul 25, 2017 6:20 am

Hi,

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:

FT3Simple_journal.png
FT3Simple_journal.png (20.86 KiB) Viewed 31631 times


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?

User avatar
neHcioHep
Posts: 18
Joined: Sat Nov 21, 2009 5:49 pm
Contact:

Re: Problem running managed code with FT3

#2 Postby neHcioHep » Thu Jul 27, 2017 1:37 am

Hello,

Forex Tester works only with unmanaged code DLLs

tpr
Posts: 4
Joined: Mon Mar 17, 2014 12:00 pm

Re: Problem running managed code with FT3

#3 Postby tpr » Thu Jul 27, 2017 5:39 am

neHcioHep wrote:Hello,

Forex Tester works only with unmanaged code DLLs

Yes, but why?
As far as I know mixed mode dlls (with managed and unmanaged sections) exist for unmanaged / managed code integration.
The idea is:
- unmanaged client calls unmanaged code in mixed dll,
- unmanaged code in mixed dll calls managed code in mixed dll,
- managed code in mixed dll calls any managed code.


Return to “FT API”

Who is online

Users browsing this forum: No registered users and 24 guests