C#でアンマネージDLLのC関数を使用しようとしています。DllImportを呼び出す直前にブレークするときにAccessViolationExceptionが発生するのはなぜですか?
関数のシグネチャは次のとおりです。
const char* CDECL get_lame_version (void);
私は機能をこのようにインポートします。
[DllImport("libmp3lame.dll")]
static extern string get_lame_version();
私はこの関数を呼び出すが、私はちょうど呼び出しの前に壊れた場合は、F5キーを押し、 AccessViolationExceptionがスローされます。
まず実行ブレークはちょうど前にコール:
その後、私はF5キーを押して、例外がある:
実行が後を破った場合代わりに呼び出しがあった場合、例外はスローされません。
私のコードに問題はありますか?そうでない場合、何が起こっているのですか?
/*! Get the LAME version string. */
/*!
\param void
\return a pointer to a string which describes the version of LAME.
*/
const char *
get_lame_version(void)
{ /* primary to write screen reports */
/* Here we can also add informations about compile time configurations */
#if LAME_ALPHA_VERSION
static /*@[email protected] */ const char *const str =
STR(LAME_MAJOR_VERSION) "." STR(LAME_MINOR_VERSION) " "
"(alpha " STR(LAME_PATCH_VERSION) ", " __DATE__ " " __TIME__ ")";
#elif LAME_BETA_VERSION
static /*@[email protected] */ const char *const str =
STR(LAME_MAJOR_VERSION) "." STR(LAME_MINOR_VERSION) " "
"(beta " STR(LAME_PATCH_VERSION) ", " __DATE__ ")";
#elif LAME_RELEASE_VERSION && (LAME_PATCH_VERSION > 0)
static /*@[email protected] */ const char *const str =
STR(LAME_MAJOR_VERSION) "." STR(LAME_MINOR_VERSION) "." STR(LAME_PATCH_VERSION);
#else
static /*@[email protected] */ const char *const str =
STR(LAME_MAJOR_VERSION) "." STR(LAME_MINOR_VERSION);
#endif
return str;
}
[this post](http://stackoverflow.com/questions/162897/marshal-char-in-c-sharp)と[this post](http://stackoverflow.com/questions/370079/pinvoke)を参照してください。 -for-c-function-that-returns-char)関数がchar *を返すときに文字列を返すinteropを具体的に扱う方法に関するアドバイスを提供します。 –