Merge pull request #5198 from BrzVlad/fix-binprot-stats
[mono.git] / mcs / class / System / System.Net.NetworkInformation / Win32NetworkInterfaceMarshal.cs
index 3c3256c709629f5e10029a9e6a004a337bc13c89..88ae0b8e3589b60d62e6609279f20a2eb10815bf 100644 (file)
@@ -33,52 +33,48 @@ using System.Text;
 
 namespace System.Net.NetworkInformation
 {
-       // They are mostly defined in iptypes.h (included by iphlpapi.h).
-       // grep around /usr/include/w32api/* for identifiers you are curious.
 
-       [StructLayout (LayoutKind.Sequential)]
-       class Win32_FIXED_INFO
-       {
-               // Can't have unresolvable pinvokes on ios
-#if !MOBILE
+
+       class Win32NetworkInterface {
+                               // Can't have unresolvable pinvokes on ios
+#if WIN_PLATFORM
                [DllImport ("iphlpapi.dll", SetLastError = true)]
-               static extern int GetNetworkParams (byte [] bytes, ref int size);
+               static extern int GetNetworkParams (IntPtr ptr, ref int size);
 #endif
 
-               static Win32_FIXED_INFO fixed_info;
+               static Win32_FIXED_INFO fixedInfo;
+               static bool initialized = false;
 
-               public static Win32_FIXED_INFO Instance {
+               public static Win32_FIXED_INFO FixedInfo {
                        get {
-                               if (fixed_info == null)
-                                       fixed_info = GetInstance ();
-                               return fixed_info;
-                       }
-               }
-
-               static Win32_FIXED_INFO GetInstance ()
-               {
-#if !MOBILE
-                       int len = 0;
-                       byte [] bytes = null;
-                       GetNetworkParams (null, ref len);
-                       bytes = new byte [len];
-                       GetNetworkParams (bytes, ref len);
-                       Win32_FIXED_INFO info = new Win32_FIXED_INFO ();
-                       unsafe {
-                               fixed (byte* ptr = bytes) {
-                                       Marshal.PtrToStructure ((IntPtr) ptr, info);
-                               }
-                       }
-                       return info;
+                               if (!initialized) {
+#if WIN_PLATFORM
+                                       int len = 0;
+                                       IntPtr ptr = IntPtr.Zero;
+                                       GetNetworkParams (ptr, ref len);
+                                       ptr = Marshal.AllocHGlobal(len);
+                                       GetNetworkParams (ptr, ref len);
+                                       fixedInfo = Marshal.PtrToStructure<Win32_FIXED_INFO> (ptr);
 #else
-               throw new NotImplementedException ();
+                                       throw new NotImplementedException ();
 #endif
+                                       initialized = true;
+                               }
+                               return fixedInfo;
+                       }
                }
+       }
+
+       // They are mostly defined in iptypes.h (included by iphlpapi.h).
+       // grep around /usr/include/w32api/* for identifiers you are curious.
+
+       [StructLayout (LayoutKind.Sequential)]
+       struct Win32_FIXED_INFO
+       {
 
                const int MAX_HOSTNAME_LEN = 128;
                const int MAX_DOMAIN_NAME_LEN = 128;
                const int MAX_SCOPE_ID_LEN = 256;
-
                [MarshalAs (UnmanagedType.ByValTStr, SizeConst = MAX_HOSTNAME_LEN + 4)]
                public string HostName;
                [MarshalAs (UnmanagedType.ByValTStr, SizeConst = MAX_DOMAIN_NAME_LEN + 4)]
@@ -105,7 +101,7 @@ namespace System.Net.NetworkInformation
        }
 
        [StructLayout (LayoutKind.Sequential, CharSet = CharSet.Unicode)]
-       class Win32_IP_ADAPTER_ADDRESSES {
+       struct Win32_IP_ADAPTER_ADDRESSES {
                public AlignmentUnion Alignment;
                public IntPtr Next; // to Win32_IP_ADAPTER_ADDRESSES
                [MarshalAs (UnmanagedType.LPStr)]
@@ -151,7 +147,7 @@ namespace System.Net.NetworkInformation
        }
 
        [StructLayout (LayoutKind.Sequential)]
-       class Win32_IP_ADAPTER_INFO
+       struct Win32_IP_ADAPTER_INFO
        {
                const int MAX_ADAPTER_NAME_LENGTH = 256;
                const int MAX_ADAPTER_DESCRIPTION_LENGTH = 128;