質問は簡単です。マネージ C# コードから私のアンマネージ C++ コードへの文字列を読み取りたいですWCHAR* []
。C 関数は次のとおりです。
extern "C" __declspec(dllexport) int __cdecl myfunc(int argc, WCHAR* argv[])
そしてC#でDLLをインポートしました:
[DllImport("mydll.dll", CharSet = CharSet.Auto, SetLastError = true, CallingConvention = CallingConvention.Cdecl)]
public static extern int myfunc(int argc, [MarshalAs(UnmanagedType.LPTStr)] StringBuilder str);
実行しましたが、C++ コードで文字列を読み取ろうとすると、AccessViolationException : Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
これを行う正しい方法とその逆 (つまり、C++ アンマネージ コードから C# マネージ コードに文字列を渡す方法) は何ですか?
感謝します。ありがとう