element
Active member
Hi,
Ive been attempting to get this to work for around 5 hours now, getting a little tired of it failing me :S
Please can somebody tell me where Im going wrong, actually - I know where Im going wrong but I dont know how to fix it.
Above are the classes used. I cannot use Structures for some reason, it says:
Additional information: Can not marshal field table of type MIB_IFTABLE: This type can not be marshaled as a structure field.
(Note this only happens when I un-comment out the table variable in the MIB_IFTABLE structure)
This is the error I get with the code I use below:
An unhandled exception of type System.Runtime.InteropServices.SafeArrayTypeMismatchException occurred in mscorlib.dll
Additional information: Mismatch has occurred between the runtime type of the array and the sub type recorded in the metadata.
It is strange, because when I comment out the table variable, it gives me the value of 1311096 in dwNumEntries. This value *should* be two (2 network devices, loopback + NIC)
Thank you for any help/advice in advance, Tom
Ive been attempting to get this to work for around 5 hours now, getting a little tired of it failing me :S
Please can somebody tell me where Im going wrong, actually - I know where Im going wrong but I dont know how to fix it.
Code:
<StructLayout(LayoutKind.Sequential)> Public Class MIB_IFTABLE
Public dwNumEntries As Integer
<MarshalAs(UnmanagedType.SafeArray)> Public table() As MIB_IFROW
End Class
<StructLayout(LayoutKind.Sequential)> Public Class MIB_IFROW
<VBFixedArray(511)> Public wszName() As Byte 511
Public wszName As Byte()
Public dwIndex As Int32
Public dwType As Int32
Public dwMtu As Int32
Public dwSpeed As Int32
Public dwPhysAddrLen As Int32
<VBFixedArray(7)> Public bPhysAddr() As Byte 7
Public bPhysAddr() As Byte
Public dwAdminStatus As Int32
Public dwOperStatus As Int32
Public dwLastChange As Int32
Public dwInOctets As Int32
Public dwInUcastPkts As Int32
Public dwInNUcastPkts As Int32
Public dwInDiscards As Int32
Public dwInErrors As Int32
Public dwInUnknownProtos As Int32
Public dwOutOctets As Int32
Public dwOutUcastPkts As Int32
Public dwOutNUcastPkts As Int32
Public dwOutDiscards As Int32
Public dwOutErrors As Int32
Public dwOutQLen As Int32
Public dwDescrLen As Int32
<VBFixedArray(255)> Public bDescr() As Byte 255
Public bDescr() As Byte
End Class
Above are the classes used. I cannot use Structures for some reason, it says:
Additional information: Can not marshal field table of type MIB_IFTABLE: This type can not be marshaled as a structure field.
(Note this only happens when I un-comment out the table variable in the MIB_IFTABLE structure)
This is the error I get with the code I use below:
An unhandled exception of type System.Runtime.InteropServices.SafeArrayTypeMismatchException occurred in mscorlib.dll
Additional information: Mismatch has occurred between the runtime type of the array and the sub type recorded in the metadata.
Code:
Dim lRetSize As Int32, lRows As Int32, ret As Long
Dim IfRowTable As New MIB_IFTABLE
Dim pStruct As IntPtr = IntPtr.Zero
ret = GetIfTable(pStruct, lRetSize, 0)
pStruct = Marshal.AllocHGlobal(lRetSize)
ret = GetIfTable(pStruct, lRetSize, 0)
IfRowTable = Marshal.PtrToStructure(pStruct, GetType(MIB_IFTABLE))
Marshal.FreeHGlobal(pStruct)
It is strange, because when I comment out the table variable, it gives me the value of 1311096 in dwNumEntries. This value *should* be two (2 network devices, loopback + NIC)
Thank you for any help/advice in advance, Tom