How to pass data (mostly strings) from MQL4 to C# DLL

 

I would like to ask how to pass some kind of data, like string to my C# dll. This questions has been answered for C++: How to pass argument by reference from MQL4 to C++ DLL

However in my case I want my C# code to get this value from mql4 and then do something with it (so all this while in the C# code).


Here is my attempt:

My C# DLL:

void TestFunction(int x)

{

 // Do something with x after having received it

}


MQL Code:

#import "Test.dll"

void TestFunction(int);

#import



void OnStart()

{

int a = 6;

TestFunction(a);

}

It's probably a similar approach, but is this still the best way to go, and could someone provide a minimum example of doing this in C#?

 

Please use the </> button to insert your code.


 
Eleni Anna Branou:

Please use the </> button to insert your code.


Done, thanks for pointing out.

 

Check if this helps.

Forum on trading, automated trading systems and testing trading strategies

Crash when string is passed to a C++ DLL function

Goldbach, 2009.10.29 04:33

I correct this issue by adding __stdcall in my export funtion.

Thanks a lot!!!!!


 
Alain Verleyen:

Check if this helps.


Thanks for the link, I check out, but this is C++, and I don't know how different it is to do this from C#.


BOOL APIENTRY DllMain(HANDLE hModule,DWORD ul_reason_for_call,LPVOID lpReserved)
{
        //----
        switch(ul_reason_for_call)
        {
        case DLL_PROCESS_ATTACH:
        case DLL_THREAD_ATTACH:
        case DLL_THREAD_DETACH:
        case DLL_PROCESS_DETACH:
                break;
        }
        //----
        return(TRUE);
}
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
MT4_EXPFUNC void __stdcall EALOG_WriteLine(char *str)
{
        MessageBoxA(NULL, str, "", MB_OK);
}


For example I'm not sure how this EALOG_WriteLine function (which is the one being called) would translate for a C# dll.

Reason: