プラットフォームの呼び出しを使用して、C#からC++に文字列を渡そうとしています。
C ++コード:
#include<string> using namespace std; extern "C" { double __declspec(dllexport) Add(double a, double b) { return a + b; } string __declspec(dllexport) ToUpper(string s) { string tmp = s; for(string::iterator it = tmp.begin();it != tmp.end();it++) (*it)-=32; return tmp; } }
C#コード:
[DllImport("TestDll.dll", CharSet = CharSet.Ansi, CallingConvention =CallingConvention.Cdecl)] public static extern string ToUpper(string s); static void Main(string[] args) { string s = "hello"; Console.WriteLine(Add(a,b)); Console.WriteLine(ToUpper(s)); }
SEHExceptionを受け取ります。このように使うことは不可能std::string
ですか?char*
代わりに使用する必要がありますか?