IntPtr到struct的数组

时间:2011-06-29 19:23:03

标签: vb.net struct structure intptr

我通过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调用返回时,它会使用指向RootCauseInfo结构的指针地址填充rcInfoPtrToArray。我使用以下行获取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'的异常被抛出。”错误。我做错了什么?

0 个答案:

没有答案