gpt4 book ai didi

c++ - 在 C++ 中导入 DLL 和使用外部函数

转载 作者:行者123 更新时间:2023-11-30 03:03:50 25 4
gpt4 key购买 nike

这一定是一个基本问题,但我现在已经为这个问题苦苦挣扎了太久。我在 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

主.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;
}

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

感谢阅读!

最佳答案

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

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

关于c++ - 在 C++ 中导入 DLL 和使用外部函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9150886/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com