0

I need to get the MAC address of a Windows Mobile device. Is it possible to do this without using, say, OpenNetCF, only by .NET Compact Framework? The solution is in C# (Visual Studio 2008, a smart device project).

Having read Read MAC Address from network adapter in .NET, I wanted to use the example by plinth, but there was no kernel32.dll on the device.

Is there aother way to get the MAC address in .NET Compact Framework, or how could I replace the following methods:

     [DllImport("Kernel32.dll", EntryPoint = "CopyMemory")]
     private static extern void ByteArray_To_IPAdapterInfo(ref AdapterInfo dst, Byte[] src, int size);
     [DllImport("Kernel32.dll", EntryPoint = "CopyMemory")]
     private static extern void IntPtr_To_IPAdapterInfo(ref AdapterInfo dst, IntPtr src, int size);
Community
  • 1
  • 1
horgh
  • 17,918
  • 22
  • 68
  • 123

1 Answers1

0

If you want to get the MAC address, but without using a library like OpenNETCF's SDF then you simply have to do the same thing it does in your own code. You need to P/Invoke GetAdaptersInfo, something like this:

[DllImport("iphlpapi.dll", SetLastError = true)]
public static extern int GetAdaptersInfo(byte[] pAdapterInfo, ref uint pOutBufLen);

You'll want to call it once with a null first parameter to get the size, then allocate a byte array of the proper size and recall it. It will fill it with an array of IP_ADAPTER_INFO structs that you then parse for each adapter in the system. The Address member is what you're after for the MAC address.

ctacke
  • 66,480
  • 18
  • 94
  • 155
  • But how should I convert the received byte array into array of structs? Or how should I extract each struct from the byte array? – horgh Jul 08 '12 at 16:16
  • In the thread, I mentioned earlier in the question, plinth used methods from kernel32.dll (IntPtr_To_IPAdapterInfo, ByteArray_To_IPAdapterInfo). But kernel32.dll is not available on windows mobile (isn't it?). – horgh Jul 08 '12 at 16:18
  • I parsed the byte array in the SDF code using unsafe code as it seemed the fastest way. You could just as easily create a sruct and do member-wise setting of variables. If all you want is the MAC, you could actually just copy from the resulting array at the proper offset into a local array for the address bytes. – ctacke Jul 08 '12 at 23:04
  • I don't follow as to why you'd use CopyMemory like you have in your question, even on the desktop. `Marshal.PtrToStructure` seems to be more appropriate, but the CF marshaler is probably going to fail on a struct this complex. – ctacke Jul 08 '12 at 23:06