/
PlatformDetection.cs
94 lines (84 loc) · 2.67 KB
/
PlatformDetection.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
// Parts based on code by MJ Hutchinson http://mjhutchinson.com/journal/2010/01/25/integrating_gtk_application_mac
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
namespace RemObjects.Mono.Helpers
{
public enum PlatformType
{
Windows,
WinCE,
XBox,
Mac,
Linux,
UnixUnknown,
Unknown
}
public static class PlatformSupport
{
static PlatformType platform;
static String unameResult;
[DllImport("libc")]
extern static int uname(IntPtr buf);
static string intuname()
{
IntPtr buf = IntPtr.Zero;
try
{
buf = System.Runtime.InteropServices.Marshal.AllocHGlobal(8192);
// This is a hacktastic way of getting sysname from uname ()
if (uname(buf) == 0)
{
return System.Runtime.InteropServices.Marshal.PtrToStringAnsi(buf);
}
}
catch
{
}
finally
{
if (buf != IntPtr.Zero)
System.Runtime.InteropServices.Marshal.FreeHGlobal(buf);
}
return null;
}
static PlatformSupport()
{
switch (Environment.OSVersion.Platform)
{
case PlatformID.WinCE: platform = PlatformType.WinCE; break;
case PlatformID.Win32NT: platform = PlatformType.Windows; break;
case PlatformID.Win32S: platform = PlatformType.Windows; break;
case PlatformID.Win32Windows: platform = PlatformType.Windows; break;
case PlatformID.Xbox: platform = PlatformType.XBox; break;
default:
unameResult = intuname();
if (unameResult == null)
platform = PlatformType.Unknown;
else if (unameResult == "Linux")
platform = PlatformType.Linux;
else if (unameResult == "Darwin")
platform = PlatformType.Mac;
else
platform = PlatformType.UnixUnknown;
break;
}
}
public static PlatformType Platform
{
get
{
return platform;
}
}
public static string UNameResult
{
get
{
return unameResult;
}
}
}
}