gpt4 book ai didi

C# 发生缓冲区溢出

转载 作者:太空宇宙 更新时间:2023-11-04 15:39:28 24 4
gpt4 key购买 nike

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);

关于C# 发生缓冲区溢出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25969673/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com