C# 发生了缓冲区溢出
C# A buffer overrun has occurred
A buffer overrun has occurred in MyApp.exe which has corrupted the program's internal state.
我已经尝试了一些方法,但无法弄清楚是什么导致了 1/50 次通话中的上述情况,可能是显而易见的,有很多猜测。
[DllImport("user32.dll")]
public static extern IntPtr GetTopWindow(IntPtr hWnd);
[DllImport("user32.dll")]
public static extern uint GetWindowThreadProcessId(IntPtr hWnd, out UInt32 pid);
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
public static extern int GetWindowText(IntPtr hWnd, StringBuilder lpString, int nMaxCount);
[DllImport("user32.dll", SetLastError = true)]
public static extern IntPtr GetWindow(IntPtr hWnd, uint uCmd);
public static IntPtr GetProcessWindow(int processId)
{
UInt32 pid = 0;
UInt32 dwThreadId = 0;
StringBuilder a = new StringBuilder();
IntPtr hwnd = GetTopWindow(IntPtr.Zero);
while(hwnd != null)
{
dwThreadId = GetWindowThreadProcessId(hwnd, out pid);
GetWindowText(hwnd, a, 256);
String name = a.ToString();
if(pid == processId && name.Contains("[Window Name]"))
return hwnd;
hwnd = GetWindow(hwnd, 2);
}
return IntPtr.Zero;
}
提前谢谢。
您没有在StringBuilder
中预先分配缓冲区。
这一行:
StringBuilder a = new StringBuilder();
应该是:
StringBuilder a = new StringBuilder(256);
尝试使用GetWindowTextLength
API 调用定义的容量初始化StringBuilder
。
相关文章:
- 使用动态分配的数组会导致代码分析发出虚假的C6386缓冲区溢出警告
- 为什么我在leetcode上收到AddressSanitizer:地址0x602000000058上的堆缓冲区溢出错误
- 在 leetcode 上提交解决方案时出现堆栈缓冲区溢出错误
- 使用 strcat 获取缓冲区溢出错误
- LeetCode 1:两和 - 地址清理器:堆缓冲区溢出地址
- 使用向量的缓冲区溢出
- 重新增长阵列时出错:写入时缓冲区溢出
- Leetcode 1366:堆缓冲区溢出
- 缓冲区溢出 - 数组索引越界(严重)
- C++二维矢量导致缓冲区溢出
- 缓冲区溢出怎么会成为黑客的攻击
- 如何防止缓冲区溢出
- 应该如何读取堆缓冲区溢出错误消息?
- 自动截断和 null 终止缓冲区溢出中的字符串缓冲区
- 如果用户输入两个或多个由空格分隔的字符串C++如何防止缓冲区溢出?
- MSVC C6029 警告:缓冲区可能溢出,使用未经检查的值.检查缓冲区大小时,警告不会消失
- 我正在尝试使用回溯来解决 N queen 问题,但在编译时它会给出运行时错误(动态堆栈缓冲区溢出)
- 我不能溢出缓冲区
- 包含溢出缓冲区的堆栈分配变量,也称为缓冲区
- STL矢量溢出缓冲区