2012-02-05 115 views
1

这必须是一个基本问题,但我现在正在努力解决这个问题太久。 我在Google上到处寻找,发现了一些类似的问题和解决方案,但没有解决我的具体问题。导入DLL并在C++中使用extern函数

我写过一个非常基本的C++ DLL。事实上,它几乎是C风格的,因为DLL只有一个带有函数的main.cpp代码文件,所以它甚至不使用类。

然后,我有两个头文件:

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_ */ 

事实上,用于创建我的DLL,我开始与现有的代码别人写道,所以我的DLL的.cpp文件有一些模糊的语法,我甚至不知道它在做什么。这是一个什么样的的.cpp看起来像一个摘录:

#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 

我不包括一切,是在.cpp文件中,那里有// ...还有别的东西,但它是我的理解基本的东西很好,因此不该不会成为我的问题的根源......我会很高兴根据需要发布更多信息。

我不是专家,像__declspec这样的所有隐含关键字,但是这个DLL可以被成功导入,函数GetOrdersDetailsNoSymbol可以被某些程序使用,即MetaTrader 4(这是主要目标我的lib)。

但是现在我想能够用C++程序测试我的库,所以我创建了一个空的控制台程序,将库项目添加到测试项目的引用中,并通过测试链接.obj和.h文件项目的属性。

我目前得到这个当我编译测试项目:

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)" ([email protected]@[email protected]@[email protected]) referenced in function "void __cdecl TestClient(void)" ([email protected]@YAXXZ) Z:\Codes\TestsCpp\main.obj TestsCpp 

哦,这里是为测试项目的main.cpp:

#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; 
} 

如果有人可以帮助我解决这个问题,我将无限感激。

感谢您的阅读!

+0

这些真的是你的代码或复制错误的一部分。看看#define语句。 #和define之间有差距。 #define DLLIMPORT __declspec(dllexport) #else/* NOT BUILDING_DLL */ #define DLLIMPORT __declspec(dllimport) – octopusgrabbus 2012-02-05 16:56:11

+0

感谢您的回复,在代码中没有差距,并且库编译良好。 – ibiza 2012-02-05 16:58:55

回答

3

您的两个GetOrdersDetailsNoSymbol声明不匹配。在你的头文件中,你用__stdcall声明了它,而在main.cpp中声明了它。你应该只有一个声明。它可以使用#define和#ifdef来适当地应用dllimport或dllexport关键字。

编辑:另外,摆脱外部“C”语句。然后使用DLLIMPORT #define声明你的函数,并在你的DLL的构建中只使用#define BUILDING_DLL。

+0

嗨,谢谢!我将__stdcall添加到main。测试项目的cpp,但我仍然有同样的错误...我不确定是否理解您对#define的评论意义,现在是否正确使用? – ibiza 2012-02-05 17:42:26

+0

无论是在你的代码中的错误或当你在这里复制,有但似乎已经纠正#和定义之间的空间。 – octopusgrabbus 2012-02-05 17:46:33

+0

是的,这已被纠正...现在我仍然坚持链接器错误,虽然 – ibiza 2012-02-05 17:49:53