Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / Interop / November 2005

Tip: Looking for answers? Try searching our database.

Problems calling native GetIfTable function

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Catherine S. Lowery - 01 Nov 2005 13:26 GMT
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

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.