不能在c#中调用c++方法

本文关键字:c++ 方法 调用 不能 | 更新日期: 2023-09-27 18:14:12

在我的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);

我能够访问logIn方法,在c#中实现如下:

 [DllImport(@"F:'MySp'MySp'bin'APIClient.dll", EntryPoint = "?logIn@CAPIClient@API@@SAHH@Z", CallingConvention = CallingConvention.Cdecl)]
public static extern int logIn(int g);

但是无法调用logInWithParams方法。下面是我尝试过的。

 [DllImport(@"F:'MySp'MySp'bin'APIClient.dll", EntryPoint = "?logInWithParams@CAPIClient@API@@SA?AV?$map@V@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", "james@Golmaal.com", "123123", "", "1403762225", "https://api.mini.net/", 4096);

ERROR:Cannot marshal 'return value':泛型类型无法封送。

我没有写c++代码,我只需要把它包含在我的c#项目中。我是在c#中集成c++库的新手。请帮忙解决这个问题。

不能在c#中调用c++方法

封送器不会自动将c++ STL类型转换为。net类型。您可以创建一个托管c++项目,在那里您可以访问该函数并在那里手动进行转换。