我正在使用C#代码中的WCT (使用pinvoke),我有以下问题:
当我调用GetThreadWaitChain时,我得到了WAITCHAIN_NODE_INFO数组的结果:
WAITCHAIN_NODE_INFO:https://msdn.microsoft.com/en-us/library/windows/desktop/ms681422(v=vs.85).aspx
WAITCHAIN_NODE_INFO包含两个结构的联合--这意味着只有一个值是有效的,我如何知道从WAITCHAIN_NODE_INFO结构中获取什么信息?
目前,我正在检查ProcessId和ThreadId是否为0,并由此假定另一个联合一半已初始化.
这是我的C# WAITCHAIN_NODE_INFO结构模型:
[StructLayout(LayoutKind.Sequential)]
public struct WAITCHAIN_NODE_INFO
{
public WCT_OBJECT_TYPE ObjectType;
public WCT_OBJECT_STATUS ObjectStatus;
public _WAITCHAIN_NODE_INFO_UNION Union;
}
[StructLayout(LayoutKind.Explicit)]
public struct _WAITCHAIN_NODE_INFO_UNION
{
[FieldOffset(0)]
public _WAITCHAIN_NODE_INFO_LOCK_OBJECT LockObject;
[FieldOffset(0)]
public _WAITCHAIN_NODE_INFO_THREAD_OBJECT ThreadObject;
}
public unsafe struct _WAITCHAIN_NODE_INFO_LOCK_OBJECT
{
/*The name of the object. Object names are only available for certain object, such as mutexes. If the object does not have a name, this member is an empty string.*/
public fixed char ObjectName[WctApiConst.WCT_OBJNAME_LENGTH];
/*This member is reserved for future use.*/
public UInt64 Timeout;
/*This member is reserved for future use.*/
public UInt32 Alertable;
}
public struct _WAITCHAIN_NODE_INFO_THREAD_OBJECT
{
/*The process identifier.*/
public UInt32 ProcessId;
/*The thread identifier. For COM and ALPC, this member can be 0.*/
public UInt32 ThreadId;
/*The wait time.*/
public UInt32 WaitTime;
/*The number of context switches.*/
public UInt32 ContextSwitches;
}有什么更好的方法来实现这一点吗?
提前感谢
发布于 2016-01-12 19:29:10
逻辑是if ObjectType是WctThreadType,那么您应该读取联合的ThreadObject部分。否则,您将阅读工会的LockObject部分。
可以从MS提供的各种示例中收集到这种情况。例如:https://msdn.microsoft.com/en-us/library/windows/desktop/ms681418.aspx
https://stackoverflow.com/questions/34738008
复制相似问题