1

我通过 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”类型的异常。错误。我究竟做错了什么?

4

0 回答 0