Advertisement
jmawebtech

devicehardware

Oct 17th, 2012
2,451
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.00 KB | None | 0 0
  1. using System;
  2. using System.Runtime.InteropServices;
  3. using MonoTouch.Foundation;
  4.  
  5. using Constants = MonoTouch.Constants;
  6.  
  7. namespace JMABarcodeMT
  8. {
  9.     /// <summary>
  10.     /// This code source is:
  11.     /// http://snippets.dzone.com/user/zachgris
  12.     ///
  13.     /// Detail descriptions how to determine what iPhone hardware is:
  14.     /// http://www.drobnik.com/touch/2009/07/determining-the-hardware-model/    
  15.     /// </summary>
  16.     public class DeviceHardware
  17.     {
  18.         // make sure to add a 'using System.Runtime.InteropServices;' line to your file
  19.         public const string HardwareProperty = "hw.machine";
  20.  
  21.         public enum HardwareVersion
  22.         {
  23.             iPhone1G,
  24.             iPhone2G,
  25.             iPhone3G,
  26.             iPhone4,
  27.             iPhone5,
  28.             iPod1G,
  29.             iPod2G,
  30.             iPod3G,
  31.             iPod4G,
  32.             iPod5G,
  33.             iPad,
  34.             iPad2,
  35.             Simulator,
  36.             Unknown
  37.         }
  38.  
  39.         [DllImport(Constants.SystemLibrary)]
  40.         internal static extern int sysctlbyname([MarshalAs(UnmanagedType.LPStr)] string property, // name of the property
  41.                                                IntPtr output, // output
  42.                                                IntPtr oldLen, // IntPtr.Zero
  43.                                                IntPtr newp, // IntPtr.Zero
  44.                                                uint newlen // 0
  45.                                               );
  46.  
  47.        
  48.        
  49.         public static HardwareVersion Version
  50.         {
  51.             get
  52.             {
  53.                
  54.                
  55.                 // get the length of the string that will be returned
  56.                 var pLen = Marshal.AllocHGlobal(sizeof(int));
  57.                 sysctlbyname(DeviceHardware.HardwareProperty, IntPtr.Zero, pLen, IntPtr.Zero, 0);
  58.  
  59.                 var length = Marshal.ReadInt32(pLen);
  60.  
  61.                 // check to see if we got a length
  62.                 if (length == 0)
  63.                 {
  64.                     Marshal.FreeHGlobal(pLen);
  65.                     return HardwareVersion.Unknown;
  66.                 }
  67.  
  68.  
  69.                 // get the hardware string
  70.                 var pStr = Marshal.AllocHGlobal(length);
  71.                 sysctlbyname(DeviceHardware.HardwareProperty, pStr, pLen, IntPtr.Zero, 0);
  72.  
  73.                 // convert the native string into a C# string
  74.                 var hardwareStr = Marshal.PtrToStringAnsi(pStr);
  75.                 var ret = HardwareVersion.Unknown;
  76.  
  77.                
  78. #if DEBUG
  79.                 Console.WriteLine(hardwareStr);
  80. #endif
  81.  
  82.                 // determine which hardware we are running
  83.                 if (hardwareStr == "iPhone1,1")
  84.                     ret = HardwareVersion.iPhone1G;
  85.                 else if (hardwareStr == "iPhone1,2")
  86.                     ret = HardwareVersion.iPhone2G;
  87.                 else if (hardwareStr == "iPhone2,1")
  88.                     ret = HardwareVersion.iPhone3G;
  89.                 else if (hardwareStr == "iPhone3,1")
  90.                     ret = HardwareVersion.iPhone4;
  91.                 else if(hardwareStr == "iPhone4,1")
  92.                     ret = HardwareVersion.iPhone5;
  93.                 else if (hardwareStr == "iPod1,1")
  94.                     ret = HardwareVersion.iPod1G;
  95.                 else if (hardwareStr == "iPod2,1")
  96.                     ret = HardwareVersion.iPod2G;
  97.                 else if (hardwareStr == "iPod3,1")
  98.                     ret = HardwareVersion.iPod3G;
  99.                 else if (hardwareStr == "iPod4,1")
  100.                     ret = HardwareVersion.iPod4G;
  101.                 else if (hardwareStr == "iPad1,1")
  102.                     ret = HardwareVersion.iPad;
  103.                 else if (hardwareStr == "iPad2,1")
  104.                     ret = HardwareVersion.iPad2;
  105.                 else if (hardwareStr == "iPad2,2")
  106.                     ret = HardwareVersion.iPad2;
  107.                 else if (hardwareStr == "iPad2,3")
  108.                     ret = HardwareVersion.iPad2;
  109.                 else if (hardwareStr == "i386")
  110.                     ret = HardwareVersion.Simulator;
  111.  
  112.                 // cleanup
  113.                 Marshal.FreeHGlobal(pLen);
  114.                 Marshal.FreeHGlobal(pStr);
  115.  
  116.                 return ret;
  117.             }
  118.         }
  119.     }
  120. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement