Visual Studio 在调用 GetThreadWaitChain WCT 时挂起

Visual Studio hangs on GetThreadWaitChain WCT call

我正在试用 WCT,我想调用 GetThreadWaitChain,我之前有一些关于这个主题的问题,但最近出现了奇怪的行为。当我调用 GetThreadWaitChain 函数时,我的 Visual Studio 挂起并显示提示消息:

vhost32.exe has stopped working

输出windows消息:

The program '[9068] testWCT.vshost.exe' has exited with code -1073740940 (0xc0000374).

每次我进入该功能时,我的 visual studio 都会挂起...

GetThreadWaitChain 文档:

https://msdn.microsoft.com/en-us/library/windows/desktop/ms679364(v=vs.85).aspx

这是我的代码:

internal void CollectWaitInformation(ClrThread thread)
{
    var g_WctHandle = OpenThreadWaitChainSession(0, 0);

    uint threadID = thread.OSThreadId;

    WAITCHAIN_NODE_INFO[] NodeInfoArray = new WAITCHAIN_NODE_INFO[16];


    int isCycle = 0;
    int count = 16;

    // Make a synchronous WCT call to retrieve the wait chain.
    bool result = GetThreadWaitChain(g_WctHandle,
                            IntPtr.Zero,
                            WCTP_GETINFO_ALL_FLAGS,
                            threadID, ref count, NodeInfoArray, out isCycle);

    if (!result)
    {
        //error
    }

    //Finaly ...
    CloseSession(g_WctHandle);
}

[DllImport("Advapi32.dll")]
public static extern IntPtr OpenThreadWaitChainSession(OpenThreadChainFlags Flags, DWORD callback);

[DllImport("Advapi32.dll")]
public static extern bool GetThreadWaitChain(
    IntPtr WctHandle,
    IntPtr Context,
    UInt32 Flags,
    uint ThreadId,
    ref int NodeCount,
    [MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 4)]
    [In, Out]
    WAITCHAIN_NODE_INFO[] NodeInfoArray,
    out int IsCycle
);

[StructLayout(LayoutKind.Sequential)]
public struct WAITCHAIN_NODE_INFO
{
    public WCT_OBJECT_TYPE ObjectType;
    public WCT_OBJECT_STATUS ObjectStatus;

    public struct LockObject
    {
        string ObjectName;
        LARGE_INTEGER Timeout;
        BOOL Alertable;
    }
    public struct ThreadObject
    {
        DWORD ProcessId;
        DWORD ThreadId;
        DWORD WaitTime;
        DWORD ContextSwitches;
    }
}

}

我尝试在同一进程上 运行 C++ 代码,这是我从 MSDN 获取的: https://msdn.microsoft.com/en-us/library/windows/desktop/ms681418(v=vs.85).aspx

我得到的结果是:所有线程都出现 0x57 错误。根据 MSDN,这是 ERROR_INVALID_PARAMETER:

https://msdn.microsoft.com/en-us/library/windows/desktop/ms681382(v=vs.85).aspx

这很奇怪,因为我没有更改 C++ 代码..

同一主题的上一个问题:

我以错误的方式声明了 WAITCHAIN_NODE_INFO 结构(我的联合是错误的)这是原始结构:

https://msdn.microsoft.com/en-us/library/windows/desktop/ms681422(v=vs.85).aspx

我一声明它是正确的,堆损坏就停止了,一切都正常了...