Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
NativeLibrary.cs
Go to the documentation of this file.
1 // Copyright (c) 2014 Silicon Studio Corp. (http://siliconstudio.co.jp)
2 // This file is distributed under GPL v3. See LICENSE.md for details.
3 using System;
4 using System.Globalization;
5 using System.IO;
6 using System.Runtime.InteropServices;
7 
8 namespace SiliconStudio.Core
9 {
10  public static class NativeLibrary
11  {
12 #if SILICONSTUDIO_PLATFORM_WINDOWS_DESKTOP
13  [DllImport("kernel32", EntryPoint = "LoadLibrary", SetLastError = true, CharSet = CharSet.Unicode)]
14  static extern IntPtr LoadLibrary(string lpFileName);
15 #elif SILICONSTUDIO_PLATFORM_WINDOWS_PHONE
16  [DllImport("PhoneAppModelHost", CharSet = CharSet.Unicode, SetLastError = true)]
17  private static extern IntPtr LoadPackagedLibrary(string libraryName, uint reserved);
18 #elif SILICONSTUDIO_PLATFORM_WINDOWS_STORE
19  [DllImport("kernel32", CharSet = CharSet.Unicode, SetLastError = true)]
20  private static extern IntPtr LoadPackagedLibrary(string libraryName, uint reserved);
21 #endif
22 
23  /// <summary>
24  /// Defines the location of the core native DLL.
25  /// </summary>
26 #if SILICONSTUDIO_PLATFORM_IOS
27  public const string LibraryName = "__Internal";
28 #else
29  public const string LibraryName = "libcore.dll";
30 #endif
31 
32  /// <summary>
33  /// Defines the calling convention for P/Invoking the native core methods.
34  /// </summary>
35  public const CallingConvention CallConvention = CallingConvention.Cdecl;
36 
37  /// <summary>
38  /// Try to preload the library.
39  /// This is useful when we want to have AnyCPU .NET and CPU-specific native code.
40  /// Only available on Windows for now.
41  /// </summary>
42  /// <param name="libraryName">Name of the library.</param>
43  /// <exception cref="System.InvalidOperationException"></exception>
44  public static void PreloadLibrary(string libraryName)
45  {
46 #if SILICONSTUDIO_PLATFORM_WINDOWS_DESKTOP
47  var systemInfo = new SYSTEM_INFO();
48  GetNativeSystemInfo(out systemInfo);
49 
50  string cpu;
51  if (systemInfo.processorArchitecture == PROCESSOR_ARCHITECTURE.PROCESSOR_ARCHITECTURE_ARM)
52  cpu = "ARM";
53  else
54  cpu = IntPtr.Size == 8 ? "x64" : "x86";
55  var libraryFilename = Path.Combine(Path.GetDirectoryName(typeof(NativeLibrary).Assembly.Location), cpu, libraryName);
56  var result = LoadLibrary(libraryFilename);
57 
58  if (result == IntPtr.Zero)
59  throw new InvalidOperationException(string.Format("Could not load native library {0} using CPU architecture {1}.", libraryName, cpu));
60 #endif
61  }
62 
63 #if SILICONSTUDIO_PLATFORM_WINDOWS_RUNTIME
64  private const string SYSINFO_FILE = "API-MS-WIN-CORE-SYSINFO-L1-2-1.DLL";
65 #else
66  private const string SYSINFO_FILE = "kernel32.dll";
67 #endif
68 
69  [DllImport(SYSINFO_FILE)]
70  static extern void GetNativeSystemInfo(out SYSTEM_INFO lpSystemInfo);
71 
72  [StructLayout(LayoutKind.Sequential)]
73  struct SYSTEM_INFO
74  {
75  public PROCESSOR_ARCHITECTURE processorArchitecture;
76  ushort reserved;
77  public uint pageSize;
78  public IntPtr minimumApplicationAddress;
79  public IntPtr maximumApplicationAddress;
80  public IntPtr activeProcessorMask;
81  public uint numberOfProcessors;
82  public uint processorType;
83  public uint allocationGranularity;
84  public ushort processorLevel;
85  public ushort processorRevision;
86  }
87 
88  enum PROCESSOR_ARCHITECTURE : ushort
89  {
90  PROCESSOR_ARCHITECTURE_AMD64 = 9,
91  PROCESSOR_ARCHITECTURE_ARM = 5,
92  PROCESSOR_ARCHITECTURE_IA64 = 6,
93  PROCESSOR_ARCHITECTURE_INTEL = 0,
94  PROCESSOR_ARCHITECTURE_UNKNOWN = 0xffff
95  }
96  }
97 }
static void PreloadLibrary(string libraryName)
Try to preload the library. This is useful when we want to have AnyCPU .NET and CPU-specific native c...