This must be a basic question but I am struggling with this problem for too long now.
I’ve looked everywhere on Google and found some similar problems and solutions but none that solved my specific problem.
I’ve written a very basic C++ DLL. In fact it is almost in C style because the DLL only has a main.cpp code file with a function in it so it’s not even using a class.
Then I have two header files :
MqlUtils.h :
#ifndef MQLUTILS_H
#define MQLUTILS_H
struct MqlStr
{
int len;
char *string;
};
enum TradeOperation
{
OP_BUY = 0,
OP_SELL = 1,
OP_BUYLIMIT = 2,
OP_SELLLIMIT = 3,
OP_BUYSTOP = 4,
OP_SELLSTOP = 5
};
#endif
main.h :
#ifndef _DLL_H_
#define _DLL_H_
#include "MqlUtils.h"
#define MT4_EXPFUNC __declspec(dllexport)
#define export extern "C" __declspec( dllexport )
MT4_EXPFUNC int __stdcall GetOrdersDetailsNoSymbol(const int orderCount, const char * MasterLicense, const char * SlaveLicense, int orderTicket[], int op[],
double orderOpenPrice[], double orderStoploss[],
double orderTakeProfit[], double orderLots[], int orderDateTime[],
MqlStr * ordersymbol, MqlStr * ordercomments, int lotsCopyingMethod[], int returnedOrders[]);
#endif /* _DLL_H_ */
In fact, for creating my DLL, I started with existing code that someone else wrote, so the .cpp file for my DLL has some obscure syntax that I am not even sure what it is doing. Here’s an excerpt of what the .cpp looks like :
#include "main.h"
#define _UNICODE 1
#define UNICODE 1
#define WIN32_LEAN_AND_MEAN
// ...
EXTERN_C IMAGE_DOS_HEADER __ImageBase;
#if BUILDING_DLL
#define DLLIMPORT __declspec (dllexport)
#else /* Not BUILDING_DLL */
#define DLLIMPORT __declspec (dllimport)
#endif /* Not BUILDING_DLL */
// ...
#ifdef __cplusplus
extern "C"
{
#endif
// ... function code
#ifdef __cplusplus
}
#endif
I did not include everything that is in the .cpp file, where there are // ... there is something else but it’s basic stuff that I understand well so it shouldn’t be the source of my problem…I’ll be glad to post more as needed.
I am not an expert with all the obscure keywords like __declspec and such, but the DLL as it is, can be succesfully imported and the function GetOrdersDetailsNoSymbol can be used by some program, namely MetaTrader 4 (which is the main goal of my lib).
But now I would like to be able to test my library with a C++ program, so I created an empty console program, added the library project to the testing project’s references and linked the .obj and .h files through the testing project’s properties.
I am currently getting this when I compile the testing project :
Error 2 error LNK1120: 1 unresolved externals Z:\Codes\Debug\TestsCpp.exe TestsCpp
Error 1 error LNK2019: unresolved external symbol "__declspec(dllimport) int __cdecl GetOrdersDetailsNoSymbol(int,char *,char *,int * const,int * const,double * const,double * const,double * const,double * const,int * const,struct MqlStr *,struct MqlStr *,int * const,int * const)" (__imp_?GetOrdersDetailsNoSymbol@@YAHHPAD0QAH1QAN2221PAUMqlStr@@311@Z) referenced in function "void __cdecl TestClient(void)" (?TestClient@@YAXXZ) Z:\Codes\TestsCpp\main.obj TestsCpp
Oh and here’s the main.cpp for the testing project :
#include "MqlUtils.h"
#include "main.h"
extern __declspec(dllimport) int GetOrdersDetailsNoSymbol(int orderCount, char * MasterLicense, char * SlaveLicense, int orderTicket[], int op[],
double orderOpenPrice[], double orderStoploss[],
double orderTakeProfit[], double orderLots[], int orderDateTime[],
MqlStr* ordersymbol, MqlStr* ordercomments, int lotsCopyingMethod[], int returnedOrders[]);
void TestClient()
{
char* Master = "7C83C4C2";
char* Slave = "3B7C22A";
int returnedOrderCount[1] = {0};
double aStoredOrderOpenPrice[4];
int aStoredOrderType[4];
int aStoredOrderTicket[4];
double aStoredOrderStopLoss[4];
double aStoredOrdeTakeProfit[4];
double aStoredOrderLots[4];
int aStoredOrderDateTime[4];
int aStoredLotsMethods[4];
MqlStr* aStoredOrderComment[4];
MqlStr* aStoredOrderSymbol[4];
for (int i = 0; i < 4; i++)
{
aStoredOrderOpenPrice[i]= -1;
aStoredOrderType[i]= -1;
aStoredOrderTicket[i]= -1;
aStoredOrderStopLoss[i]= -1;
aStoredOrdeTakeProfit[i]= -1;
aStoredOrderLots[i]= -1;
aStoredOrderDateTime[i]= -1;
aStoredLotsMethods[i]= -1;
aStoredOrderComment[i]->len = 56;
aStoredOrderComment[i]->string = "11111111111111111111111111111111111111111111111111111111";
aStoredOrderSymbol[i]->len = 56;
aStoredOrderSymbol[i]->string = "11111111111111111111111111111111111111111111111111111111";
}
GetOrdersDetailsNoSymbol(1, Master, Slave, aStoredOrderTicket, aStoredOrderType,
aStoredOrderOpenPrice, aStoredOrderStopLoss,
aStoredOrdeTakeProfit, aStoredOrderLots, aStoredOrderDateTime,
*aStoredOrderSymbol, *aStoredOrderComment, aStoredLotsMethods, returnedOrderCount);
}
int main(int argc, char **argv)
{
TestClient();
return 0;
}
If anyone could help me solve this, I would be infinitely grateful.
Thanks for reading!
Your two declarations of GetOrdersDetailsNoSymbol do not match. In your header file you declare it with __stdcall and in main.cpp you don’t. You should have only a single declaration. It can use #define and #ifdef to apply the dllimport or dllexport keywords as appropriate.
EDIT: Also, get rid of the extern “C” statements. And then use the DLLIMPORT #define to declare your function and only #define BUILDING_DLL in the build of your DLL.