1
0

UnsafeNativeMethods.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. using Microsoft.Win32.SafeHandles;
  2. using System;
  3. using System.Runtime.InteropServices;
  4. namespace WinSCP
  5. {
  6. internal enum JobObjectInfoType
  7. {
  8. AssociateCompletionPortInformation = 7,
  9. BasicLimitInformation = 2,
  10. BasicUIRestrictions = 4,
  11. EndOfJobTimeInformation = 6,
  12. ExtendedLimitInformation = 9,
  13. SecurityLimitInformation = 5,
  14. GroupInformation = 11
  15. }
  16. [StructLayout(LayoutKind.Sequential)]
  17. internal struct SecurityAttributes
  18. {
  19. public int nLength;
  20. public IntPtr lpSecurityDescriptor;
  21. public int bInheritHandle;
  22. }
  23. [StructLayout(LayoutKind.Sequential)]
  24. internal struct JobObjectBasicLimitInformation
  25. {
  26. public Int64 PerProcessUserTimeLimit;
  27. public Int64 PerJobUserTimeLimit;
  28. public Int16 LimitFlags;
  29. public UInt32 MinimumWorkingSetSize;
  30. public UInt32 MaximumWorkingSetSize;
  31. public Int16 ActiveProcessLimit;
  32. public Int64 Affinity;
  33. public Int16 PriorityClass;
  34. public Int16 SchedulingClass;
  35. }
  36. [StructLayout(LayoutKind.Sequential)]
  37. internal struct IOCounters
  38. {
  39. public UInt64 ReadOperationCount;
  40. public UInt64 WriteOperationCount;
  41. public UInt64 OtherOperationCount;
  42. public UInt64 ReadTransferCount;
  43. public UInt64 WriteTransferCount;
  44. public UInt64 OtherTransferCount;
  45. }
  46. [StructLayout(LayoutKind.Sequential)]
  47. internal struct JobObjectExtendedLimitInformation
  48. {
  49. public JobObjectBasicLimitInformation BasicLimitInformation;
  50. public IOCounters IoInfo;
  51. public UInt32 ProcessMemoryLimit;
  52. public UInt32 JobMemoryLimit;
  53. public UInt32 PeakProcessMemoryUsed;
  54. public UInt32 PeakJobMemoryUsed;
  55. }
  56. [Flags]
  57. internal enum FileMapProtection : uint
  58. {
  59. PageReadonly = 0x02,
  60. PageReadWrite = 0x04,
  61. PageWriteCopy = 0x08,
  62. PageExecuteRead = 0x20,
  63. PageExecuteReadWrite = 0x40,
  64. SectionCommit = 0x8000000,
  65. SectionImage = 0x1000000,
  66. SectionNoCache = 0x10000000,
  67. SectionReserve = 0x4000000,
  68. }
  69. [Flags]
  70. public enum FileMapAccess : int
  71. {
  72. FileMapCopy = 0x0001,
  73. FileMapWrite = 0x0002,
  74. FileMapRead = 0x0004,
  75. FileMapAllAccess = 0x001f,
  76. FileMapExecute = 0x0020,
  77. }
  78. internal static class UnsafeNativeMethods
  79. {
  80. public const int ERROR_ALREADY_EXISTS = 183;
  81. [DllImport("kernel32", CharSet = CharSet.Unicode, SetLastError = true)]
  82. public static extern SafeFileHandle CreateFileMapping(SafeFileHandle hFile, IntPtr lpAttributes, FileMapProtection fProtect, int dwMaximumSizeHigh, int dwMaximumSizeLow, string lpName);
  83. [DllImport("kernel32", SetLastError = true, ExactSpelling = true)]
  84. public static extern IntPtr MapViewOfFile(SafeFileHandle handle, FileMapAccess dwDesiredAccess, uint dwFileOffsetHigh, uint dwFileOffsetLow, UIntPtr dwNumberOfBytesToMap);
  85. [DllImport("kernel32", ExactSpelling = true)]
  86. [return: MarshalAs(UnmanagedType.Bool)]
  87. public static extern bool UnmapViewOfFile(IntPtr lpBaseAddress);
  88. [DllImport("kernel32", CharSet = CharSet.Auto, SetLastError = true, ExactSpelling = true)]
  89. public static extern int CloseHandle(IntPtr hObject);
  90. [DllImport("kernel32", CharSet = CharSet.Unicode)]
  91. public static extern IntPtr CreateJobObject(IntPtr a, string lpName);
  92. [DllImport("kernel32")]
  93. [return: MarshalAs(UnmanagedType.Bool)]
  94. public static extern bool SetInformationJobObject(IntPtr hJob, JobObjectInfoType infoType, IntPtr lpJobObjectInfo, uint cbJobObjectInfoLength);
  95. [DllImport("kernel32", SetLastError = true)]
  96. [return: MarshalAs(UnmanagedType.Bool)]
  97. public static extern bool AssignProcessToJobObject(IntPtr job, IntPtr process);
  98. }
  99. }