Joseph Lee Joseph Lee - 12 days ago 5x Question

Create MetaTrader4 DLL with VB.Net

I'm trying to create a DLL for MetaTrader4 with VB.Net. Most of the examples are in C++, but I know it can be done in VB.Net. I know it is a bit more tricky than C++.

Can anyone point me in the right direction?
something about defining MT4_EXPFUNC __declspec(dllexport)


I finally figure out how to do it. Although this is not the ideal solution, it works. I'm documenting here for "future-generations".

Basically, I'm using a program called DLLExporter.Exe that converts a managed .Net class method into a direct unmanaged function. It at:

Sample of my VB.Net code:

Imports System.Runtime.InteropServices
Public Class myFXTrader

    Public Shared Function storeFXRatesIntoSQL(
                                              <MarshalAsAttribute(UnmanagedType.LPWStr)> pvsBroker As String _
                                              , <MarshalAsAttribute(UnmanagedType.LPWStr)> pvsSymbol As String _
                                              , <MarshalAsAttribute(UnmanagedType.LPWStr)> pvsTimeStamp As String _
                                              , pviAsk As Double _
                                              , pviBid As Double) As <MarshalAsAttribute(UnmanagedType.LPWStr)> String
        Dim vsErrorCode As String = ""
            '... do some work here ...
        Catch ex As Exception
        End Try

        Return vsErrorCode
    End Function
End Class

After compilation of the above code, I just run DLLExporter myFXTrader.dll and it will do it's magic. The resulting .dll file can be copied to MT4's library folder and can be #import and used. See sample Expert Advisor code below:

#import "FXTrader.Exports.dll"
   string storeFXRatesIntoSQL(string pvsBroker, string pvsSymbol, string pviTimeStamp, double pviAsk, double pviBid);

int start()
   Comment( storeFXRatesIntoSQL( AccountCompany(), Symbol(), TimeLocal(), Ask, Bid) );
   int err;
   err = GetLastError();
   if(err>0) { Alert( err ); }

Hope this will help whoever out-there who's attempting the same thing. I had to do quite a lot of reading/research to figure this out.