[英]Is buffer overflow/overrun possible in completely managed asp.net c# web application
[英]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
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.