1
0

UnsafeNativeMethods.cs 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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. internal static class UnsafeNativeMethods
  57. {
  58. [DllImport("kernel32", CharSet = CharSet.Unicode, SetLastError = true)]
  59. public static extern SafeFileHandle CreateFileMapping(SafeFileHandle hFile, IntPtr lpAttributes, int fProtect, int dwMaximumSizeHigh, int dwMaximumSizeLow, string lpName);
  60. [DllImport("kernel32", SetLastError = true, ExactSpelling = true)]
  61. public static extern IntPtr MapViewOfFile(SafeFileHandle handle, int dwDesiredAccess, uint dwFileOffsetHigh, uint dwFileOffsetLow, UIntPtr dwNumberOfBytesToMap);
  62. [DllImport("kernel32", ExactSpelling = true)]
  63. [return: MarshalAs(UnmanagedType.Bool)]
  64. public static extern bool UnmapViewOfFile(IntPtr lpBaseAddress);
  65. [DllImport("kernel32", CharSet = CharSet.Auto, SetLastError = true, ExactSpelling = true)]
  66. public static extern int CloseHandle(IntPtr hObject);
  67. [DllImport("kernel32", CharSet = CharSet.Unicode)]
  68. public static extern IntPtr CreateJobObject(IntPtr a, string lpName);
  69. [DllImport("kernel32")]
  70. [return: MarshalAs(UnmanagedType.Bool)]
  71. public static extern bool SetInformationJobObject(IntPtr hJob, JobObjectInfoType infoType, IntPtr lpJobObjectInfo, uint cbJobObjectInfoLength);
  72. [DllImport("kernel32", SetLastError = true)]
  73. [return: MarshalAs(UnmanagedType.Bool)]
  74. public static extern bool AssignProcessToJobObject(IntPtr job, IntPtr process);
  75. }
  76. }