forked from dannycabrera/Get-iOS-Model
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Xamarin.iOS.DeviceHarware.cs
61 lines (51 loc) · 2.29 KB
/
Xamarin.iOS.DeviceHarware.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
using System;
using System.Runtime.InteropServices;
using Foundation;
using ObjCRuntime;
namespace Xamarin.Forms.Devices
{
public static class Hardware
{
private const string HardwareProperty = "hw.machine";
[DllImport(Constants.SystemLibrary)]
internal static extern int sysctlbyname([MarshalAs(UnmanagedType.LPStr)] string property, // name of the property
IntPtr output, // output
IntPtr oldLen, // IntPtr.Zero
IntPtr newp, // IntPtr.Zero
uint newlen // 0
);
public static string Version
{
get
{
try
{
// get the length of the string that will be returned
var pLen = Marshal.AllocHGlobal(sizeof(int));
sysctlbyname(HardwareProperty, IntPtr.Zero, pLen, IntPtr.Zero, 0);
var length = Marshal.ReadInt32(pLen);
// check to see if we got a length
if (length == 0)
{
Marshal.FreeHGlobal(pLen);
return "Unknown";
}
// get the hardware string
var pStr = Marshal.AllocHGlobal(length);
sysctlbyname(HardwareProperty, pStr, pLen, IntPtr.Zero, 0);
// convert the native string into a C# string
var hardwareStr = Marshal.PtrToStringAnsi(pStr);
// cleanup
Marshal.FreeHGlobal(pLen);
Marshal.FreeHGlobal(pStr);
return hardwareStr;
}
catch (Exception ex)
{
Console.WriteLine("DeviceHardware.Version Ex: " + ex.Message);
}
return "Unknown";
}
}
}
}