Random 非托管DLL中的随机访问冲突
在本机DLL中,我有:Random 非托管DLL中的随机访问冲突,random,pinvoke,marshalling,access-violation,Random,Pinvoke,Marshalling,Access Violation,在本机DLL中,我有: ReturnMode Decoder::Decode(int16 samples[], int num_samples, char** hypothesis){ char *hypstr = NULL; try { // some other... if (s3_decode(&decoder, NULL, &hypstr, NULL) == 0) { *hypothesis
ReturnMode Decoder::Decode(int16 samples[], int num_samples, char** hypothesis){
char *hypstr = NULL;
try
{
// some other...
if (s3_decode(&decoder, NULL, &hypstr, NULL) == 0)
{
*hypothesis = (char*)CoTaskMemAlloc(strlen(hypstr) + 1);
strcpy(*hypothesis, hypstr);
}
if (hypstr != NULL)
{
free(hypstr);
hypstr = NULL;
}
}
catch (...)
{
return FatalError;
}
return Ok;}
编辑:C#中的调用:
并以这种方式导入:
[DllImport("ShenavaDec.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
static public extern ShProxy.ReturnMode Decode(IntPtr pDec, short[] samples, int num_samples, out IntPtr Hypothesis);
频繁解码方法调用,但在本机DLL中的s3_解码API中随机出现AV。
现在我想知道我的错误在哪里?
它是否与封送处理数据有关?否。
谢谢
输出字符串假设
是错误的。必须是输出IntPtr假设
,并手动编组。谢谢David,但结果是一样的。使用Marshal.PtrToStringAnsi(假设);显然还有其他错误,但您没有提供函数必须如何调用的任何细节,也没有说明您是如何调用的。@David Heffernan,更新的调用例程。您没有得到这些信息。仅仅传递正确类型的参数是不够的。例如,如果传递一个数组,则可能需要将其分配到某个长度。或者,在int参数中传递什么值很重要。在这个问题中,您没有提到函数的语义。
[DllImport("ShenavaDec.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
static public extern ShProxy.ReturnMode Decode(IntPtr pDec, short[] samples, int num_samples, out IntPtr Hypothesis);