IntPtr 到结构体数组

发布于 2024-11-18 06:43:43 字数 3396 浏览 10 评论 0原文

我通过 VB.Net 调用本机 API,传递指向结构数组 (RootCausesInfo) 的指针,该数组又包含指向结构数组 (RepairInfoEx) 的指针。 API 的本机签名位于 MSDN - NdfDiagnoseIncident

我在 VB.Net 中使用的结构定义和 PInvoke 调用是:

<System.Runtime.InteropServices.StructLayoutAttribute(System.Runtime.InteropServices.LayoutKind.Sequential)> _
Public Structure RepairInfoEx
    Public repair As RepairInfo
    Public repairRank As UShort
End Structure

<System.Runtime.InteropServices.StructLayoutAttribute(System.Runtime.InteropServices.LayoutKind.Sequential)> _
Public Structure RootCauseInfo
    <System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.LPWStr)> _
    Public pwszDescription As String
    Public rootCauseID As GUID
    Public rootCauseFlags As UInteger
    Public networkInterfaceID As GUID
    Public pRepairs As IntPtr
    Public repairCount As UShort
End Structure

    <System.Runtime.InteropServices.DllImportAttribute("ndfapi.dll", EntryPoint:="NdfDiagnoseIncident", CallingConvention:=System.Runtime.InteropServices.CallingConvention.StdCall)> _
    Public Function NdfDiagnoseIncident(<System.Runtime.InteropServices.InAttribute()> ByVal Handle As System.IntPtr, <System.Runtime.InteropServices.OutAttribute()> ByRef RootCauseCount As UInteger, ByRef RootCauses As IntPtr, ByVal dwWait As Integer, ByVal dwFlags As UInteger) As Integer
    End Function

调用上述 API 的代码是:

    Dim rcInfoPtrToArray As IntPtr = Nothing
    Dim intRootCauseInfoSize As Integer = Marshal.SizeOf(GetType(RootCauseInfo))
    Dim intRepairInfoExSize As Integer = Marshal.SizeOf(GetType(RepairInfoEx))
    If HResult = 0 Then
        HResult = NdfDiagnoseIncident(ndfHandle, rootCauseCount, rcInfoPtrToArray, NativeConstants.INFINITE, 0)
        If rootCauseCount > 0 Then
            For i = 0 To rootCauseCount - 1 
                Dim rootCausePtr As IntPtr = New IntPtr(rcInfoPtrToArray.ToInt32 + intRootCauseInfoSize * i)
                Dim causeObj As RootCauseInfo = CType(Marshal.PtrToStructure(rootCausePtr, GetType(RootCauseInfo)), RootCauseInfo)
                Dim pRepairs(causeObj.repairCount) As IntPtr
                Marshal.Copy(causeObj.pRepairs, pRepairs, 0, causeObj.repairCount)
                For Each pRepair As IntPtr In pRepairs 
                    Dim repair As RepairInfoEx = CType(Marshal.PtrToStructure(pRepair, GetType(RepairInfoEx)), RepairInfoEx)
                    MsgBox(repair.repair.pwszDescription)
                Next
            Next
        End If
    End If

当 API 调用返回时,它填充rcInfoPtrToArray,带有指向 RootCauseInfo 结构的指针地址。我使用以下行获取 RootCauseInfo 的每个实例:

Dim rootCausePtr As IntPtr = New IntPtr(rcInfoPtrToArray.ToInt32 + intRootCauseInfoSize * i)

这工作正常。我使用类似的 IntPtr 算术导航到 RepairInfoEx 实例的各个元素:

Dim repairInfoExPtr As IntPtr = New IntPtr(causeObj.pRepairs.ToInt32 + intRepairInfoExSize * j)

当我使用以下行将 RepairInfoExPtr 转换为 RepairInfoEx 结构时,对于 j=0 的 RepairInfoEx 的第一个实例,它再次工作正常:

Dim repair As RepairInfoEx = CType(Marshal.PtrToStructure(repairInfoExPtr, GetType(RepairInfoEx)), RepairInfoEx)

但在 j=1 的下一次迭代中,指针似乎搞砸了,我得到“抛出了‘System.ExecutionEngineException’类型的异常。”错误。我做错了什么?

I am calling a native API through VB.Net passing a pointer to array of structures (RootCausesInfo) which in turn contains pointer to array of structures (RepairInfoEx). The native signature of the API is on MSDN - NdfDiagnoseIncident

The structure definition and PInvoke call that I used in VB.Net are:

<System.Runtime.InteropServices.StructLayoutAttribute(System.Runtime.InteropServices.LayoutKind.Sequential)> _
Public Structure RepairInfoEx
    Public repair As RepairInfo
    Public repairRank As UShort
End Structure

<System.Runtime.InteropServices.StructLayoutAttribute(System.Runtime.InteropServices.LayoutKind.Sequential)> _
Public Structure RootCauseInfo
    <System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.LPWStr)> _
    Public pwszDescription As String
    Public rootCauseID As GUID
    Public rootCauseFlags As UInteger
    Public networkInterfaceID As GUID
    Public pRepairs As IntPtr
    Public repairCount As UShort
End Structure

    <System.Runtime.InteropServices.DllImportAttribute("ndfapi.dll", EntryPoint:="NdfDiagnoseIncident", CallingConvention:=System.Runtime.InteropServices.CallingConvention.StdCall)> _
    Public Function NdfDiagnoseIncident(<System.Runtime.InteropServices.InAttribute()> ByVal Handle As System.IntPtr, <System.Runtime.InteropServices.OutAttribute()> ByRef RootCauseCount As UInteger, ByRef RootCauses As IntPtr, ByVal dwWait As Integer, ByVal dwFlags As UInteger) As Integer
    End Function

The code that calls the above API is:

    Dim rcInfoPtrToArray As IntPtr = Nothing
    Dim intRootCauseInfoSize As Integer = Marshal.SizeOf(GetType(RootCauseInfo))
    Dim intRepairInfoExSize As Integer = Marshal.SizeOf(GetType(RepairInfoEx))
    If HResult = 0 Then
        HResult = NdfDiagnoseIncident(ndfHandle, rootCauseCount, rcInfoPtrToArray, NativeConstants.INFINITE, 0)
        If rootCauseCount > 0 Then
            For i = 0 To rootCauseCount - 1 
                Dim rootCausePtr As IntPtr = New IntPtr(rcInfoPtrToArray.ToInt32 + intRootCauseInfoSize * i)
                Dim causeObj As RootCauseInfo = CType(Marshal.PtrToStructure(rootCausePtr, GetType(RootCauseInfo)), RootCauseInfo)
                Dim pRepairs(causeObj.repairCount) As IntPtr
                Marshal.Copy(causeObj.pRepairs, pRepairs, 0, causeObj.repairCount)
                For Each pRepair As IntPtr In pRepairs 
                    Dim repair As RepairInfoEx = CType(Marshal.PtrToStructure(pRepair, GetType(RepairInfoEx)), RepairInfoEx)
                    MsgBox(repair.repair.pwszDescription)
                Next
            Next
        End If
    End If

When the API call returns, it populates the rcInfoPtrToArray with a pointer address to RootCauseInfo structures. I get each instance of RootCauseInfo with following line:

Dim rootCausePtr As IntPtr = New IntPtr(rcInfoPtrToArray.ToInt32 + intRootCauseInfoSize * i)

This works fine. I use similar IntPtr arithmetic to navigate to individual elements of RepairInfoEx instances:

Dim repairInfoExPtr As IntPtr = New IntPtr(causeObj.pRepairs.ToInt32 + intRepairInfoExSize * j)

When I get to converting repairInfoExPtr to RepairInfoEx structure with following line, it again works fine for first instance of RepairInfoEx with j=0:

Dim repair As RepairInfoEx = CType(Marshal.PtrToStructure(repairInfoExPtr, GetType(RepairInfoEx)), RepairInfoEx)

But in the next iteration of j=1, the pointer seems screwed up and I get "Exception of type 'System.ExecutionEngineException' was thrown." error. What am I doing wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文