exist example for dll very easy? like hello world?

 

Hi exist example or tutorial for create and interact with .Dll , but extremely easy ?  like simple Hello world?


thanks

 
faustf:

Hi exist example or tutorial for create and interact with .Dll , but extremely easy ?  like simple Hello world?

1. Write some C++ code, like this:

#include <windows.h>

extern "C" __declspec(dllexport) int ShowMessage(char *message)
{
    int len = MultiByteToWideChar(CP_ACP, 0, message, -1, NULL, 0);
    wchar_t *wstr = new wchar_t[len];
    MultiByteToWideChar(CP_ACP, 0, message, -1, wstr, len);
    int result = MessageBoxW(NULL, wstr, L"DLL Message", MB_OKCANCEL);
    delete[] wstr;
    return result;
}

extern "C" __declspec(dllexport) int Add(int a, int b)
{
    return a + b;
}

2. Compile, I used cl.exe from Visual Studio for this (only bc I had it installed, a lot of tutorials are for gcc compiler):

cl.exe /LD sampledll.cpp /link user32.lib

3. Copy to MQL4\libraries & enjoy your newborn baby ;)

#property strict

#import "sampledll.dll"
int Add(int a, int b);
int ShowMessage(char &message[]);
#import

void OnStart()
  {
   printf("1 + 2 = %i",Add(1,2));
   char arr[];
   StringToCharArray("Mother Tell your children not to walk my way",arr);
   int res=ShowMessage(arr);
   Print(res==IDOK?"OK":"CANCEL");
  }
 

sorry i use a  visualstudio 2019 i have created  a dll library and  return me this code

// dllmain.cpp : Defines the entry point for the DLL application.
#include "pch.h"

BOOL APIENTRY DllMain( HMODULE 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;
}

how can modify this ? ?

thanks

 

Check the mql4/scripts/examples/DLL folder in you MetaEditor.

There is a complete example with c++ and mql code. 

c++ is used to create a dll (note export statements)

mql code demonstrates how to use that dll.

 
Drazen Penic #:

Check the mql4/scripts/examples/DLL folder in you MetaEditor.

There is a complete example with c++ and mql code. 

c++ is used to create a dll (note export statements)

mql code demonstrates how to use that dll.

yea i know  but  is much complicated like example therfore i ask if exist some simple example like hello world
 
faustf #:
yea i know  but  is much complicated like example therfore i ask if exist some simple example like hello world
I gave you the c++ example that works where I define functions as extern. Take my code and tailor for your needs. Can't get easier than that ;)
 

sorry but i am not so much expert in c++ and VS, i did do in this mode , choice dll library , VS open new file i  paste  a code

// dllmain.cpp : Defines the entry point for the DLL application.
#include "pch.h"
#include <windows.h>

extern "C" __declspec(dllexport) int ShowMessage(char* message)
{
    int len = MultiByteToWideChar(CP_ACP, 0, message, -1, NULL, 0);
    wchar_t* wstr = new wchar_t[len];
    MultiByteToWideChar(CP_ACP, 0, message, -1, wstr, len);
    int result = MessageBoxW(NULL, wstr, L"DLL Message", MB_OKCANCEL);
    delete[] wstr;
    return result;
}

extern "C" __declspec(dllexport) int Add(int a, int b)
{
    return a + b;
}
BOOL APIENTRY DllMain( HMODULE 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;
}

when compile return this (look attach) , i suppose is  correct , but i dont know ,  i create a mql4  file and  run it

//+------------------------------------------------------------------+
//|                                                da_cancellare.mq4 |
//|                                  Copyright 2023, MetaQuotes Ltd. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2023, MetaQuotes Ltd."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict
#property indicator_chart_window
 
#import "Dlltest.dll"
int Add(int a, int b);
int ShowMessage(char &message[]);
#import

void OnStart()
  {
   printf("1 + 2 = %i",Add(1,2));
   char arr[];
   StringToCharArray("Mother Tell your children not to walk my way",arr);
   int res=ShowMessage(arr);
   Print(res==IDOK?"OK":"CANCEL");
  }

but not return nothing, i look only load succesfully and  remove succes.

Files:
compile.jpg  20 kb
 

Here is the easy example ;)

#include <cwchar>
extern "C" __declspec(dllexport) const wchar_t* HowAreYou(void)
{
    return L"I am fine, thank you.";
}
#property strict

#import "easydll.dll"
const string HowAreYou(void);
#import

void OnStart()
  {
   Print(HowAreYou());
  }
 
Marcin Madrzak #:

Here is the easy example ;)

thank you for easy example  but the problem suppose, is located when i create a dll in VS  because also in this mode not return nothing  like  before, exist a  tutorial step by step visualstudio 19 create dll  ? thanks

when i run
cl.exe

tell me not  internal command  where  i find  this tool ?

 
faustf #:

thank you for easy example  but the problem suppose, is located when i create a dll in VS  because also in this mode not return nothing  like  before, exist a  tutorial step by step visualstudio 19 create dll  ? thanks

when i run

tell me not  internal command  where  i find  this tool ?

Install Visual C++  (or the entire Visual Studio ),version >= 6.0
 

i have just install VS2019 , but cl.exe is not in enviroment path  iadd in enviroment path i add  cl.exe  (64bit)  i  opena  file text i past  this code

#include <windows.h>

extern "C" __declspec(dllexport) int ShowMessage(char *message)
{
    int len = MultiByteToWideChar(CP_ACP, 0, message, -1, NULL, 0);
    wchar_t *wstr = new wchar_t[len];
    MultiByteToWideChar(CP_ACP, 0, message, -1, wstr, len);
    int result = MessageBoxW(NULL, wstr, L"DLL Message", MB_OKCANCEL);
    delete[] wstr;
    return result;
}

extern "C" __declspec(dllexport) int Add(int a, int b)
{
    return a + b;
}

i call by cmd  the cl.exe   like  this 

cl.exe /LD sampledll.cpp /link user32.lib

but  return 

Microsoft Windows [Version 10.0.19045.3448]
(c) Microsoft Corporation. All rights reserved.

C:\Users\pct>cl
Microsoft (R) C/C++ Optimizing Compiler Version 19.29.30151 for x64
Copyright (C) Microsoft Corporation.  All rights reserved.

usage: cl [ option... ] filename... [ /link linkoption... ]

C:\Users\pct>cd C:\Users\pct\source\repos\Dlltest\Debug

C:\Users\pct\source\repos\Dlltest\Debug>cd C:\Users\pct\Desktop\test-dll-mql4

C:\Users\pct\Desktop\test-dll-mql4>cl.exe /LD sampledll.cpp /link user32.lib
Microsoft (R) C/C++ Optimizing Compiler Version 19.29.30151 for x64
Copyright (C) Microsoft Corporation.  All rights reserved.

sampledll.cpp
sampledll.cpp(1): fatal error C1034: windows.h: no include path set

C:\Users\pct\Desktop\test-dll-mql4>