Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/155.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#发生了缓冲区溢出_C#_C++_Dllimport - Fatal编程技术网

C#发生了缓冲区溢出

C#发生了缓冲区溢出,c#,c++,dllimport,C#,C++,Dllimport,我尝试了一些方法,但无法找出是什么导致了1/50的通话出现上述情况,可能是一些明显的原因,这有很多值得猜测的地方 A buffer overrun has occurred in MyApp.exe which has corrupted the program's internal state. 提前感谢。您没有在StringBuilder中预先分配缓冲区 这一行: [DllImport("user32.dll")] public static extern IntPtr GetTopWin

我尝试了一些方法,但无法找出是什么导致了1/50的通话出现上述情况,可能是一些明显的原因,这有很多值得猜测的地方

A buffer overrun has occurred in MyApp.exe which has corrupted the program's internal state.

提前感谢。

您没有在
StringBuilder
中预先分配缓冲区

这一行:

[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 a = new StringBuilder();

您没有在
StringBuilder
中预先分配缓冲区

这一行:

[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 a = new StringBuilder();

尝试使用
GetWindowTextLength
API调用定义的容量初始化
StringBuilder

尝试使用
GetWindowTextLength
API调用定义的容量初始化
StringBuilder

错误发生在哪里?哪条线?堆栈跟踪是什么?对任何阅读本文的人来说:当你通过谷歌搜索错误消息发现它时,这不太可能是你的问题。该错误导致堆损坏,但错误消息几乎总是堆栈损坏问题。错误发生在哪里?哪条线?堆栈跟踪是什么?对任何阅读本文的人来说:当你通过谷歌搜索错误消息发现它时,这不太可能是你的问题。该错误导致堆损坏,但错误消息几乎总是堆栈损坏问题。+1中有明确的解释:唯一的警告是必须为
StringBuilder
分配足够的空间用于返回值,否则文本将溢出,导致P/Invoke引发异常。这意味着OP的代码只有在文本小于默认的
StringBuilder
容量时才能工作(这是特定于实现的,但目前在.NET 4.5 IIRC中为16)。+1中有明确的说明:唯一的警告是
StringBuilder
必须为返回值分配足够的空间,或者文本将溢出,导致P/Invoke引发异常。这意味着OP的代码只有在文本小于默认的
StringBuilder
capacity(这是特定于实现的,但目前为16英寸.NET 4.5 IIRC)时才能工作。