Hello everyone,
I need to call the native function GetIfTable() and set up everything to
do so as follows:
----- START OF CODE -----
[DllImport("iphlpapi.dll", SetLastError = true)]
public static extern int GetIfTable(
IntPtr pIfTable,
ref uint pdwSize,
bool bOrder
);
[StructLayout(LayoutKind.Sequential)]
public struct MIB_IFROW {
[MarshalAs(UnmanagedType.ByValTStr, SizeConst =
IpApi32.MAX_INTERFACE_NAME_LEN)]
public string wszName;
public uint dwIndex;
public uint dwType;
public uint dwMtu;
public uint dwSpeed;
public uint dwPhysAddrLen;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = IpApi32.MAXLEN_PHYSADDR)]
public byte[] bPhysAddr;
public uint dwAdminStatus;
public uint dwOperStatus;
public uint dwLastChange;
public uint dwInOctets;
public uint dwInUcastPkts;
public uint dwInNUcastPkts;
public uint dwInDiscards;
public uint dwInErrors;
public uint dwInUnknownProtos;
public uint dwOutOctets;
public uint dwOutUcastPkts;
public uint dwOutNUcastPkts;
public uint dwOutDiscards;
public uint dwOutErrors;
public uint dwOutQLen;
public uint dwDescrLen;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = IpApi32.MAXLEN_IFDESCR)]
public byte[] bDescr;
}
[StructLayout(LayoutKind.Sequential)]
public struct MIB_IFTABLE {
public int dwNumEntries;
public MIB_IFROW[] table;
}
----- END OF CODE -----
The only problem I have is concerning the wszName member of MIB_IFROW.
Now comes the method that makes use of it and troubles me:
----- START OF CODE -----
public void GetIfTable() {
uint size = 0;
int result = 0;
IntPtr pBuffer = IntPtr.Zero;
result = IpApi32Wrapper.GetIfTable(IntPtr.Zero, ref size, false);
if (result == ErrorCodes.ERROR_INSUFFICIENT_BUFFER) {
pBuffer = Marshal.AllocHGlobal((int)size);
result = IpApi32Wrapper.GetIfTable(pBuffer, ref size, false);
} else {
throw new System.ComponentModel.Win32Exception(result);
}
if (result != ErrorCodes.NO_ERROR) {
throw new System.ComponentModel.Win32Exception(result);
} else {
int count = Marshal.ReadInt32(pBuffer);
int ptr = pBuffer.ToInt32() + Marshal.SizeOf(typeof(int));
MIB_IFROW ifRow;
for (int i = 0; i < count; i++ ) {
ifRow = (MIB_IFROW)Marshal.PtrToStructure(
(IntPtr)ptr, typeof(MIB_IFROW));
Console.WriteLine("ifRow.wszName: {0}", ifRow.wszName);
ptr += Marshal.SizeOf(typeof(MIB_IFROW));
}
}
Marshal.FreeHGlobal(pBuffer);
}
----- END OF CODE -----
The question is: why is the following output produced?
ifRow.wszName:
ifRow.wszName: Realtek RTL8139-Family-PCI-Fast Ethernet-NIC #2
ifRow.wszName:
ifRow.wszName:
ifRow.wszName:
There are 4 adapters installed (excl. Loopback), but only one entry is
displayed correctly. I'v written a small C program that does almost the
same thing and it prints all entries as expected.
As you can see, the MIB_IFROW member wszName is a wide character string
and may need special treatment by using Charset.Unicode and the like,
but it seem have no effect, since I already tried.
What's wrong with my code? Any ideas highly appreciated. Thanks.
Catherine
Mattias Sjögren - 01 Nov 2005 18:04 GMT
Catherine,
>What's wrong with my code? Any ideas highly appreciated. Thanks.
wszName is a Unicode string but the struct has ANSI string marshaling
by default. Add
CharSet=CharSet.Unicode
to the StructLayout attribute.
I must say I'm surprised you even got one correct output line.
Mattias

Signature
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Catherine S. Lowery - 02 Nov 2005 17:49 GMT
> Catherine,
Hi Mattias,
>>What's wrong with my code? Any ideas highly appreciated. Thanks.
>
[quoted text clipped - 4 lines]
>
> to the StructLayout attribute.
Thanks for your reply. I already tried with every CharSet w/o success.
Luckily, I know how to obtain the same information alternatively, but
unsolved problems like these cause real headaches. Doubt that it will
be the last time I run into this thing.
> I must say I'm surprised you even got one correct output line.
As I found out later, it's not the correct line for this adapter that
has a different index as ipconfig says. Seems to be some kind of side
effect.
> Mattias
Thanks for your effort. I'll keep trying and share the results.
Catherine