2014-07-22 95 views
-1

在我的C#項目中,我必須包含一個C++庫。無法在C中調用C++方法#

我有我的C兩種方法++庫如下:

APICLIENT_API std::map<std::string, std::string> logInWithParams(const char* url, const char* loginID, const char* password, const char* salt, const char* timeStamp, const char* refererURL, int iterations); 


static APICLIENT_API int logIn(int iterations); 

我能夠與訪問以下實施登錄方法在C#:

[DllImport(@"F:\MySp\MySp\bin\APIClient.dll", EntryPoint = "[email protected]@[email protected]@[email protected]", CallingConvention = CallingConvention.Cdecl)] 

public static extern int logIn(int g); 

但無法調用logInWithParams方法的任何意思。以下是我所嘗試過的。

[DllImport(@"F:\MySp\MySp\bin\APIClient.dll", EntryPoint = "[email protected]@[email protected]@[email protected]@Z", CallingConvention = CallingConvention.Cdecl)] 
     public static extern System.Collections.Generic.Dictionary<string, string> logInWithParams([MarshalAs(UnmanagedType.LPStr)]string a, [MarshalAs(UnmanagedType.LPStr)]string b, [MarshalAs(UnmanagedType.LPStr)]string c, [MarshalAs(UnmanagedType.LPStr)]string d, [MarshalAs(UnmanagedType.LPStr)]string e, [MarshalAs(UnmanagedType.LPStr)]string f, int g); 

C#Implementaion:

System.Collections.Generic.Dictionary<string, string> strMe = new Dictionary<string, string>(); 
        strMe = APICaller.logInWithParams("https://api.net/api/0.1/session/login", "[email protected]", "123123", "", "1403762225", "https://api.mini.net/", 4096); 

錯誤:無法元帥返回值「:泛型類型不能被封。

我還沒有編寫C++代碼,我只需將其包含在我的C#項目中。 我是C++中整合C++庫的新手。請幫助解決此問題。

+0

您如何期望C++知道'Dictionary '是什麼?你可能必須爲此使用'C++/CLI'。 – leppie

+0

使用C++/CLI將'std :: map'映射到您需要的'Generic.Dictionary'數據類型。 C#不理解C++庫對象。或者,你可以將'map'分解爲某種類型的「原始」內存,然後導入該函數。 – Niall

回答

1

封送器不會自動將C++ STL類型轉換爲.NET類型。 您可以創建一個託管C++項目,您可以在該項目中訪問該功能並在那裏手動進行轉換。 See this example on MSDN.