2012-02-05 122 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