UnsafeNativeMethods.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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 UInt32 nLength;
  20. public IntPtr lpSecurityDescriptor;
  21. public Int32 bInheritHandle;
  22. }
  23. [StructLayout(LayoutKind.Sequential)]
  24. internal struct JobObjectBasicLimitInformation
  25. {
  26. public Int64 PerProcessUserTimeLimit;
  27. public Int64 PerJobUserTimeLimit;
  28. public UInt32 LimitFlags;
  29. public UIntPtr MinimumWorkingSetSize;
  30. public UIntPtr MaximumWorkingSetSize;
  31. public UInt32 ActiveProcessLimit;
  32. public UIntPtr Affinity;
  33. public UInt32 PriorityClass;
  34. public UInt32 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 UIntPtr ProcessMemoryLimit;
  52. public UIntPtr JobMemoryLimit;
  53. public UIntPtr PeakProcessMemoryUsed;
  54. public UIntPtr 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. internal enum FileMapAccess : int
  71. {
  72. FileMapCopy = 0x0001,
  73. FileMapWrite = 0x0002,
  74. FileMapRead = 0x0004,
  75. FileMapAllAccess = 0x001f,
  76. FileMapExecute = 0x0020,
  77. }
  78. [Flags]
  79. internal enum StandardRights
  80. {
  81. Delete = 0x00010000,
  82. ReadPermissions = 0x00020000,
  83. WritePermissions = 0x00040000,
  84. TakeOwnership = 0x00080000,
  85. Synchronize = 0x00100000,
  86. Required = Delete | ReadPermissions | WritePermissions | TakeOwnership,
  87. }
  88. // Isn't this the same as FileMapAccess?
  89. [Flags]
  90. internal enum FileMappingRights
  91. {
  92. FileMapCopy = 1,
  93. FileMapWrite = 2,
  94. FileMapRead = 4,
  95. SectionMapExecute = 8,
  96. SectionExtendSize = 16,
  97. FileMapExecute = 32,
  98. AllAccess = FileMapCopy | FileMapWrite | FileMapRead | SectionMapExecute | SectionExtendSize |
  99. StandardRights.Required
  100. }
  101. [Flags]
  102. internal enum DesktopRights
  103. {
  104. ReadObjects = 1,
  105. CreateWindow = 2,
  106. CreateMenu = 4,
  107. HookControl = 8,
  108. JournalRecord = 16,
  109. JournalPlayback = 32,
  110. Enumerate = 64,
  111. WriteObjects = 128,
  112. SwitchDesktop = 256,
  113. AllAccess = ReadObjects | CreateWindow | CreateMenu | HookControl |
  114. JournalRecord | JournalPlayback | Enumerate | WriteObjects | SwitchDesktop |
  115. StandardRights.Required
  116. }
  117. [Flags]
  118. internal enum WindowStationRights
  119. {
  120. EnumDesktops = 1,
  121. ReadAttributes = 2,
  122. AccessClipboard = 4,
  123. CreateDesktop = 8,
  124. WriteAttributes = 16,
  125. AccessGlobalAtoms = 32,
  126. ExitWindows = 64,
  127. Enumerate = 256,
  128. ReadScreen = 512,
  129. AllAccess = EnumDesktops | ReadAttributes | AccessClipboard | CreateDesktop |
  130. WriteAttributes | AccessGlobalAtoms | ExitWindows | Enumerate | ReadScreen |
  131. StandardRights.Required
  132. }
  133. internal enum RegistryFlags
  134. {
  135. RegSz = 0x02,
  136. SubKeyWow6432Key = 0x00020000,
  137. }
  138. internal enum RegistryType
  139. {
  140. RegNone = 0,
  141. }
  142. internal static class UnsafeNativeMethods
  143. {
  144. public const int ERROR_ALREADY_EXISTS = 183;
  145. [DllImport("kernel32", CharSet = CharSet.Unicode, SetLastError = true)]
  146. public static extern SafeFileHandle CreateFileMapping(SafeFileHandle hFile, IntPtr lpAttributes, FileMapProtection fProtect, int dwMaximumSizeHigh, int dwMaximumSizeLow, string lpName);
  147. [DllImport("kernel32", SetLastError = true, ExactSpelling = true)]
  148. public static extern IntPtr MapViewOfFile(SafeFileHandle handle, FileMapAccess dwDesiredAccess, uint dwFileOffsetHigh, uint dwFileOffsetLow, UIntPtr dwNumberOfBytesToMap);
  149. [DllImport("kernel32", ExactSpelling = true)]
  150. [return: MarshalAs(UnmanagedType.Bool)]
  151. public static extern bool UnmapViewOfFile(IntPtr lpBaseAddress);
  152. [DllImport("kernel32", CharSet = CharSet.Auto, SetLastError = true, ExactSpelling = true)]
  153. public static extern int CloseHandle(IntPtr hObject);
  154. [DllImport("kernel32", CharSet = CharSet.Unicode, SetLastError = true)]
  155. public static extern IntPtr CreateJobObject(IntPtr a, string lpName);
  156. [DllImport("kernel32", SetLastError = true)]
  157. [return: MarshalAs(UnmanagedType.Bool)]
  158. public static extern bool SetInformationJobObject(IntPtr hJob, JobObjectInfoType infoType, IntPtr lpJobObjectInfo, uint cbJobObjectInfoLength);
  159. [DllImport("user32.dll", SetLastError = true)]
  160. public static extern IntPtr GetProcessWindowStation();
  161. [DllImport("user32.dll", SetLastError = true)]
  162. public static extern IntPtr GetThreadDesktop(int dwThreadId);
  163. [DllImport("kernel32.dll", SetLastError = true)]
  164. public static extern int GetCurrentThreadId();
  165. [DllImport("advapi32", CharSet = CharSet.Unicode, SetLastError = true)]
  166. public static extern int RegGetValue(UIntPtr hkey, string lpSubKey, string lpValue, RegistryFlags dwFlags, out RegistryType pdwType, IntPtr pvData, ref uint pcbData);
  167. }
  168. }