Getting RAS_LINKS info from C#

Home Forums Discussions Support Portal Getting RAS_LINKS info from C#

This topic contains 4 replies, has 2 voices, and was last updated by  Vadim Smirnov 6 years, 7 months ago.

Viewing 5 posts - 1 through 5 (of 5 total)
  • Author
    Posts
  • #5332

    libzhark
    Participant

    I’m trying to extend the C# listadapters example to display information on WAN links, but the RAS_LINKS structure returned by GetRasLinks always has nNumberOfLinks of 0. I’m adding the following code inside the adapter loop, after it prints the adapter mode.


    RAS_LINKS pLinks = new RAS_LINKS();
    GCHandle.Alloc(pLinks);
    Ndisapi.GetRasLinks(hNdisapi, AdList.m_nAdapterHandle, ref pLinks);
    Console.WriteLine("RAS_LINKS:" + pLinks.nNumberOfLinks);

    The C++ example returns the proper information on links when i plug in a 3g card, but from C# it always tells me nothing is there. To get this to run, I did have to change RAS_LINKS_MAX to 32 so that the runtime would stop complaining about too large a data structure to marshal, so maybe that has something to do with it?

    #6960

    Vadim Smirnov
    Moderator

    When driver receives the memory block it checks if it has the correct size before filling it with data. If you shrinked the RAS_LINKS structure and passed smaller memory block to driver then it just has failed the operation. Is there a problem in C# with allocating large block of unmanaged memory?

    #6961

    libzhark
    Participant

    Yes, when calling the line

    Ndisapi.GetRasLinks(hNdisapi, AdList.m_nAdapterHandle, ref pLinks);

    an exception is thrown:

    System.Runtime.InteropServices.MarshalDirectiveException: Cannot marshal 'parameter #3': Internal limitation: structure is too complex or too large.

    This happens when RAS_LINKS_MAX = 256. From quick googling, it appears there is a hard 64k (65535 byte) limit for marshaling a single chunk of data.

    #6962

    libzhark
    Participant

    Ok, got it working. Need to change the declaration of GetRasLinks to:


    [DllImport("ndisapi.dll")]
    public static extern bool GetRasLinks(IntPtr hOpen, IntPtr hAdapter, IntPtr pLinks);

    And then you can call it via:


    IntPtr data = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(RAS_LINKS)));
    Ndisapi.GetRasLinks(hNdisapi, AdList.m_nAdapterHandle, data);
    RAS_LINKS RasLinks = (RAS_LINKS)Marshal.PtrToStructure(data, typeof(RAS_LINKS));
    Console.WriteLine("RAS_LINKS:" + RasLinks.nNumberOfLinks);
    #6963

    Vadim Smirnov
    Moderator

    Thank you for the information. We will change declaration of GetRasLinks for the next release.

Viewing 5 posts - 1 through 5 (of 5 total)

You must be logged in to reply to this topic.