【问题标题】:Importing a DLL and using an extern function in C++在 C++ 中导入 DLL 并使用外部函数
【发布时间】:2012-02-05 16:31:58
【问题描述】:

这一定是一个基本问题,但我现在为这个问题苦苦挣扎太久了。 我在 Google 上到处找,发现了一些类似的问题和解决方案,但没有一个能解决我的具体问题。

我编写了一个非常基本的 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(这是我的库)。

但是现在我希望能够使用 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)" (__imp_?GetOrdersDetailsNoSymbol@@YAHHPAD0QAH1QAN2221PAUMqlStr@@311@Z) referenced in function "void __cdecl TestClient(void)" (?TestClient@@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;
}

如果有人能帮我解决这个问题,我将不胜感激。

感谢阅读!

【问题讨论】:

  • 这些真的是您的代码的一部分还是复制错误。查看#define 语句。 # 和define 之间有间隔。 #define DLLIMPORT __declspec (dllexport) #else /* Not BUILDING_DLL */ #define DLLIMPORT __declspec (dllimport)
  • 感谢您的回复,代码中没有空白,库编译良好。

标签: c++ dll linker


【解决方案1】:

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

编辑:另外,去掉 extern "C" 语句。然后使用 DLLIMPORT #define 来声明你的函数,并且在你的 DLL 的构建中只使用 #define BUILDING_DLL。

【讨论】:

  • 您好,谢谢!我在测试项目的main.cpp中添加了__stdcall,但我仍然有同样的错误......我不确定我是否理解您关于#define的评论的含义,它现在是否正确使用?
  • 无论是您的代码中的错误还是您在此处复制时,在 # 和 define 之间存在但似乎已被更正的空格。
  • 是的,这已经得到纠正......现在我仍然被链接器错误所困扰
  • 在lib项目的main.h中,函数定义中有const关键字。我应该把这些关键字放在测试项目的main.cpp中的函数定义中吗?
  • 嗨,凯莉,最后一个问题。由于删除 extern "C" 语句有效,你能告诉我什么时候应该使用这些语句有用吗?谢谢:)
猜你喜欢
  • 2016-05-23
  • 1970-01-01
  • 2010-11-04
  • 1970-01-01
  • 2013-12-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多