我有一個控制檯應用程序使用ComImport調用Windows搜索。 雖然這在調試模式下正常工作...控制檯應用程序在發佈模式下崩潰。 可能是什麼問題?COM互操作失敗發佈模式
[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函數時代碼在發佈模式下失敗。
它在我的代碼被用作如下所示
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倍之間)。
崩潰轉儲說以下關於異常信息 The thread tried to read from or write to a virtual address for which it does not have the appropriate access.
注意:我沒有在我的代碼中使用任何線程。
它究竟是如何崩潰,究竟是哪個代碼? – sharptooth
@sharptooth:添加了deatails – Mulki
@Mulki你有沒有什麼運氣調試這個? –