使用指针参数(WCT)从C#调用C++方法
Calling C++ method from C# with pointer parameter (WCT)
我对从C#调用C++方法的概念很陌生。
假设我想从C#调用C++函数GetThreadWaitChain
:
https://msdn.microsoft.com/en-us/library/windows/desktop/ms679364(v=vs.85).aspxhttps://msdn.microsoft.com/en-us/library/windows/desktop/ms681623(v=vs.85).aspx
我建立了一个呼叫所依赖的其他类型的模型:
[DllImport("Advapi32.dll")]
public static extern void CloseThreadWaitChainSession(IntPtr WctHandle);
[DllImport("Advapi32.dll")]
public static extern HANDLE OpenThreadWaitChainSession(UInt32 Flags, UInt32 callback);
[DllImport("Advapi32.dll")]
public static extern BOOL GetThreadWaitChain(
IntPtr WctHandle,
UInt32 Context,
UInt32 flags,
UInt32 ThreadId,
WAITCHAIN_NODE_INFO NodeInfoArray,
UInt32 IsCycle
);
[StructLayout(LayoutKind.Sequential)]
public struct WAITCHAIN_NODE_INFO
{
public UInt32 ObjectType;
public UInt32 ObjectStatus;
public struct LockObject
{
string ObjectName;
UInt64 Timeout;
UInt32 Alertable;
}
public struct ThreadObject
{
UInt32 ProcessId;
UInt32 ThreadId;
UInt32 WaitTime;
UInt32 ContextSwitches;
}
}
如何调用GetThreadWaitChain函数?它接受指向WAITCHAIN_NODE_INFO结构体的指针。。。
目前,我就是这样想调用这个函数的(显然它不起作用):
void CollectWaitInformation(int threadId)
{
var wctHandle = OpenThreadWaitChainSession(0, 0);
WAITCHAIN_NODE_INFO info = new WAITCHAIN_NODE_INFO();
var result = GetThreadWaitChain(wctHandle, 0,
GetThreadWaitChainFlags.WCT_OUT_OF_PROC_COM_FLAG, threadID, info , 0);
}
我的C++类型映射到C#类型对吗?
GetThreadWaitChain函数原型不正确。应该是:
[DllImport("advapi32.dll", SetLastError = true)]
public static extern bool GetThreadWaitChain(
IntPtr WctHandle,
IntPtr Context,
UInt32 Flags,
int ThreadId,
ref int NodeCount,
[MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 4)]
[In, Out]
WAITCHAIN_NODE_INFO[] NodeInfoArray,
out int IsCycle
);
调用时,首先分配WAITCHAIN_NODE_INFO数组。
WAITCHAIN_NODE_INFO[] data = new WAITCHAIN_NODE_INFO[16];
相关文章:
- 什么时候调用组成单元对象的析构函数
- 对RValue对象调用的LValue ref限定成员函数
- 为什么使用 "this" 指针调用派生成员函数?
- 函数调用中参数的顺序重要吗
- OpenGL - 在抛出"__gnu_cxx::recursive_init_error"实例后终止调用?
- 基于另一个成员参数将函数调用从类传递给它的一个成员
- 为什么我的C#代码在调用回C++COM直到Task时会暂停.等待/线程.加入
- 在c++类上调用void函数
- 为什么 std::unique 不调用 std::sort?
- 调用专用模板时出错"no matching function for call to [...]"
- 选择要调用的构造函数
- C++为什么尽管我调用了void函数,它却不起作用
- 构造函数正在调用一个使用当前类类型的函数
- 变量没有改变?通过向量的函数调用
- 没有为自己的结构调用列表推回方法
- 调用'begin(int [n])'没有匹配函数
- 什么时候调用析构函数
- 如何用参数值调用函数(仅在运行时已知)
- std::cout.imbue()多重调用
- 函数何时会在c++中包含stack_Unwind_Resume调用