这必须是一个基本问题,但我现在正在努力解决这个问题太久。 我在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;
}
如果有人可以帮助我解决这个问题,我将无限感激。
感谢您的阅读!
这些真的是你的代码或复制错误的一部分。看看#define语句。 #和define之间有差距。 #define DLLIMPORT __declspec(dllexport) #else/* NOT BUILDING_DLL */ #define DLLIMPORT __declspec(dllimport) – octopusgrabbus 2012-02-05 16:56:11
感谢您的回复,在代码中没有差距,并且库编译良好。 – ibiza 2012-02-05 16:58:55