我有一个控制台应用程序,它使用 ComImport 调用 Windows 搜索。 虽然这在 Debug模式下工作正常......控制台应用程序在 Release模式下崩溃。 可能是什么问题?
[ComImport]
[Guid("9DAA54E8-CD95-4107-8E7F-BA3F24732D95")]
[ClassInterface(ClassInterfaceType.None)]
[TypeLibType(TypeLibTypeFlags.FCanCreate)]
public class WordBreaker : IWordBreaker
{
[MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)]
public virtual extern bool Init([In] bool query, [In] uint maxTokenSize);
[MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)]
public virtual extern void BreakText([In, MarshalAs(UnmanagedType.LPStruct)] TEXT_SOURCE textSource,
[In] IWordSink wordSink, [In] IPhraseSink phraseSink);
[MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)]
public virtual extern void ComposePhrase([In, MarshalAs(UnmanagedType.LPWStr)] string noun, [In] uint nounLen,
[In, MarshalAs(UnmanagedType.LPWStr)] string modifier, [In] uint modifierLen,
[In] uint attachmentType, [Out] out IntPtr phrase, [In, Out] ref uint phraseLen);
[MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)]
public virtual extern IntPtr GetLicenseToUse();
}
代码在访问 WordBreaker.BreakText 函数时在 Release模式下失败。
它正在我的代码中使用,如下所示
if (!string.IsNullOrWhiteSpace(text))
try
{
IWordBreaker breaker = new WordBreaker();
bool reqLicense = breaker.Init(query, 256);
if (reqLicense)
{
IntPtr lic = breaker.GetLicenseToUse();
string licText = Marshal.PtrToStringUni(lic);
}
TEXT_SOURCE source = new TEXT_SOURCE();
source.fillTextBuffer = FillTextBuffer;
source.buffer = text;
source.cur = 0;
source.end = (uint)(text.Length);
breaker.BreakText(source, new WordSink(result), null);
}
catch (Exception ex)
{
Console.Out.WriteLine(ex.ToString());
//log4net.LogManager.GetLogger(typeof(WindowsIntegration)).Error("BreakText", ex);
}
var resultWithoutNoise = NoiseWord.Remove(result);
return resultWithoutNoise;
}
崩溃恰好发生在breaker.BreakText
BreakText
函数在应用崩溃前被调用多次(500 到 7000 次)。
故障转储说明了以下有关异常信息的信息
线程试图读取或写入它没有适当访问权限的虚拟地址。
注意:我没有在我的代码中使用任何线程。
最佳答案
代码至少需要一个线程,这就是错误所指的。 至于崩溃的类型,它本质上是一种访问冲突,要么是空指针/对象引用(在 native 代码中),要么是缓冲区溢出。 检查什么值。字符串。您在失败时传入的长度等。
关于c# - COM 互操作在 Release模式下失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6519660/