Download MetaTrader 5
To add comments, please log in or register
Share your knowledge. Become a well-known developer!
Lime 2014.02.16 13:02 

Hi Coders,

I'm working on a DLL function written in C++. The function will read the content of a file on the Internet. Then the content will return to the .EX4 (Build 600+).

Please, take a look at my C++ code:

#define WIN32_LEAN_AND_MEAN  // Exclude rarely-used stuff from Windows headers
#include "stdafx.h"
#include <windows.h>
#include <stdlib.h>
#include <stdio.h>
#include <ctime>
#include <cstdlib>
#include <curl.h>
#include <string>
#include <iostream>

using namespace std;
std::string page_markup;

#define MT4_EXPFUNC __declspec(dllexport)

// =============================================== \\
// char to wchar_t
wchar_t *WChar(const char *orig){
        size_t newsize = strlen(orig) + 1;
        wchar_t * wcstring = new wchar_t[newsize];
        size_t convertedChars = 0;
        mbstowcs_s(&convertedChars, wcstring, newsize, orig, _TRUNCATE);
        return wcstring;

extern "C" size_t append_chunk(char *ptr, size_t size, size_t nmemb, void *userdata) {
        std::string *target = (std::string*)userdata;
        target->append(ptr, size*nmemb);
        return size*nmemb;

int main(void){
        CURL *curl;
        CURLcode res;
        curl = curl_easy_init();
        if (curl) {
                curl_easy_setopt(curl, CURLOPT_URL, "");
                // is redirected, so we tell libcurl to follow redirection
                curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
                curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, append_chunk);
                curl_easy_setopt(curl, CURLOPT_WRITEDATA, &page_markup);
                // Perform the request, res will get the return code
                res = curl_easy_perform(curl);
                // Check for errors
                if (res != CURLE_OK)
                        fprintf(stderr, "curl_easy_perform() failed: %s\n",
                // always cleanup
        return 0;

// =============================================== \\
// ReadContent
MT4_EXPFUNC wchar_t* __stdcall ReadContent(const double x){
        const char* cc_contents = page_markup.c_str();
        return WChar(cc_contents);

First look, this code seems work in the Visual Studio 2013. There is no any error or warning message at the compiling.

But this solution does not work in the Expert Advisor. The EA can not load the DLL file. It sends the following error messages:

2014.02.16 12:51:04.894    unresolved import function call
2014.02.16 12:51:04.891    Cannot call 'C:\Users\Lime\Documents\Visual Studio 2013\Projects\Cubic6\Debug\Cubic6.dll::ReadContent', 'C:\Users\Lime\Documents\Visual Studio 2013\Projects\Cubic6\Debug\Cubic6.dll' is not loaded
2014.02.16 12:51:04.891    Cannot load 'C:\Users\Lime\Documents\Visual Studio 2013\Projects\Cubic6\Debug\Cubic6.dll'

I know, the problem is with the cURL method. But I don't know, how I fix it. Anyway is it possible to use the cURL to our DLL function to .EX4 programs? Or could you offer another solution to read Internet file content?

Thank you in advance.


franzk 2014.08.11 21:59  


when you are using libcurl you should also put the following dll files into the MQL4\Libraries folder: curllib.dll, libeay32.dll, openldap.dll, ssleay32.dll (you find them in the root directory of libcurl) and libsasl.dll (google for it).

Hope that helps. 

To add comments, please log in or register