這必須是一個基本問題,但我現在正在努力解決這個問題太久。 我在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