0

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)

Victor Zakharov
  • 25,801
  • 18
  • 85
  • 151
jlee88my
  • 2,935
  • 21
  • 28

3 Answers3

3

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: http://www.codeproject.com/Articles/37675/Simple-Method-of-DLL-Export-without-C-CLI

Sample of my VB.Net code:

Imports System.Runtime.InteropServices
Public Class myFXTrader

    <DllExporter.DllExport()>
    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 = ""
        '---------------------------------
        Try
            '... 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);
#import

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

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.

jlee88my
  • 2,935
  • 21
  • 28
1

You will not find any official examples from metatrader. Their support is always less then helpful.

The api as you noted is indeed c++, meaning native. You are looking to write something in vb.net. .Net being managed code, whether your looking for vb or c# or even f# you will need a translation layer that will convert native api and data structures to manged.

Look into c++/cli. Cli is a framework that allows you mix native and managed code in the same assembly.

__declspec(dllexport) is an instruction that tells the linker which functions to export out of a native dll so that they are visible to the consumer of the dll and can be invoked in code. When you export a function like that it is still a native function.

I'm not sure if your trying to implement manager or server api. In case of a manager the entire api is in a class, the pointer to which is provided by the only function exported by the manager dll. In case of the server your are the one writing the dll as a plugin to the server so, in theory, you could export every function of your plugin but then the input and output types will still be native, in which case you will ether have to write converters or use pinvoke on the functions you exported. Here's an explanation of pinvoke http://www.codeproject.com/Articles/4965/Using-Platform-Invoke

In any case you still need to write native c++ code.

Dmitry
  • 1,513
  • 1
  • 15
  • 33
  • Thanks for the info. It is helpful. But I already found an answer how to do it, not the ideal, but works. See my answer. cheersd. – jlee88my Mar 04 '13 at 02:10
0

There is open source project, which gives you almost full access to mtmanapi: MetaTrader4.Manager.Wrapper.

It's written in C++/CLI, so it won't be a problem to use it from VB.NET

Uriil
  • 11,948
  • 11
  • 47
  • 68