当我为 GetWindowText 运行下面的代码时,我收到以下错误作为内部异常抛出:
{“试图读取或写入受保护的内存。这通常表明其他内存已损坏。”}
[DllImport("user32.dll", EntryPoint = "GetWindowTextLength", SetLastError = true)]
internal static extern int GetWindowTextLength(IntPtr hwnd);
[DllImport("user32.dll", EntryPoint = "GetWindowText", SetLastError = true)]
internal static extern int GetWindowText(IntPtr hwnd, ref StringBuilder wndTxt, int MaxCount);
try{
int strLength = NativeMethods.GetWindowTextLength(wndHandle);
var wndStr = new StringBuilder(strLength);
GetWindowText(wndHandle, ref wndStr, wndStr.Capacity);
}
catch(Exception e){ LogError(e) }
我有两个问题:
为什么错误没有被 try catch 捕获?
知道如何在程序遇到此类错误时阻止程序崩溃,而不是使用 try/catch
干杯