UnsafeNativeMethods.cs 60 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238
  1. //------------------------------------------------------------
  2. // Copyright (c) Microsoft Corporation. All rights reserved.
  3. //------------------------------------------------------------
  4. #define WSARECV
  5. namespace System.ServiceModel.Channels
  6. {
  7. using System.Runtime;
  8. using System.Runtime.InteropServices;
  9. using System.Runtime.ConstrainedExecution;
  10. using System.Runtime.Versioning;
  11. using System.Security;
  12. using System.Security.Permissions;
  13. using System.Security.Principal;
  14. using System.Threading;
  15. using Microsoft.Win32.SafeHandles;
  16. using System.ComponentModel;
  17. using System.Text;
  18. using System.Transactions;
  19. using System.ServiceModel.Activation;
  20. using System.ServiceModel.Security;
  21. using System.EnterpriseServices;
  22. using SafeCloseHandle = System.ServiceModel.Activation.SafeCloseHandle;
  23. using TOKEN_INFORMATION_CLASS = System.ServiceModel.Activation.ListenerUnsafeNativeMethods.TOKEN_INFORMATION_CLASS;
  24. [SuppressUnmanagedCodeSecurity]
  25. internal static class UnsafeNativeMethods
  26. {
  27. public const string KERNEL32 = "kernel32.dll";
  28. public const string ADVAPI32 = "advapi32.dll";
  29. public const string BCRYPT = "bcrypt.dll";
  30. public const string MQRT = "mqrt.dll";
  31. public const string SECUR32 = "secur32.dll";
  32. public const string USERENV = "userenv.dll";
  33. #if WSARECV
  34. public const string WS2_32 = "ws2_32.dll";
  35. #endif
  36. //
  37. public const int ERROR_SUCCESS = 0;
  38. public const int ERROR_FILE_NOT_FOUND = 2;
  39. public const int ERROR_ACCESS_DENIED = 5;
  40. public const int ERROR_INVALID_HANDLE = 6;
  41. public const int ERROR_NOT_ENOUGH_MEMORY = 8;
  42. public const int ERROR_OUTOFMEMORY = 14;
  43. public const int ERROR_SHARING_VIOLATION = 32;
  44. public const int ERROR_NETNAME_DELETED = 64;
  45. public const int ERROR_INVALID_PARAMETER = 87;
  46. public const int ERROR_BROKEN_PIPE = 109;
  47. public const int ERROR_ALREADY_EXISTS = 183;
  48. public const int ERROR_PIPE_BUSY = 231;
  49. public const int ERROR_NO_DATA = 232;
  50. public const int ERROR_MORE_DATA = 234;
  51. public const int WAIT_TIMEOUT = 258;
  52. public const int ERROR_PIPE_CONNECTED = 535;
  53. public const int ERROR_OPERATION_ABORTED = 995;
  54. public const int ERROR_IO_PENDING = 997;
  55. public const int ERROR_SERVICE_ALREADY_RUNNING = 1056;
  56. public const int ERROR_SERVICE_DISABLED = 1058;
  57. public const int ERROR_NO_TRACKING_SERVICE = 1172;
  58. public const int ERROR_ALLOTTED_SPACE_EXCEEDED = 1344;
  59. public const int ERROR_NO_SYSTEM_RESOURCES = 1450;
  60. // When querying for the token length
  61. const int ERROR_INSUFFICIENT_BUFFER = 122;
  62. public const int STATUS_PENDING = 0x103;
  63. // socket errors
  64. public const int WSAACCESS = 10013;
  65. public const int WSAEMFILE = 10024;
  66. public const int WSAEMSGSIZE = 10040;
  67. public const int WSAEADDRINUSE = 10048;
  68. public const int WSAEADDRNOTAVAIL = 10049;
  69. public const int WSAENETDOWN = 10050;
  70. public const int WSAENETUNREACH = 10051;
  71. public const int WSAENETRESET = 10052;
  72. public const int WSAECONNABORTED = 10053;
  73. public const int WSAECONNRESET = 10054;
  74. public const int WSAENOBUFS = 10055;
  75. public const int WSAESHUTDOWN = 10058;
  76. public const int WSAETIMEDOUT = 10060;
  77. public const int WSAECONNREFUSED = 10061;
  78. public const int WSAEHOSTDOWN = 10064;
  79. public const int WSAEHOSTUNREACH = 10065;
  80. public const int DUPLICATE_CLOSE_SOURCE = 0x00000001;
  81. public const int DUPLICATE_SAME_ACCESS = 0x00000002;
  82. public const int FILE_FLAG_OVERLAPPED = 0x40000000;
  83. public const int FILE_FLAG_FIRST_PIPE_INSTANCE = 0x00080000;
  84. public const int GENERIC_ALL = 0x10000000;
  85. public const int GENERIC_READ = unchecked((int)0x80000000);
  86. public const int GENERIC_WRITE = 0x40000000;
  87. public const int FILE_CREATE_PIPE_INSTANCE = 0x00000004;
  88. public const int FILE_WRITE_ATTRIBUTES = 0x00000100;
  89. public const int FILE_WRITE_DATA = 0x00000002;
  90. public const int FILE_WRITE_EA = 0x00000010;
  91. public const int OPEN_EXISTING = 3;
  92. public const int PIPE_ACCESS_DUPLEX = 3;
  93. public const int PIPE_UNLIMITED_INSTANCES = 255;
  94. public const int PIPE_TYPE_BYTE = 0;
  95. public const int PIPE_TYPE_MESSAGE = 4;
  96. public const int PIPE_READMODE_BYTE = 0;
  97. public const int PIPE_READMODE_MESSAGE = 2;
  98. // VirtualAlloc constants
  99. public const uint MEM_COMMIT = 0x1000;
  100. public const uint MEM_DECOMMIT = 0x4000;
  101. public const int PAGE_READWRITE = 4;
  102. public const int FILE_MAP_WRITE = 2;
  103. public const int FILE_MAP_READ = 4;
  104. public const int SDDL_REVISION_1 = 1;
  105. public const int SECURITY_ANONYMOUS = 0x00000000;
  106. public const int SECURITY_QOS_PRESENT = 0x00100000;
  107. public const int SECURITY_IDENTIFICATION = 0x00010000;
  108. public const int FORMAT_MESSAGE_ALLOCATE_BUFFER = 0x00000100;
  109. public const int FORMAT_MESSAGE_IGNORE_INSERTS = 0x00000200;
  110. public const int FORMAT_MESSAGE_FROM_STRING = 0x00000400;
  111. public const int FORMAT_MESSAGE_FROM_SYSTEM = 0x00001000;
  112. public const int FORMAT_MESSAGE_ARGUMENT_ARRAY = 0x00002000;
  113. public const int FORMAT_MESSAGE_FROM_HMODULE = 0x00000800;
  114. public const int MQ_RECEIVE_ACCESS = 0x00000001;
  115. public const int MQ_SEND_ACCESS = 0x00000002;
  116. public const int MQ_MOVE_ACCESS = 0x00000004;
  117. public const int MQ_DENY_NONE = 0x00000000;
  118. public const int MQ_DENY_RECEIVE_SHARE = 0x00000001;
  119. public const int MQ_ACTION_RECEIVE = 0x00000000;
  120. public const int MQ_ACTION_PEEK_CURRENT = unchecked((int)0x80000000);
  121. public const int MQ_ACTION_PEEK_NEXT = unchecked((int)0x80000001);
  122. public const int MQ_LOOKUP_RECEIVE_CURRENT = unchecked((int)0x40000020);
  123. public const int MQ_LOOKUP_PEEK_CURRENT = unchecked((int)0x40000010);
  124. public const int MQ_NO_TRANSACTION = 0;
  125. public const int MQ_MTS_TRANSACTION = 1;
  126. public const int MQ_SINGLE_MESSAGE = 3;
  127. public const int MQ_INFORMATION_PROPERTY = unchecked((int)0x400E0001);
  128. public const int MQ_INFORMATION_ILLEGAL_PROPERTY = unchecked((int)0x400E0002);
  129. public const int MQ_INFORMATION_PROPERTY_IGNORED = unchecked((int)0x400E0003);
  130. public const int MQ_INFORMATION_UNSUPPORTED_PROPERTY = unchecked((int)0x400E0004);
  131. public const int MQ_INFORMATION_DUPLICATE_PROPERTY = unchecked((int)0x400E0005);
  132. public const int MQ_INFORMATION_OPERATION_PENDING = unchecked((int)0x400E0006);
  133. public const int MQ_INFORMATION_FORMATNAME_BUFFER_TOO_SMALL = unchecked((int)0x400E0009);
  134. public const int MQ_INFORMATION_INTERNAL_USER_CERT_EXIST = unchecked((int)0x400E000A);
  135. public const int MQ_INFORMATION_OWNER_IGNORED = unchecked((int)0x400E000B);
  136. public const int MQ_ERROR = unchecked((int)0xC00E0001);
  137. public const int MQ_ERROR_PROPERTY = unchecked((int)0xC00E0002);
  138. public const int MQ_ERROR_QUEUE_NOT_FOUND = unchecked((int)0xC00E0003);
  139. public const int MQ_ERROR_QUEUE_NOT_ACTIVE = unchecked((int)0xC00E0004);
  140. public const int MQ_ERROR_QUEUE_EXISTS = unchecked((int)0xC00E0005);
  141. public const int MQ_ERROR_INVALID_PARAMETER = unchecked((int)0xC00E0006);
  142. public const int MQ_ERROR_INVALID_HANDLE = unchecked((int)0xC00E0007);
  143. public const int MQ_ERROR_OPERATION_CANCELLED = unchecked((int)0xC00E0008);
  144. public const int MQ_ERROR_SHARING_VIOLATION = unchecked((int)0xC00E0009);
  145. public const int MQ_ERROR_SERVICE_NOT_AVAILABLE = unchecked((int)0xC00E000B);
  146. public const int MQ_ERROR_MACHINE_NOT_FOUND = unchecked((int)0xC00E000D);
  147. public const int MQ_ERROR_ILLEGAL_SORT = unchecked((int)0xC00E0010);
  148. public const int MQ_ERROR_ILLEGAL_USER = unchecked((int)0xC00E0011);
  149. public const int MQ_ERROR_NO_DS = unchecked((int)0xC00E0013);
  150. public const int MQ_ERROR_ILLEGAL_QUEUE_PATHNAME = unchecked((int)0xC00E0014);
  151. public const int MQ_ERROR_ILLEGAL_PROPERTY_VALUE = unchecked((int)0xC00E0018);
  152. public const int MQ_ERROR_ILLEGAL_PROPERTY_VT = unchecked((int)0xC00E0019);
  153. public const int MQ_ERROR_BUFFER_OVERFLOW = unchecked((int)0xC00E001A);
  154. public const int MQ_ERROR_IO_TIMEOUT = unchecked((int)0xC00E001B);
  155. public const int MQ_ERROR_ILLEGAL_CURSOR_ACTION = unchecked((int)0xC00E001C);
  156. public const int MQ_ERROR_MESSAGE_ALREADY_RECEIVED = unchecked((int)0xC00E001D);
  157. public const int MQ_ERROR_ILLEGAL_FORMATNAME = unchecked((int)0xC00E001E);
  158. public const int MQ_ERROR_FORMATNAME_BUFFER_TOO_SMALL = unchecked((int)0xC00E001F);
  159. public const int MQ_ERROR_UNSUPPORTED_FORMATNAME_OPERATION = unchecked((int)0xC00E0020);
  160. public const int MQ_ERROR_ILLEGAL_SECURITY_DESCRIPTOR = unchecked((int)0xC00E0021);
  161. public const int MQ_ERROR_SENDERID_BUFFER_TOO_SMALL = unchecked((int)0xC00E0022);
  162. public const int MQ_ERROR_SECURITY_DESCRIPTOR_TOO_SMALL = unchecked((int)0xC00E0023);
  163. public const int MQ_ERROR_CANNOT_IMPERSONATE_CLIENT = unchecked((int)0xC00E0024);
  164. public const int MQ_ERROR_ACCESS_DENIED = unchecked((int)0xC00E0025);
  165. public const int MQ_ERROR_PRIVILEGE_NOT_HELD = unchecked((int)0xC00E0026);
  166. public const int MQ_ERROR_INSUFFICIENT_RESOURCES = unchecked((int)0xC00E0027);
  167. public const int MQ_ERROR_USER_BUFFER_TOO_SMALL = unchecked((int)0xC00E0028);
  168. public const int MQ_ERROR_MESSAGE_STORAGE_FAILED = unchecked((int)0xC00E002A);
  169. public const int MQ_ERROR_SENDER_CERT_BUFFER_TOO_SMALL = unchecked((int)0xC00E002B);
  170. public const int MQ_ERROR_INVALID_CERTIFICATE = unchecked((int)0xC00E002C);
  171. public const int MQ_ERROR_CORRUPTED_INTERNAL_CERTIFICATE = unchecked((int)0xC00E002D);
  172. public const int MQ_ERROR_INTERNAL_USER_CERT_EXIST = unchecked((int)0xC00E002E);
  173. public const int MQ_ERROR_NO_INTERNAL_USER_CERT = unchecked((int)0xC00E002F);
  174. public const int MQ_ERROR_CORRUPTED_SECURITY_DATA = unchecked((int)0xC00E0030);
  175. public const int MQ_ERROR_CORRUPTED_PERSONAL_CERT_STORE = unchecked((int)0xC00E0031);
  176. public const int MQ_ERROR_COMPUTER_DOES_NOT_SUPPORT_ENCRYPTION = unchecked((int)0xC00E0033);
  177. public const int MQ_ERROR_BAD_SECURITY_CONTEXT = unchecked((int)0xC00E0035);
  178. public const int MQ_ERROR_COULD_NOT_GET_USER_SID = unchecked((int)0xC00E0036);
  179. public const int MQ_ERROR_COULD_NOT_GET_ACCOUNT_INFO = unchecked((int)0xC00E0037);
  180. public const int MQ_ERROR_ILLEGAL_MQCOLUMNS = unchecked((int)0xC00E0038);
  181. public const int MQ_ERROR_ILLEGAL_PROPID = unchecked((int)0xC00E0039);
  182. public const int MQ_ERROR_ILLEGAL_RELATION = unchecked((int)0xC00E003A);
  183. public const int MQ_ERROR_ILLEGAL_PROPERTY_SIZE = unchecked((int)0xC00E003B);
  184. public const int MQ_ERROR_ILLEGAL_RESTRICTION_PROPID = unchecked((int)0xC00E003C);
  185. public const int MQ_ERROR_ILLEGAL_MQQUEUEPROPS = unchecked((int)0xC00E003D);
  186. public const int MQ_ERROR_PROPERTY_NOTALLOWED = unchecked((int)0xC00E003E);
  187. public const int MQ_ERROR_INSUFFICIENT_PROPERTIES = unchecked((int)0xC00E003F);
  188. public const int MQ_ERROR_MACHINE_EXISTS = unchecked((int)0xC00E0040);
  189. public const int MQ_ERROR_ILLEGAL_MQQMPROPS = unchecked((int)0xC00E0041);
  190. public const int MQ_ERROR_DS_IS_FULL = unchecked((int)0xC00E0042);
  191. public const int MQ_ERROR_DS_ERROR = unchecked((int)0xC00E0043);
  192. public const int MQ_ERROR_INVALID_OWNER = unchecked((int)0xC00E0044);
  193. public const int MQ_ERROR_UNSUPPORTED_ACCESS_MODE = unchecked((int)0xC00E0045);
  194. public const int MQ_ERROR_RESULT_BUFFER_TOO_SMALL = unchecked((int)0xC00E0046);
  195. public const int MQ_ERROR_DELETE_CN_IN_USE = unchecked((int)0xC00E0048);
  196. public const int MQ_ERROR_NO_RESPONSE_FROM_OBJECT_SERVER = unchecked((int)0xC00E0049);
  197. public const int MQ_ERROR_OBJECT_SERVER_NOT_AVAILABLE = unchecked((int)0xC00E004A);
  198. public const int MQ_ERROR_QUEUE_NOT_AVAILABLE = unchecked((int)0xC00E004B);
  199. public const int MQ_ERROR_DTC_CONNECT = unchecked((int)0xC00E004C);
  200. public const int MQ_ERROR_TRANSACTION_IMPORT = unchecked((int)0xC00E004E);
  201. public const int MQ_ERROR_TRANSACTION_USAGE = unchecked((int)0xC00E0050);
  202. public const int MQ_ERROR_TRANSACTION_SEQUENCE = unchecked((int)0xC00E0051);
  203. public const int MQ_ERROR_MISSING_CONNECTOR_TYPE = unchecked((int)0xC00E0055);
  204. public const int MQ_ERROR_STALE_HANDLE = unchecked((int)0xC00E0056);
  205. public const int MQ_ERROR_TRANSACTION_ENLIST = unchecked((int)0xC00E0058);
  206. public const int MQ_ERROR_QUEUE_DELETED = unchecked((int)0xC00E005A);
  207. public const int MQ_ERROR_ILLEGAL_CONTEXT = unchecked((int)0xC00E005B);
  208. public const int MQ_ERROR_ILLEGAL_SORT_PROPID = unchecked((int)0xC00E005C);
  209. public const int MQ_ERROR_LABEL_TOO_LONG = unchecked((int)0xC00E005D);
  210. public const int MQ_ERROR_LABEL_BUFFER_TOO_SMALL = unchecked((int)0xC00E005E);
  211. public const int MQ_ERROR_MQIS_SERVER_EMPTY = unchecked((int)0xC00E005F);
  212. public const int MQ_ERROR_MQIS_READONLY_MODE = unchecked((int)0xC00E0060);
  213. public const int MQ_ERROR_SYMM_KEY_BUFFER_TOO_SMALL = unchecked((int)0xC00E0061);
  214. public const int MQ_ERROR_SIGNATURE_BUFFER_TOO_SMALL = unchecked((int)0xC00E0062);
  215. public const int MQ_ERROR_PROV_NAME_BUFFER_TOO_SMALL = unchecked((int)0xC00E0063);
  216. public const int MQ_ERROR_ILLEGAL_OPERATION = unchecked((int)0xC00E0064);
  217. public const int MQ_ERROR_WRITE_NOT_ALLOWED = unchecked((int)0xC00E0065);
  218. public const int MQ_ERROR_WKS_CANT_SERVE_CLIENT = unchecked((int)0xC00E0066);
  219. public const int MQ_ERROR_DEPEND_WKS_LICENSE_OVERFLOW = unchecked((int)0xC00E0067);
  220. public const int MQ_ERROR_REMOTE_MACHINE_NOT_AVAILABLE = unchecked((int)0xC00E0069);
  221. public const int MQ_ERROR_UNSUPPORTED_OPERATION = unchecked((int)0xC00E006A);
  222. public const int MQ_ERROR_ENCRYPTION_PROVIDER_NOT_SUPPORTED = unchecked((int)0xC00E006B);
  223. public const int MQ_ERROR_CANNOT_SET_CRYPTO_SEC_DESCR = unchecked((int)0xC00E006C);
  224. public const int MQ_ERROR_CERTIFICATE_NOT_PROVIDED = unchecked((int)0xC00E006D);
  225. public const int MQ_ERROR_Q_DNS_PROPERTY_NOT_SUPPORTED = unchecked((int)0xC00E006E);
  226. public const int MQ_ERROR_CANNOT_CREATE_CERT_STORE = unchecked((int)0xC00E006F);
  227. public const int MQ_ERROR_CANNOT_OPEN_CERT_STORE = unchecked((int)0xC00E0070);
  228. public const int MQ_ERROR_ILLEGAL_ENTERPRISE_OPERATION = unchecked((int)0xC00E0071);
  229. public const int MQ_ERROR_CANNOT_GRANT_ADD_GUID = unchecked((int)0xC00E0072);
  230. public const int MQ_ERROR_CANNOT_LOAD_MSMQOCM = unchecked((int)0xC00E0073);
  231. public const int MQ_ERROR_NO_ENTRY_POINT_MSMQOCM = unchecked((int)0xC00E0074);
  232. public const int MQ_ERROR_NO_MSMQ_SERVERS_ON_DC = unchecked((int)0xC00E0075);
  233. public const int MQ_ERROR_CANNOT_JOIN_DOMAIN = unchecked((int)0xC00E0076);
  234. public const int MQ_ERROR_CANNOT_CREATE_ON_GC = unchecked((int)0xC00E0077);
  235. public const int MQ_ERROR_GUID_NOT_MATCHING = unchecked((int)0xC00E0078);
  236. public const int MQ_ERROR_PUBLIC_KEY_NOT_FOUND = unchecked((int)0xC00E0079);
  237. public const int MQ_ERROR_PUBLIC_KEY_DOES_NOT_EXIST = unchecked((int)0xC00E007A);
  238. public const int MQ_ERROR_ILLEGAL_MQPRIVATEPROPS = unchecked((int)0xC00E007B);
  239. public const int MQ_ERROR_NO_GC_IN_DOMAIN = unchecked((int)0xC00E007C);
  240. public const int MQ_ERROR_NO_MSMQ_SERVERS_ON_GC = unchecked((int)0xC00E007D);
  241. public const int MQ_ERROR_CANNOT_GET_DN = unchecked((int)0xC00E007E);
  242. public const int MQ_ERROR_CANNOT_HASH_DATA_EX = unchecked((int)0xC00E007F);
  243. public const int MQ_ERROR_CANNOT_SIGN_DATA_EX = unchecked((int)0xC00E0080);
  244. public const int MQ_ERROR_CANNOT_CREATE_HASH_EX = unchecked((int)0xC00E0081);
  245. public const int MQ_ERROR_FAIL_VERIFY_SIGNATURE_EX = unchecked((int)0xC00E0082);
  246. public const int MQ_ERROR_CANNOT_DELETE_PSC_OBJECTS = unchecked((int)0xC00E0083);
  247. public const int MQ_ERROR_NO_MQUSER_OU = unchecked((int)0xC00E0084);
  248. public const int MQ_ERROR_CANNOT_LOAD_MQAD = unchecked((int)0xC00E0085);
  249. public const int MQ_ERROR_CANNOT_LOAD_MQDSSRV = unchecked((int)0xC00E0086);
  250. public const int MQ_ERROR_PROPERTIES_CONFLICT = unchecked((int)0xC00E0087);
  251. public const int MQ_ERROR_MESSAGE_NOT_FOUND = unchecked((int)0xC00E0088);
  252. public const int MQ_ERROR_CANT_RESOLVE_SITES = unchecked((int)0xC00E0089);
  253. public const int MQ_ERROR_NOT_SUPPORTED_BY_DEPENDENT_CLIENTS = unchecked((int)0xC00E008A);
  254. public const int MQ_ERROR_OPERATION_NOT_SUPPORTED_BY_REMOTE_COMPUTER = unchecked((int)0xC00E008B);
  255. public const int MQ_ERROR_NOT_A_CORRECT_OBJECT_CLASS = unchecked((int)0xC00E008C);
  256. public const int MQ_ERROR_MULTI_SORT_KEYS = unchecked((int)0xC00E008D);
  257. public const int MQ_ERROR_GC_NEEDED = unchecked((int)0xC00E008E);
  258. public const int MQ_ERROR_DS_BIND_ROOT_FOREST = unchecked((int)0xC00E008F);
  259. public const int MQ_ERROR_DS_LOCAL_USER = unchecked((int)0xC00E0090);
  260. public const int MQ_ERROR_Q_ADS_PROPERTY_NOT_SUPPORTED = unchecked((int)0xC00E0091);
  261. public const int MQ_ERROR_BAD_XML_FORMAT = unchecked((int)0xC00E0092);
  262. public const int MQ_ERROR_UNSUPPORTED_CLASS = unchecked((int)0xC00E0093);
  263. public const int MQ_ERROR_UNINITIALIZED_OBJECT = unchecked((int)0xC00E0094);
  264. public const int MQ_ERROR_CANNOT_CREATE_PSC_OBJECTS = unchecked((int)0xC00E0095);
  265. public const int MQ_ERROR_CANNOT_UPDATE_PSC_OBJECTS = unchecked((int)0xC00E0096);
  266. public const int MQ_ERROR_MESSAGE_LOCKED_UNDER_TRANSACTION = unchecked((int)0xC00E009C);
  267. public const int MQMSG_DELIVERY_EXPRESS = 0;
  268. public const int MQMSG_DELIVERY_RECOVERABLE = 1;
  269. public const int PROPID_M_MSGID_SIZE = 20;
  270. public const int PROPID_M_CORRELATIONID_SIZE = 20;
  271. public const int MQ_MAX_MSG_LABEL_LEN = 250;
  272. public const int MQMSG_JOURNAL_NONE = 0;
  273. public const int MQMSG_DEADLETTER = 1;
  274. public const int MQMSG_JOURNAL = 2;
  275. public const int MQMSG_ACKNOWLEDGMENT_NONE = 0x00;
  276. public const int MQMSG_ACKNOWLEDGMENT_POS_ARRIVAL = 0x01;
  277. public const int MQMSG_ACKNOWLEDGMENT_POS_RECEIVE = 0x02;
  278. public const int MQMSG_ACKNOWLEDGMENT_NEG_ARRIVAL = 0x04;
  279. public const int MQMSG_ACKNOWLEDGMENT_NEG_RECEIVE = 0x08;
  280. public const int MQMSG_CLASS_NORMAL = 0x0;
  281. public const int MQMSG_CLASS_REPORT = 0x1;
  282. public const int MQMSG_SENDERID_TYPE_NONE = 0;
  283. public const int MQMSG_SENDERID_TYPE_SID = 1;
  284. public const int MQMSG_AUTH_LEVEL_NONE = 0;
  285. public const int MQMSG_AUTH_LEVEL_ALWAYS = 1;
  286. public const int MQMSG_PRIV_LEVEL_NONE = 0;
  287. public const int MQMSG_PRIV_LEVEL_BODY_BASE = 0x01;
  288. public const int MQMSG_PRIV_LEVEL_BODY_ENHANCED = 0x03;
  289. public const int MQMSG_TRACE_NONE = 0;
  290. public const int MQMSG_SEND_ROUTE_TO_REPORT_QUEUE = 1;
  291. public const int PROPID_M_BASE = 0;
  292. public const int PROPID_M_CLASS = (PROPID_M_BASE + 1); /* VT_UI2 */
  293. public const int PROPID_M_MSGID = (PROPID_M_BASE + 2); /* VT_UI1|VT_VECTOR */
  294. public const int PROPID_M_CORRELATIONID = (PROPID_M_BASE + 3); /* VT_UI1|VT_VECTOR */
  295. public const int PROPID_M_PRIORITY = (PROPID_M_BASE + 4); /* VT_UI1 */
  296. public const int PROPID_M_DELIVERY = (PROPID_M_BASE + 5); /* VT_UI1 */
  297. public const int PROPID_M_ACKNOWLEDGE = (PROPID_M_BASE + 6); /* VT_UI1 */
  298. public const int PROPID_M_JOURNAL = (PROPID_M_BASE + 7); /* VT_UI1 */
  299. public const int PROPID_M_APPSPECIFIC = (PROPID_M_BASE + 8); /* VT_UI4 */
  300. public const int PROPID_M_BODY = (PROPID_M_BASE + 9); /* VT_UI1|VT_VECTOR */
  301. public const int PROPID_M_BODY_SIZE = (PROPID_M_BASE + 10); /* VT_UI4 */
  302. public const int PROPID_M_LABEL = (PROPID_M_BASE + 11); /* VT_LPWSTR */
  303. public const int PROPID_M_LABEL_LEN = (PROPID_M_BASE + 12); /* VT_UI4 */
  304. public const int PROPID_M_TIME_TO_REACH_QUEUE = (PROPID_M_BASE + 13); /* VT_UI4 */
  305. public const int PROPID_M_TIME_TO_BE_RECEIVED = (PROPID_M_BASE + 14); /* VT_UI4 */
  306. public const int PROPID_M_RESP_QUEUE = (PROPID_M_BASE + 15); /* VT_LPWSTR */
  307. public const int PROPID_M_RESP_QUEUE_LEN = (PROPID_M_BASE + 16); /* VT_UI4 */
  308. public const int PROPID_M_ADMIN_QUEUE = (PROPID_M_BASE + 17); /* VT_LPWSTR */
  309. public const int PROPID_M_ADMIN_QUEUE_LEN = (PROPID_M_BASE + 18); /* VT_UI4 */
  310. public const int PROPID_M_VERSION = (PROPID_M_BASE + 19); /* VT_UI4 */
  311. public const int PROPID_M_SENDERID = (PROPID_M_BASE + 20); /* VT_UI1|VT_VECTOR */
  312. public const int PROPID_M_SENDERID_LEN = (PROPID_M_BASE + 21); /* VT_UI4 */
  313. public const int PROPID_M_SENDERID_TYPE = (PROPID_M_BASE + 22); /* VT_UI4 */
  314. public const int PROPID_M_PRIV_LEVEL = (PROPID_M_BASE + 23); /* VT_UI4 */
  315. public const int PROPID_M_AUTH_LEVEL = (PROPID_M_BASE + 24); /* VT_UI4 */
  316. public const int PROPID_M_AUTHENTICATED = (PROPID_M_BASE + 25); /* VT_UI1 */
  317. public const int PROPID_M_HASH_ALG = (PROPID_M_BASE + 26); /* VT_UI4 */
  318. public const int PROPID_M_ENCRYPTION_ALG = (PROPID_M_BASE + 27); /* VT_UI4 */
  319. public const int PROPID_M_SENDER_CERT = (PROPID_M_BASE + 28); /* VT_UI1|VT_VECTOR */
  320. public const int PROPID_M_SENDER_CERT_LEN = (PROPID_M_BASE + 29); /* VT_UI4 */
  321. public const int PROPID_M_SRC_MACHINE_ID = (PROPID_M_BASE + 30); /* VT_CLSID */
  322. public const int PROPID_M_SENTTIME = (PROPID_M_BASE + 31); /* VT_UI4 */
  323. public const int PROPID_M_ARRIVEDTIME = (PROPID_M_BASE + 32); /* VT_UI4 */
  324. public const int PROPID_M_DEST_QUEUE = (PROPID_M_BASE + 33); /* VT_LPWSTR */
  325. public const int PROPID_M_DEST_QUEUE_LEN = (PROPID_M_BASE + 34); /* VT_UI4 */
  326. public const int PROPID_M_EXTENSION = (PROPID_M_BASE + 35); /* VT_UI1|VT_VECTOR */
  327. public const int PROPID_M_EXTENSION_LEN = (PROPID_M_BASE + 36); /* VT_UI4 */
  328. public const int PROPID_M_SECURITY_CONTEXT = (PROPID_M_BASE + 37); /* VT_UI4 */
  329. public const int PROPID_M_CONNECTOR_TYPE = (PROPID_M_BASE + 38); /* VT_CLSID */
  330. public const int PROPID_M_XACT_STATUS_QUEUE = (PROPID_M_BASE + 39); /* VT_LPWSTR */
  331. public const int PROPID_M_XACT_STATUS_QUEUE_LEN = (PROPID_M_BASE + 40); /* VT_UI4 */
  332. public const int PROPID_M_TRACE = (PROPID_M_BASE + 41); /* VT_UI1 */
  333. public const int PROPID_M_BODY_TYPE = (PROPID_M_BASE + 42); /* VT_UI4 */
  334. public const int PROPID_M_DEST_SYMM_KEY = (PROPID_M_BASE + 43); /* VT_UI1|VT_VECTOR */
  335. public const int PROPID_M_DEST_SYMM_KEY_LEN = (PROPID_M_BASE + 44); /* VT_UI4 */
  336. public const int PROPID_M_SIGNATURE = (PROPID_M_BASE + 45); /* VT_UI1|VT_VECTOR */
  337. public const int PROPID_M_SIGNATURE_LEN = (PROPID_M_BASE + 46); /* VT_UI4 */
  338. public const int PROPID_M_PROV_TYPE = (PROPID_M_BASE + 47); /* VT_UI4 */
  339. public const int PROPID_M_PROV_NAME = (PROPID_M_BASE + 48); /* VT_LPWSTR */
  340. public const int PROPID_M_PROV_NAME_LEN = (PROPID_M_BASE + 49); /* VT_UI4 */
  341. public const int PROPID_M_FIRST_IN_XACT = (PROPID_M_BASE + 50); /* VT_UI1 */
  342. public const int PROPID_M_LAST_IN_XACT = (PROPID_M_BASE + 51); /* VT_UI1 */
  343. public const int PROPID_M_XACTID = (PROPID_M_BASE + 52); /* VT_UI1|VT_VECTOR */
  344. public const int PROPID_M_AUTHENTICATED_EX = (PROPID_M_BASE + 53); /* VT_UI1 */
  345. public const int PROPID_M_RESP_FORMAT_NAME = (PROPID_M_BASE + 54); /* VT_LPWSTR */
  346. public const int PROPID_M_RESP_FORMAT_NAME_LEN = (PROPID_M_BASE + 55); /* VT_UI4 */
  347. public const int PROPID_M_DEST_FORMAT_NAME = (PROPID_M_BASE + 58); /* VT_LPWSTR */
  348. public const int PROPID_M_DEST_FORMAT_NAME_LEN = (PROPID_M_BASE + 59); /* VT_UI4 */
  349. public const int PROPID_M_LOOKUPID = (PROPID_M_BASE + 60); /* VT_UI8 */
  350. public const int PROPID_M_SOAP_ENVELOPE = (PROPID_M_BASE + 61); /* VT_LPWSTR */
  351. public const int PROPID_M_SOAP_ENVELOPE_LEN = (PROPID_M_BASE + 62); /* VT_UI4 */
  352. public const int PROPID_M_COMPOUND_MESSAGE = (PROPID_M_BASE + 63); /* VT_UI1|VT_VECTOR */
  353. public const int PROPID_M_COMPOUND_MESSAGE_SIZE = (PROPID_M_BASE + 64); /* VT_UI4 */
  354. public const int PROPID_M_SOAP_HEADER = (PROPID_M_BASE + 65); /* VT_LPWSTR */
  355. public const int PROPID_M_SOAP_BODY = (PROPID_M_BASE + 66); /* VT_LPWSTR */
  356. public const int PROPID_M_DEADLETTER_QUEUE = (PROPID_M_BASE + 67); /* VT_LPWSTR */
  357. public const int PROPID_M_DEADLETTER_QUEUE_LEN = (PROPID_M_BASE + 68); /* VT_UI4 */
  358. public const int PROPID_M_ABORT_COUNT = (PROPID_M_BASE + 69); /* VT_UI4 */
  359. public const int PROPID_M_MOVE_COUNT = (PROPID_M_BASE + 70); /* VT_UI4 */
  360. public const int PROPID_M_GROUP_ID = (PROPID_M_BASE + 71); /* VT_LPWSTR */
  361. public const int PROPID_M_GROUP_ID_LEN = (PROPID_M_BASE + 72); /* VT_UI4 */
  362. public const int PROPID_M_FIRST_IN_GROUP = (PROPID_M_BASE + 73); /* VT_UI1 */
  363. public const int PROPID_M_LAST_IN_GROUP = (PROPID_M_BASE + 74); /* VT_UI1 */
  364. public const int PROPID_M_LAST_MOVE_TIME = (PROPID_M_BASE + 75); /* VT_UI4 */
  365. public const int PROPID_Q_BASE = 100;
  366. public const int PROPID_Q_INSTANCE = (PROPID_Q_BASE + 1); /* VT_CLSID */
  367. public const int PROPID_Q_TYPE = (PROPID_Q_BASE + 2); /* VT_CLSID */
  368. public const int PROPID_Q_PATHNAME = (PROPID_Q_BASE + 3); /* VT_LPWSTR */
  369. public const int PROPID_Q_JOURNAL = (PROPID_Q_BASE + 4); /* VT_UI1 */
  370. public const int PROPID_Q_QUOTA = (PROPID_Q_BASE + 5); /* VT_UI4 */
  371. public const int PROPID_Q_BASEPRIORITY = (PROPID_Q_BASE + 6); /* VT_I2 */
  372. public const int PROPID_Q_JOURNAL_QUOTA = (PROPID_Q_BASE + 7); /* VT_UI4 */
  373. public const int PROPID_Q_LABEL = (PROPID_Q_BASE + 8); /* VT_LPWSTR */
  374. public const int PROPID_Q_CREATE_TIME = (PROPID_Q_BASE + 9); /* VT_I4 */
  375. public const int PROPID_Q_MODIFY_TIME = (PROPID_Q_BASE + 10); /* VT_I4 */
  376. public const int PROPID_Q_AUTHENTICATE = (PROPID_Q_BASE + 11); /* VT_UI1 */
  377. public const int PROPID_Q_PRIV_LEVEL = (PROPID_Q_BASE + 12); /* VT_UI4 */
  378. public const int PROPID_Q_TRANSACTION = (PROPID_Q_BASE + 13); /* VT_UI1 */
  379. public const int PROPID_Q_PATHNAME_DNS = (PROPID_Q_BASE + 24); /* VT_LPWSTR */
  380. public const int PROPID_Q_MULTICAST_ADDRESS = (PROPID_Q_BASE + 25); /* VT_LPWSTR */
  381. public const int PROPID_Q_ADS_PATH = (PROPID_Q_BASE + 26); /* VT_LPWSTR */
  382. public const int PROPID_PC_BASE = 5800;
  383. public const int PROPID_PC_VERSION = (PROPID_PC_BASE + 1); /* VT_UI4 */
  384. public const int PROPID_PC_DS_ENABLED = (PROPID_PC_BASE + 2); /* VT_BOOL */
  385. public const int PROPID_MGMT_QUEUE_BASE = 0;
  386. public const int PROPID_MGMT_QUEUE_SUBQUEUE_NAMES = (PROPID_MGMT_QUEUE_BASE + 27); /* VT_LPWSTR|VT_VECTOR */
  387. public const int MQ_TRANSACTIONAL_NONE = 0;
  388. public const int MQ_TRANSACTIONAL = 1;
  389. public const int ALG_CLASS_HASH = (4 << 13);
  390. public const int ALG_CLASS_DATA_ENCRYPT = (3 << 13);
  391. public const int ALG_TYPE_ANY = 0;
  392. public const int ALG_TYPE_STREAM = (4 << 9);
  393. public const int ALG_TYPE_BLOCK = (3 << 9);
  394. public const int ALG_SID_MD5 = 3;
  395. public const int ALG_SID_SHA1 = 4;
  396. public const int ALG_SID_SHA_256 = 12;
  397. public const int ALG_SID_SHA_512 = 14;
  398. public const int ALG_SID_RC4 = 1;
  399. public const int ALG_SID_AES = 17;
  400. public const int CALG_MD5 = ALG_CLASS_HASH | ALG_TYPE_ANY | ALG_SID_MD5;
  401. public const int CALG_SHA1 = ALG_CLASS_HASH | ALG_TYPE_ANY | ALG_SID_SHA1;
  402. public const int CALG_SHA_256 = ALG_CLASS_HASH | ALG_TYPE_ANY | ALG_SID_SHA_256;
  403. public const int CALG_SHA_512 = ALG_CLASS_HASH | ALG_TYPE_ANY | ALG_SID_SHA_512;
  404. public const int CALG_RC4 = ALG_CLASS_DATA_ENCRYPT | ALG_TYPE_STREAM | ALG_SID_RC4;
  405. public const int CALG_AES = ALG_CLASS_DATA_ENCRYPT | ALG_TYPE_BLOCK | ALG_SID_AES;
  406. public const int PROV_RSA_AES = 24;
  407. public const string MS_ENH_RSA_AES_PROV = "Microsoft Enhanced RSA and AES Cryptographic Provider";
  408. public const ushort VT_NULL = 1;
  409. public const ushort VT_BOOL = 11;
  410. public const ushort VT_UI1 = 17;
  411. public const ushort VT_UI2 = 18;
  412. public const ushort VT_UI4 = 19;
  413. public const ushort VT_UI8 = 21;
  414. public const ushort VT_LPWSTR = 31;
  415. public const ushort VT_VECTOR = 0x1000;
  416. public const uint MAX_PATH = 260;
  417. [StructLayout(LayoutKind.Sequential)]
  418. internal class SECURITY_ATTRIBUTES
  419. {
  420. internal int nLength = Marshal.SizeOf(typeof(SECURITY_ATTRIBUTES));
  421. internal IntPtr lpSecurityDescriptor = IntPtr.Zero;
  422. internal bool bInheritHandle = false;
  423. }
  424. public unsafe delegate void MQReceiveCallback(int error, IntPtr handle, int timeout,
  425. int action, IntPtr props, NativeOverlapped* nativeOverlapped, IntPtr cursor);
  426. [DllImport(KERNEL32), ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
  427. [ResourceExposure(ResourceScope.None)]
  428. internal static extern int CloseHandle
  429. (
  430. IntPtr handle
  431. );
  432. [DllImport(SECUR32), ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
  433. [ResourceExposure(ResourceScope.None)]
  434. internal static extern int SspiFreeAuthIdentity(
  435. [In] IntPtr ppAuthIdentity
  436. );
  437. [DllImport(SECUR32), ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
  438. [ResourceExposure(ResourceScope.None)]
  439. internal static extern uint SspiExcludePackage(
  440. [In] IntPtr AuthIdentity,
  441. [MarshalAs(UnmanagedType.LPWStr)]
  442. [In] string pszPackageName,
  443. [Out] out IntPtr ppNewAuthIdentity);
  444. [DllImport(KERNEL32, SetLastError = true)]
  445. [ResourceExposure(ResourceScope.None)]
  446. internal unsafe static extern int ConnectNamedPipe
  447. (
  448. PipeHandle handle,
  449. NativeOverlapped* lpOverlapped
  450. );
  451. [DllImport(KERNEL32, CharSet = CharSet.Unicode, SetLastError = true)]
  452. [ResourceExposure(ResourceScope.Machine)]
  453. internal static extern PipeHandle CreateFile
  454. (
  455. string lpFileName,
  456. int dwDesiredAccess,
  457. int dwShareMode,
  458. IntPtr lpSECURITY_ATTRIBUTES,
  459. int dwCreationDisposition,
  460. int dwFlagsAndAttributes,
  461. IntPtr hTemplateFile
  462. );
  463. [DllImport(KERNEL32, CharSet = CharSet.Unicode, SetLastError = true)]
  464. [ResourceExposure(ResourceScope.None)]
  465. internal static extern SafeFileMappingHandle CreateFileMapping(
  466. IntPtr fileHandle,
  467. SECURITY_ATTRIBUTES securityAttributes,
  468. int protect,
  469. int sizeHigh,
  470. int sizeLow,
  471. string name
  472. );
  473. [DllImport(KERNEL32, CharSet = CharSet.Unicode, SetLastError = true)]
  474. [ResourceExposure(ResourceScope.Machine)]
  475. internal unsafe static extern PipeHandle CreateNamedPipe
  476. (
  477. string name,
  478. int openMode,
  479. int pipeMode,
  480. int maxInstances,
  481. int outBufSize,
  482. int inBufSize,
  483. int timeout,
  484. SECURITY_ATTRIBUTES securityAttributes
  485. );
  486. [DllImport(KERNEL32, SetLastError = true)]
  487. [ResourceExposure(ResourceScope.None)]
  488. internal unsafe static extern int DisconnectNamedPipe
  489. (
  490. PipeHandle handle
  491. );
  492. [DllImport(KERNEL32, ExactSpelling = true, SetLastError = true)]
  493. [ResourceExposure(ResourceScope.None)]
  494. internal static extern bool DuplicateHandle(
  495. IntPtr hSourceProcessHandle,
  496. PipeHandle hSourceHandle,
  497. SafeCloseHandle hTargetProcessHandle,
  498. out IntPtr lpTargetHandle,
  499. int dwDesiredAccess,
  500. bool bInheritHandle,
  501. int dwOptions
  502. );
  503. [DllImport(KERNEL32, CharSet = CharSet.Unicode)]
  504. [ResourceExposure(ResourceScope.None)]
  505. internal static extern int FormatMessage
  506. (
  507. int dwFlags,
  508. IntPtr lpSource,
  509. int dwMessageId,
  510. int dwLanguageId,
  511. StringBuilder lpBuffer,
  512. int nSize,
  513. IntPtr arguments
  514. );
  515. [DllImport(KERNEL32, CharSet = CharSet.Unicode)]
  516. [ResourceExposure(ResourceScope.None)]
  517. internal static extern int FormatMessage
  518. (
  519. int dwFlags,
  520. SafeLibraryHandle lpSource,
  521. int dwMessageId,
  522. int dwLanguageId,
  523. StringBuilder lpBuffer,
  524. int nSize,
  525. IntPtr arguments
  526. );
  527. [DllImport(KERNEL32, SetLastError = true)]
  528. [ResourceExposure(ResourceScope.None)]
  529. unsafe internal static extern int GetOverlappedResult
  530. (
  531. PipeHandle handle,
  532. NativeOverlapped* overlapped,
  533. out int bytesTransferred,
  534. int wait
  535. );
  536. // This p/invoke is for perf-sensitive codepaths which can guarantee a valid handle via custom locking.
  537. [DllImport(KERNEL32, SetLastError = true)]
  538. [ResourceExposure(ResourceScope.None)]
  539. unsafe internal static extern int GetOverlappedResult
  540. (
  541. IntPtr handle,
  542. NativeOverlapped* overlapped,
  543. out int bytesTransferred,
  544. int wait
  545. );
  546. // NOTE: a macro in win32
  547. [PermissionSet(SecurityAction.Demand, Unrestricted = true), SecuritySafeCritical]
  548. internal unsafe static bool HasOverlappedIoCompleted(
  549. NativeOverlapped* overlapped)
  550. {
  551. return overlapped->InternalLow != (IntPtr)STATUS_PENDING;
  552. }
  553. [DllImport(KERNEL32, SetLastError = true, CharSet = CharSet.Unicode)]
  554. [ResourceExposure(ResourceScope.Machine)]
  555. internal static extern SafeFileMappingHandle OpenFileMapping
  556. (
  557. int access,
  558. bool inheritHandle,
  559. string name
  560. );
  561. [DllImport(KERNEL32, SetLastError = true)]
  562. [ResourceExposure(ResourceScope.None)]
  563. internal static extern SafeViewOfFileHandle MapViewOfFile
  564. (
  565. SafeFileMappingHandle handle,
  566. int dwDesiredAccess,
  567. int dwFileOffsetHigh,
  568. int dwFileOffsetLow,
  569. IntPtr dwNumberOfBytesToMap
  570. );
  571. [DllImport(KERNEL32, SetLastError = true)]
  572. [ResourceExposure(ResourceScope.None)]
  573. [SuppressUnmanagedCodeSecurity, HostProtection(SecurityAction.LinkDemand)]
  574. public static extern int QueryPerformanceCounter(out long time);
  575. // This p/invoke is for perf-sensitive codepaths which can guarantee a valid handle via custom locking.
  576. [DllImport(KERNEL32, SetLastError = true)]
  577. [ResourceExposure(ResourceScope.None)]
  578. unsafe internal static extern int ReadFile
  579. (
  580. IntPtr handle,
  581. byte* bytes,
  582. int numBytesToRead,
  583. IntPtr numBytesRead_mustBeZero,
  584. NativeOverlapped* overlapped
  585. );
  586. [DllImport(KERNEL32, SetLastError = true)]
  587. [ResourceExposure(ResourceScope.None)]
  588. internal static extern int SetNamedPipeHandleState
  589. (
  590. PipeHandle handle,
  591. ref int mode,
  592. IntPtr collectionCount,
  593. IntPtr collectionDataTimeout
  594. );
  595. // This p/invoke is for perf-sensitive codepaths which can guarantee a valid handle via custom locking.
  596. [DllImport(KERNEL32, SetLastError = true)]
  597. [ResourceExposure(ResourceScope.None)]
  598. internal static unsafe extern int WriteFile
  599. (
  600. IntPtr handle,
  601. byte* bytes,
  602. int numBytesToWrite,
  603. IntPtr numBytesWritten_mustBeZero,
  604. NativeOverlapped* lpOverlapped
  605. );
  606. [DllImport(KERNEL32, SetLastError = true)]
  607. [ResourceExposure(ResourceScope.None)]
  608. internal static unsafe extern bool GetNamedPipeClientProcessId(PipeHandle handle, out int id);
  609. [DllImport(KERNEL32, SetLastError = true)]
  610. [ResourceExposure(ResourceScope.None)]
  611. internal static unsafe extern bool GetNamedPipeServerProcessId(PipeHandle handle, out int id);
  612. [DllImport(KERNEL32, ExactSpelling = true),
  613. ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
  614. [ResourceExposure(ResourceScope.None)]
  615. internal static extern int UnmapViewOfFile
  616. (
  617. IntPtr lpBaseAddress
  618. );
  619. [DllImport(KERNEL32, ExactSpelling = true)]
  620. [ResourceExposure(ResourceScope.None)]
  621. public static extern bool SetWaitableTimer(SafeWaitHandle handle, ref long dueTime, int period, IntPtr mustBeZero, IntPtr mustBeZeroAlso, bool resume);
  622. [DllImport(KERNEL32, BestFitMapping = false, CharSet = CharSet.Auto)]
  623. [ResourceExposure(ResourceScope.Machine)]
  624. public static extern SafeWaitHandle CreateWaitableTimer(IntPtr mustBeZero, bool manualReset, string timerName);
  625. #if WSARECV
  626. [DllImport(WS2_32, SetLastError = true)]
  627. [ResourceExposure(ResourceScope.None)]
  628. internal static unsafe extern int WSARecv
  629. (
  630. IntPtr handle, WSABuffer* buffers, int bufferCount, out int bytesTransferred,
  631. ref int socketFlags,
  632. NativeOverlapped* nativeOverlapped,
  633. IntPtr completionRoutine
  634. );
  635. [DllImport(WS2_32, SetLastError = true)]
  636. [ResourceExposure(ResourceScope.None)]
  637. internal static unsafe extern bool WSAGetOverlappedResult(
  638. IntPtr socketHandle,
  639. NativeOverlapped* overlapped,
  640. out int bytesTransferred,
  641. bool wait,
  642. out uint flags
  643. );
  644. [StructLayout(LayoutKind.Sequential)]
  645. internal struct WSABuffer
  646. {
  647. public int length;
  648. public IntPtr buffer;
  649. }
  650. #endif
  651. internal static string GetComputerName(ComputerNameFormat nameType)
  652. {
  653. return System.Runtime.Interop.UnsafeNativeMethods.GetComputerName(nameType);
  654. }
  655. [DllImport(USERENV, SetLastError = true)]
  656. internal static extern int DeriveAppContainerSidFromAppContainerName
  657. (
  658. [In, MarshalAs(UnmanagedType.LPWStr)] string appContainerName,
  659. out IntPtr appContainerSid
  660. );
  661. [DllImport(ADVAPI32, SetLastError = true)]
  662. internal static extern IntPtr FreeSid
  663. (
  664. IntPtr pSid
  665. );
  666. // If the function succeeds, the return value is ERROR_SUCCESS and 'packageFamilyNameLength' contains the size of the data copied
  667. // to 'packageFamilyName' (in WCHARs, including the null-terminator). If the function fails, the return value is a Win32 error code.
  668. [DllImport(KERNEL32)]
  669. internal static extern int PackageFamilyNameFromFullName
  670. (
  671. [In, MarshalAs(UnmanagedType.LPWStr)] string packageFullName,
  672. ref uint packageFamilyNameLength,
  673. [In, Out, MarshalAs(UnmanagedType.LPWStr)] StringBuilder packageFamilyName
  674. );
  675. [DllImport(KERNEL32, SetLastError = true)]
  676. internal static extern bool GetAppContainerNamedObjectPath
  677. (
  678. IntPtr token,
  679. IntPtr appContainerSid,
  680. uint objectPathLength,
  681. [In, Out, MarshalAs(UnmanagedType.LPWStr)] StringBuilder objectPath,
  682. ref uint returnLength
  683. );
  684. [DllImport(KERNEL32, SetLastError = true)]
  685. internal static extern IntPtr GetCurrentProcess();
  686. [DllImport(ADVAPI32, SetLastError = true)]
  687. [ResourceExposure(ResourceScope.None)]
  688. internal static extern bool OpenProcessToken
  689. (
  690. IntPtr ProcessHandle,
  691. TokenAccessLevels DesiredAccess,
  692. out SafeCloseHandle TokenHandle
  693. );
  694. // Token marshalled as byte[]
  695. [DllImport(ADVAPI32, SetLastError = true)]
  696. static extern unsafe bool GetTokenInformation
  697. (
  698. SafeCloseHandle tokenHandle,
  699. TOKEN_INFORMATION_CLASS tokenInformationClass,
  700. byte[] tokenInformation,
  701. uint tokenInformationLength,
  702. out uint returnLength
  703. );
  704. // Token marshalled as uint
  705. [DllImport(ADVAPI32, SetLastError = true)]
  706. static extern bool GetTokenInformation
  707. (
  708. SafeCloseHandle tokenHandle,
  709. TOKEN_INFORMATION_CLASS tokenInformationClass,
  710. out uint tokenInformation,
  711. uint tokenInformationLength,
  712. out uint returnLength
  713. );
  714. internal static unsafe SecurityIdentifier GetAppContainerSid(SafeCloseHandle tokenHandle)
  715. {
  716. // Get length of buffer needed for sid.
  717. uint returnLength = UnsafeNativeMethods.GetTokenInformationLength(
  718. tokenHandle,
  719. TOKEN_INFORMATION_CLASS.TokenAppContainerSid);
  720. byte[] tokenInformation = new byte[returnLength];
  721. fixed (byte* pTokenInformation = tokenInformation)
  722. {
  723. if (!UnsafeNativeMethods.GetTokenInformation(
  724. tokenHandle,
  725. TOKEN_INFORMATION_CLASS.TokenAppContainerSid,
  726. tokenInformation,
  727. returnLength,
  728. out returnLength))
  729. {
  730. int errorCode = Marshal.GetLastWin32Error();
  731. throw FxTrace.Exception.AsError(new Win32Exception(errorCode));
  732. }
  733. TokenAppContainerInfo* ptg = (TokenAppContainerInfo*)pTokenInformation;
  734. return new SecurityIdentifier(ptg->psid);
  735. }
  736. }
  737. [StructLayout(LayoutKind.Sequential)]
  738. struct TokenAppContainerInfo
  739. {
  740. public IntPtr psid;
  741. }
  742. static uint GetTokenInformationLength(SafeCloseHandle token, TOKEN_INFORMATION_CLASS tokenInformationClass)
  743. {
  744. uint lengthNeeded;
  745. bool success;
  746. if (!(success = GetTokenInformation(
  747. token,
  748. tokenInformationClass,
  749. null,
  750. 0,
  751. out lengthNeeded)))
  752. {
  753. int error = Marshal.GetLastWin32Error();
  754. if (error != UnsafeNativeMethods.ERROR_INSUFFICIENT_BUFFER)
  755. {
  756. throw FxTrace.Exception.AsError(new Win32Exception(error));
  757. }
  758. }
  759. Fx.Assert(!success, "Retreving the length should always fail.");
  760. return lengthNeeded;
  761. }
  762. internal static int GetSessionId(SafeCloseHandle tokenHandle)
  763. {
  764. uint sessionId;
  765. uint returnLength;
  766. if (!UnsafeNativeMethods.GetTokenInformation(
  767. tokenHandle,
  768. TOKEN_INFORMATION_CLASS.TokenSessionId,
  769. out sessionId,
  770. sizeof(uint),
  771. out returnLength))
  772. {
  773. int errorCode = Marshal.GetLastWin32Error();
  774. throw FxTrace.Exception.AsError(new Win32Exception(errorCode));
  775. }
  776. return (int)sessionId;
  777. }
  778. internal static bool RunningInAppContainer(SafeCloseHandle tokenHandle)
  779. {
  780. uint runningInAppContainer;
  781. uint returnLength;
  782. if (!UnsafeNativeMethods.GetTokenInformation(
  783. tokenHandle,
  784. TOKEN_INFORMATION_CLASS.TokenIsAppContainer,
  785. out runningInAppContainer,
  786. sizeof(uint),
  787. out returnLength))
  788. {
  789. int errorCode = Marshal.GetLastWin32Error();
  790. throw FxTrace.Exception.AsError(new Win32Exception(errorCode));
  791. }
  792. return runningInAppContainer == 1;
  793. }
  794. [StructLayout(LayoutKind.Sequential)]
  795. public class MQMSGPROPS
  796. {
  797. public int count;
  798. public IntPtr ids;
  799. public IntPtr variants;
  800. public IntPtr status;
  801. }
  802. [StructLayout(LayoutKind.Explicit)]
  803. public struct MQPROPVARIANT
  804. {
  805. [FieldOffset(0)]
  806. public ushort vt;
  807. [FieldOffset(2)]
  808. public ushort reserved1;
  809. [FieldOffset(4)]
  810. public ushort reserved2;
  811. [FieldOffset(6)]
  812. public ushort reserved3;
  813. [FieldOffset(8)]
  814. public byte byteValue;
  815. [FieldOffset(8)]
  816. public short shortValue;
  817. [FieldOffset(8)]
  818. public int intValue;
  819. [FieldOffset(8)]
  820. public long longValue;
  821. [FieldOffset(8)]
  822. public IntPtr intPtr;
  823. [FieldOffset(8)]
  824. public CAUI1 byteArrayValue;
  825. [FieldOffset(8)]
  826. public CALPWSTR stringArraysValue;
  827. [StructLayout(LayoutKind.Sequential)]
  828. public struct CAUI1
  829. {
  830. public int size;
  831. public IntPtr intPtr;
  832. }
  833. [StructLayout(LayoutKind.Sequential)]
  834. public struct CALPWSTR
  835. {
  836. public int count;
  837. public IntPtr stringArrays;
  838. }
  839. }
  840. [DllImport(MQRT, CharSet = CharSet.Unicode)]
  841. [ResourceExposure(ResourceScope.Machine)]
  842. public static extern int MQOpenQueue(string formatName, int access, int shareMode, out MsmqQueueHandle handle);
  843. [DllImport(MQRT, CharSet = CharSet.Unicode)]
  844. [ResourceExposure(ResourceScope.None)]
  845. public static extern int MQBeginTransaction(out ITransaction refTransaction);
  846. [DllImport(MQRT, CharSet = CharSet.Unicode)]
  847. [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
  848. [ResourceExposure(ResourceScope.None)]
  849. public static extern int MQCloseQueue(IntPtr handle);
  850. [DllImport(MQRT, CharSet = CharSet.Unicode)]
  851. [ResourceExposure(ResourceScope.None)]
  852. public static extern int MQSendMessage(MsmqQueueHandle handle, IntPtr properties, IntPtr transaction);
  853. [DllImport(MQRT, CharSet = CharSet.Unicode)]
  854. [ResourceExposure(ResourceScope.None)]
  855. public static extern int MQSendMessage(MsmqQueueHandle handle, IntPtr properties, IDtcTransaction transaction);
  856. [DllImport(MQRT, CharSet = CharSet.Unicode)]
  857. [ResourceExposure(ResourceScope.None)]
  858. public unsafe static extern int MQReceiveMessage(MsmqQueueHandle handle, int timeout, int action, IntPtr properties,
  859. NativeOverlapped* nativeOverlapped, IntPtr receiveCallback, IntPtr cursorHandle, IntPtr transaction);
  860. [DllImport(MQRT, CharSet = CharSet.Unicode)]
  861. [ResourceExposure(ResourceScope.None)]
  862. public unsafe static extern int MQReceiveMessage(IntPtr handle, int timeout, int action, IntPtr properties,
  863. NativeOverlapped* nativeOverlapped, IntPtr receiveCallback, IntPtr cursorHandle, IntPtr transaction);
  864. [DllImport(MQRT, CharSet = CharSet.Unicode)]
  865. [ResourceExposure(ResourceScope.None)]
  866. public unsafe static extern int MQReceiveMessage(MsmqQueueHandle handle, int timeout, int action, IntPtr properties,
  867. NativeOverlapped* nativeOverlapped, IntPtr receiveCallback, IntPtr cursorHandle, IDtcTransaction transaction);
  868. [DllImport(MQRT, CharSet = CharSet.Unicode)]
  869. [ResourceExposure(ResourceScope.None)]
  870. public unsafe static extern int MQReceiveMessage(IntPtr handle, int timeout, int action, IntPtr properties,
  871. NativeOverlapped* nativeOverlapped, IntPtr receiveCallback, IntPtr cursorHandle, IDtcTransaction transaction);
  872. [DllImport(MQRT, CharSet = CharSet.Unicode)]
  873. [ResourceExposure(ResourceScope.None)]
  874. public unsafe static extern int MQReceiveMessage(MsmqQueueHandle handle, int timeout, int action, IntPtr properties,
  875. NativeOverlapped* nativeOverlapped, MQReceiveCallback receiveCallback, IntPtr cursorHandle, IntPtr transaction);
  876. [DllImport(MQRT, CharSet = CharSet.Unicode)]
  877. [ResourceExposure(ResourceScope.None)]
  878. public unsafe static extern int MQReceiveMessage(IntPtr handle, int timeout, int action, IntPtr properties,
  879. NativeOverlapped* nativeOverlapped, IntPtr receiveCallback, IntPtr cursorHandle, ITransaction transaction);
  880. [DllImport(MQRT, CharSet = CharSet.Unicode)]
  881. [ResourceExposure(ResourceScope.None)]
  882. public unsafe static extern int MQReceiveMessageByLookupId(MsmqQueueHandle handle, long lookupId, int action,
  883. IntPtr properties, NativeOverlapped* nativeOverlapped, IntPtr receiveCallback, IDtcTransaction transaction);
  884. [DllImport(MQRT, CharSet = CharSet.Unicode)]
  885. [ResourceExposure(ResourceScope.None)]
  886. public unsafe static extern int MQReceiveMessageByLookupId(MsmqQueueHandle handle, long lookupId, int action,
  887. IntPtr properties, NativeOverlapped* nativeOverlapped, IntPtr receiveCallback, IntPtr transaction);
  888. [DllImport(MQRT, CharSet = CharSet.Unicode)]
  889. [ResourceExposure(ResourceScope.None)]
  890. public unsafe static extern int MQReceiveMessageByLookupId(MsmqQueueHandle handle, long lookupId, int action,
  891. IntPtr properties, NativeOverlapped* nativeOverlapped, IntPtr receiveCallback, ITransaction transaction);
  892. [DllImport(MQRT, CharSet = CharSet.Unicode)]
  893. [ResourceExposure(ResourceScope.None)]
  894. public unsafe static extern int MQGetPrivateComputerInformation(string computerName, IntPtr properties);
  895. [DllImport(MQRT, CharSet = CharSet.Unicode)]
  896. [ResourceExposure(ResourceScope.None)]
  897. public unsafe static extern int MQMarkMessageRejected(MsmqQueueHandle handle, long lookupId);
  898. [DllImport(MQRT, CharSet = CharSet.Unicode)]
  899. [ResourceExposure(ResourceScope.None)]
  900. public static extern int MQMoveMessage(MsmqQueueHandle sourceQueueHandle,
  901. MsmqQueueHandle destinationQueueHandle,
  902. long lookupId,
  903. IntPtr transaction);
  904. [DllImport(MQRT, CharSet = CharSet.Unicode)]
  905. [ResourceExposure(ResourceScope.None)]
  906. public static extern int MQMoveMessage(MsmqQueueHandle sourceQueueHandle,
  907. MsmqQueueHandle destinationQueueHandle,
  908. long lookupId,
  909. IDtcTransaction transaction);
  910. [DllImport(MQRT, CharSet = CharSet.Unicode)]
  911. [ResourceExposure(ResourceScope.None)]
  912. public unsafe static extern int MQGetOverlappedResult(NativeOverlapped* nativeOverlapped);
  913. [DllImport(MQRT, CharSet = CharSet.Unicode)]
  914. [ResourceExposure(ResourceScope.None)]
  915. public unsafe static extern int MQGetQueueProperties(string formatName, IntPtr properties);
  916. [DllImport(MQRT, CharSet = CharSet.Unicode)]
  917. [ResourceExposure(ResourceScope.None)]
  918. public unsafe static extern int MQPathNameToFormatName(string pathName, StringBuilder formatName, ref int count);
  919. [DllImport(MQRT, CharSet = CharSet.Unicode)]
  920. [ResourceExposure(ResourceScope.None)]
  921. public unsafe static extern int MQMgmtGetInfo(string computerName, string objectName, IntPtr properties);
  922. [DllImport(MQRT, CharSet = CharSet.Unicode)]
  923. [ResourceExposure(ResourceScope.None)]
  924. public unsafe static extern void MQFreeMemory(IntPtr nativeBuffer);
  925. [DllImport(KERNEL32, SetLastError = true)]
  926. [ResourceExposure(ResourceScope.None)]
  927. public unsafe static extern int GetHandleInformation(MsmqQueueHandle handle, out int flags);
  928. [StructLayout(LayoutKind.Sequential)]
  929. public struct MEMORYSTATUSEX
  930. {
  931. public uint dwLength;
  932. public uint dwMemoryLoad;
  933. public ulong ullTotalPhys;
  934. public ulong ullAvailPhys;
  935. public ulong ullTotalPageFile;
  936. public ulong ullAvailPageFile;
  937. public ulong ullTotalVirtual;
  938. public ulong ullAvailVirtual;
  939. public ulong ullAvailExtendedVirtual;
  940. }
  941. [DllImport(KERNEL32, SetLastError = true)]
  942. [ResourceExposure(ResourceScope.None)]
  943. public static extern bool GlobalMemoryStatusEx(ref MEMORYSTATUSEX lpBuffer);
  944. [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
  945. [DllImport(KERNEL32, CallingConvention = CallingConvention.StdCall, SetLastError = true)]
  946. [ResourceExposure(ResourceScope.None)]
  947. internal static extern IntPtr VirtualAlloc(IntPtr lpAddress, UIntPtr dwSize, uint flAllocationType, uint flProtect);
  948. [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
  949. [DllImport(KERNEL32, CallingConvention = CallingConvention.StdCall, SetLastError = true)]
  950. [ResourceExposure(ResourceScope.None)]
  951. internal static extern bool VirtualFree(IntPtr lpAddress, UIntPtr dwSize, uint dwFreeType);
  952. [DllImport(KERNEL32, CharSet = CharSet.Auto)]
  953. [ResourceExposure(ResourceScope.None)]
  954. internal static extern IntPtr GetProcAddress(SafeLibraryHandle hModule, [In, MarshalAs(UnmanagedType.LPStr)]string lpProcName);
  955. [DllImport(KERNEL32, CharSet = CharSet.Unicode, SetLastError = true)]
  956. [ResourceExposure(ResourceScope.Process)]
  957. internal static extern SafeLibraryHandle LoadLibrary(string libFilename);
  958. // On Vista and higher, check the value of the machine FIPS policy
  959. [DllImport(BCRYPT, SetLastError = true)]
  960. [ResourceExposure(ResourceScope.None)]
  961. internal static extern int BCryptGetFipsAlgorithmMode(
  962. [MarshalAs(UnmanagedType.U1), Out] out bool pfEnabled
  963. );
  964. // AppModel.h functions (Win8+)
  965. [DllImport(KERNEL32, CharSet = CharSet.None, EntryPoint = "GetCurrentPackageId")]
  966. [SecurityCritical]
  967. [return: MarshalAs(UnmanagedType.I4)]
  968. private static extern Int32 GetCurrentPackageId(ref Int32 pBufferLength, Byte[] pBuffer);
  969. [Fx.Tag.SecurityNote(
  970. Critical = "Critical because it calls the native function GetCurrentPackageId.",
  971. Safe = "Safe because it takes no user input and it doesn't leak security sensitive information.")]
  972. [SecuritySafeCritical]
  973. private static bool _IsTailoredApplication()
  974. {
  975. if (OSEnvironmentHelper.IsAtLeast(OSVersion.Win8))
  976. {
  977. int bufLen = 0;
  978. // Will return ERROR_INSUFFICIENT_BUFFER when running within a tailored application,
  979. // and will return ERROR_NO_PACKAGE_IDENTITY otherwise.
  980. return GetCurrentPackageId(ref bufLen, null) == ERROR_INSUFFICIENT_BUFFER;
  981. }
  982. else
  983. {
  984. return false;
  985. }
  986. }
  987. /// <summary>
  988. /// Indicates weather the running application is an immersive (or modern) Windows 8 (or later) application.
  989. /// </summary>
  990. internal static Lazy<bool> IsTailoredApplication = new Lazy<bool>(() => _IsTailoredApplication());
  991. }
  992. [SuppressUnmanagedCodeSecurity]
  993. class PipeHandle : SafeHandleMinusOneIsInvalid
  994. {
  995. internal PipeHandle() : base(true) { }
  996. // This is unsafe, but is useful for a duplicated handle, which is inherently unsafe already.
  997. internal PipeHandle(IntPtr handle)
  998. : base(true)
  999. {
  1000. SetHandle(handle);
  1001. }
  1002. internal int GetClientPid()
  1003. {
  1004. int pid;
  1005. #pragma warning suppress 56523 // [....], Win32Exception ctor calls Marshal.GetLastWin32Error()
  1006. bool success = UnsafeNativeMethods.GetNamedPipeClientProcessId(this, out pid);
  1007. if (!success)
  1008. {
  1009. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new Win32Exception());
  1010. }
  1011. return pid;
  1012. }
  1013. protected override bool ReleaseHandle()
  1014. {
  1015. return UnsafeNativeMethods.CloseHandle(handle) != 0;
  1016. }
  1017. }
  1018. [SuppressUnmanagedCodeSecurityAttribute()]
  1019. sealed class SafeFileMappingHandle : SafeHandleZeroOrMinusOneIsInvalid
  1020. {
  1021. internal SafeFileMappingHandle()
  1022. : base(true)
  1023. {
  1024. }
  1025. override protected bool ReleaseHandle()
  1026. {
  1027. return UnsafeNativeMethods.CloseHandle(handle) != 0;
  1028. }
  1029. }
  1030. [SuppressUnmanagedCodeSecurityAttribute]
  1031. sealed class SafeLibraryHandle : SafeHandleZeroOrMinusOneIsInvalid
  1032. {
  1033. bool doNotfreeLibraryOnRelease;
  1034. internal SafeLibraryHandle()
  1035. : base(true)
  1036. {
  1037. doNotfreeLibraryOnRelease = false;
  1038. }
  1039. public void DoNotFreeLibraryOnRelease()
  1040. {
  1041. this.doNotfreeLibraryOnRelease = true;
  1042. }
  1043. [DllImport(UnsafeNativeMethods.KERNEL32, CharSet = CharSet.Unicode)]
  1044. [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
  1045. private static extern bool FreeLibrary(IntPtr hModule);
  1046. override protected bool ReleaseHandle()
  1047. {
  1048. if (doNotfreeLibraryOnRelease)
  1049. {
  1050. handle = IntPtr.Zero;
  1051. return true;
  1052. }
  1053. return FreeLibrary(handle);
  1054. }
  1055. }
  1056. [SuppressUnmanagedCodeSecurityAttribute]
  1057. sealed class SafeViewOfFileHandle : SafeHandleZeroOrMinusOneIsInvalid
  1058. {
  1059. internal SafeViewOfFileHandle()
  1060. : base(true)
  1061. {
  1062. }
  1063. override protected bool ReleaseHandle()
  1064. {
  1065. if (UnsafeNativeMethods.UnmapViewOfFile(handle) != 0)
  1066. {
  1067. handle = IntPtr.Zero;
  1068. return true;
  1069. }
  1070. return false;
  1071. }
  1072. }
  1073. [SuppressUnmanagedCodeSecurity]
  1074. sealed class MsmqQueueHandle : SafeHandleZeroOrMinusOneIsInvalid
  1075. {
  1076. internal MsmqQueueHandle() : base(true) { }
  1077. protected override bool ReleaseHandle()
  1078. {
  1079. return UnsafeNativeMethods.MQCloseQueue(handle) >= 0;
  1080. }
  1081. }
  1082. }