- This topic has 4 replies, 2 voices, and was last updated 15 years, 3 months ago by
Vadim Smirnov.
-
AuthorPosts
-
July 6, 2010 at 1:34 pm #5332
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?
July 7, 2010 at 8:43 pm #6960When 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?
July 8, 2010 at 1:08 am #6961Yes, 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.
July 8, 2010 at 1:30 am #6962Ok, 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);
July 8, 2010 at 7:44 am #6963Thank you for the information. We will change declaration of GetRasLinks for the next release.
-
AuthorPosts
- You must be logged in to reply to this topic.
