Search code examples
c#visual-studio-2015pinvokefreeze

Visual Studio hangs on GetThreadWaitChain WCT call


I am trying out WCT and I want to call GetThreadWaitChain, I had some previous questions on this topic, but I've got recently strange behavior. As I am calling GetThreadWaitChain fuction my Visual Studio hangs with prompt message:

vhost32.exe has stopped working

Output windows message:

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

Every time I am getting to that function my visual studio hangs...

GetThreadWaitChain documentation:

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

This is my code:

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

}

I've tried to run C++ code on the same process, which I took from MSDN: https://msdn.microsoft.com/en-us/library/windows/desktop/ms681418(v=vs.85).aspx

And all I've got is: 0x57 error on all threads as a result. Which is ERROR_INVALID_PARAMETER accordingly to MSDN:

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

Which is weird cause I didn't changed the C++ code..

Previous question on the same topic: WCT GetThreadWaitChain call allways return false


Solution

  • I declared the WAITCHAIN_NODE_INFO struct in a wrong way (my union was wrong) this is the original structure:

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

    As soon as I declared it right, the heap corruption stopped and everything worked...