C# コードにアンマネージ DLL をインポートしています。.h ファイルには、次のようにメソッドが記述されています。
DLL_API int __stdcall SetConfiguration(IN char* configuration);
DLL_API int __stdcall GetErrorMessage_UTF16(
INOUT int* errorGroup,
INOUT char* errorCode, /*It must be allocated by the caller and its length must be at least 10 bytes, otherwise the function will crash*/
INOUT wchar_t *messageText,
IN int bufferLength);
SetConfiguration メソッドを呼び出すことができました
[DllImport(DllPath, CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]
private static extern int SetConfiguration([In] MarshalAs(UnmanagedType.LPStr)] string configuration);
単純な C# 文字列を指定するだけでうまくいきました。
今私は GetErrorMessage_UTF16 メソッドを成功させようとしています。では、errorCode の長さをどのように割り当て、wchar_t *messageText 変数を宣言するのですか?
私はこのようなことを試しましたが、うまくいかないようです
[DllImport(DllPath, CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]
private static extern int GetErrorMessage_UTF16(
[In, Out] int errorGroup,
[In, Out] [MarshalAs(UnmanagedType.LPStr)] string errorCode,
[In, Out] [MarshalAs(UnmanagedType.LPStr)] StringBuilder messageText,
[In] int bufferLength);
私が試したものは何でも私は得る
Exception thrown at 0x0f5d1235 (Mydll.dll) in Mydll.exe: 0xc0000005: Access violation writing location 0x067d1000
If there is a handler for this exception, the program may be safely continued.