1 //------------------------------------------------------------
2 // Copyright (c) Microsoft Corporation. All rights reserved.
3 //------------------------------------------------------------
5 namespace System
.ServiceModel
.Channels
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
;
18 using System
.Transactions
;
19 using System
.ServiceModel
.Activation
;
20 using System
.ServiceModel
.Security
;
21 using System
.EnterpriseServices
;
23 using SafeCloseHandle
= System
.ServiceModel
.Activation
.SafeCloseHandle
;
24 using TOKEN_INFORMATION_CLASS
= System
.ServiceModel
.Activation
.ListenerUnsafeNativeMethods
.TOKEN_INFORMATION_CLASS
;
26 [SuppressUnmanagedCodeSecurity
]
27 internal static class UnsafeNativeMethods
29 public const string KERNEL32
= "kernel32.dll";
30 public const string ADVAPI32
= "advapi32.dll";
31 public const string BCRYPT
= "bcrypt.dll";
32 public const string MQRT
= "mqrt.dll";
33 public const string SECUR32
= "secur32.dll";
34 public const string USERENV
= "userenv.dll";
37 public const string WS2_32
= "ws2_32.dll";
42 public const int ERROR_SUCCESS
= 0;
43 public const int ERROR_FILE_NOT_FOUND
= 2;
44 public const int ERROR_ACCESS_DENIED
= 5;
45 public const int ERROR_INVALID_HANDLE
= 6;
46 public const int ERROR_NOT_ENOUGH_MEMORY
= 8;
47 public const int ERROR_OUTOFMEMORY
= 14;
48 public const int ERROR_SHARING_VIOLATION
= 32;
49 public const int ERROR_NETNAME_DELETED
= 64;
50 public const int ERROR_INVALID_PARAMETER
= 87;
51 public const int ERROR_BROKEN_PIPE
= 109;
52 public const int ERROR_ALREADY_EXISTS
= 183;
53 public const int ERROR_PIPE_BUSY
= 231;
54 public const int ERROR_NO_DATA
= 232;
55 public const int ERROR_MORE_DATA
= 234;
56 public const int WAIT_TIMEOUT
= 258;
57 public const int ERROR_PIPE_CONNECTED
= 535;
58 public const int ERROR_OPERATION_ABORTED
= 995;
59 public const int ERROR_IO_PENDING
= 997;
60 public const int ERROR_SERVICE_ALREADY_RUNNING
= 1056;
61 public const int ERROR_SERVICE_DISABLED
= 1058;
62 public const int ERROR_NO_TRACKING_SERVICE
= 1172;
63 public const int ERROR_ALLOTTED_SPACE_EXCEEDED
= 1344;
64 public const int ERROR_NO_SYSTEM_RESOURCES
= 1450;
66 // When querying for the token length
67 const int ERROR_INSUFFICIENT_BUFFER
= 122;
69 public const int STATUS_PENDING
= 0x103;
72 public const int WSAACCESS
= 10013;
73 public const int WSAEMFILE
= 10024;
74 public const int WSAEMSGSIZE
= 10040;
75 public const int WSAEADDRINUSE
= 10048;
76 public const int WSAEADDRNOTAVAIL
= 10049;
77 public const int WSAENETDOWN
= 10050;
78 public const int WSAENETUNREACH
= 10051;
79 public const int WSAENETRESET
= 10052;
80 public const int WSAECONNABORTED
= 10053;
81 public const int WSAECONNRESET
= 10054;
82 public const int WSAENOBUFS
= 10055;
83 public const int WSAESHUTDOWN
= 10058;
84 public const int WSAETIMEDOUT
= 10060;
85 public const int WSAECONNREFUSED
= 10061;
86 public const int WSAEHOSTDOWN
= 10064;
87 public const int WSAEHOSTUNREACH
= 10065;
89 public const int DUPLICATE_CLOSE_SOURCE
= 0x00000001;
90 public const int DUPLICATE_SAME_ACCESS
= 0x00000002;
92 public const int FILE_FLAG_OVERLAPPED
= 0x40000000;
93 public const int FILE_FLAG_FIRST_PIPE_INSTANCE
= 0x00080000;
95 public const int GENERIC_ALL
= 0x10000000;
96 public const int GENERIC_READ
= unchecked((int)0x80000000);
97 public const int GENERIC_WRITE
= 0x40000000;
98 public const int FILE_CREATE_PIPE_INSTANCE
= 0x00000004;
99 public const int FILE_WRITE_ATTRIBUTES
= 0x00000100;
100 public const int FILE_WRITE_DATA
= 0x00000002;
101 public const int FILE_WRITE_EA
= 0x00000010;
103 public const int OPEN_EXISTING
= 3;
105 public const int PIPE_ACCESS_DUPLEX
= 3;
106 public const int PIPE_UNLIMITED_INSTANCES
= 255;
107 public const int PIPE_TYPE_BYTE
= 0;
108 public const int PIPE_TYPE_MESSAGE
= 4;
109 public const int PIPE_READMODE_BYTE
= 0;
110 public const int PIPE_READMODE_MESSAGE
= 2;
112 // VirtualAlloc constants
113 public const uint MEM_COMMIT
= 0x1000;
114 public const uint MEM_DECOMMIT
= 0x4000;
115 public const int PAGE_READWRITE
= 4;
117 public const int FILE_MAP_WRITE
= 2;
118 public const int FILE_MAP_READ
= 4;
121 public const int SDDL_REVISION_1
= 1;
123 public const int SECURITY_ANONYMOUS
= 0x00000000;
124 public const int SECURITY_QOS_PRESENT
= 0x00100000;
125 public const int SECURITY_IDENTIFICATION
= 0x00010000;
127 public const int FORMAT_MESSAGE_ALLOCATE_BUFFER
= 0x00000100;
128 public const int FORMAT_MESSAGE_IGNORE_INSERTS
= 0x00000200;
129 public const int FORMAT_MESSAGE_FROM_STRING
= 0x00000400;
130 public const int FORMAT_MESSAGE_FROM_SYSTEM
= 0x00001000;
131 public const int FORMAT_MESSAGE_ARGUMENT_ARRAY
= 0x00002000;
132 public const int FORMAT_MESSAGE_FROM_HMODULE
= 0x00000800;
134 public const int MQ_RECEIVE_ACCESS
= 0x00000001;
135 public const int MQ_SEND_ACCESS
= 0x00000002;
136 public const int MQ_MOVE_ACCESS
= 0x00000004;
138 public const int MQ_DENY_NONE
= 0x00000000;
139 public const int MQ_DENY_RECEIVE_SHARE
= 0x00000001;
141 public const int MQ_ACTION_RECEIVE
= 0x00000000;
142 public const int MQ_ACTION_PEEK_CURRENT
= unchecked((int)0x80000000);
143 public const int MQ_ACTION_PEEK_NEXT
= unchecked((int)0x80000001);
145 public const int MQ_LOOKUP_RECEIVE_CURRENT
= unchecked((int)0x40000020);
146 public const int MQ_LOOKUP_PEEK_CURRENT
= unchecked((int)0x40000010);
148 public const int MQ_NO_TRANSACTION
= 0;
149 public const int MQ_MTS_TRANSACTION
= 1;
150 public const int MQ_SINGLE_MESSAGE
= 3;
152 public const int MQ_INFORMATION_PROPERTY
= unchecked((int)0x400E0001);
153 public const int MQ_INFORMATION_ILLEGAL_PROPERTY
= unchecked((int)0x400E0002);
154 public const int MQ_INFORMATION_PROPERTY_IGNORED
= unchecked((int)0x400E0003);
155 public const int MQ_INFORMATION_UNSUPPORTED_PROPERTY
= unchecked((int)0x400E0004);
156 public const int MQ_INFORMATION_DUPLICATE_PROPERTY
= unchecked((int)0x400E0005);
157 public const int MQ_INFORMATION_OPERATION_PENDING
= unchecked((int)0x400E0006);
158 public const int MQ_INFORMATION_FORMATNAME_BUFFER_TOO_SMALL
= unchecked((int)0x400E0009);
159 public const int MQ_INFORMATION_INTERNAL_USER_CERT_EXIST
= unchecked((int)0x400E000A);
160 public const int MQ_INFORMATION_OWNER_IGNORED
= unchecked((int)0x400E000B);
161 public const int MQ_ERROR
= unchecked((int)0xC00E0001);
162 public const int MQ_ERROR_PROPERTY
= unchecked((int)0xC00E0002);
163 public const int MQ_ERROR_QUEUE_NOT_FOUND
= unchecked((int)0xC00E0003);
164 public const int MQ_ERROR_QUEUE_NOT_ACTIVE
= unchecked((int)0xC00E0004);
165 public const int MQ_ERROR_QUEUE_EXISTS
= unchecked((int)0xC00E0005);
166 public const int MQ_ERROR_INVALID_PARAMETER
= unchecked((int)0xC00E0006);
167 public const int MQ_ERROR_INVALID_HANDLE
= unchecked((int)0xC00E0007);
168 public const int MQ_ERROR_OPERATION_CANCELLED
= unchecked((int)0xC00E0008);
169 public const int MQ_ERROR_SHARING_VIOLATION
= unchecked((int)0xC00E0009);
170 public const int MQ_ERROR_SERVICE_NOT_AVAILABLE
= unchecked((int)0xC00E000B);
171 public const int MQ_ERROR_MACHINE_NOT_FOUND
= unchecked((int)0xC00E000D);
172 public const int MQ_ERROR_ILLEGAL_SORT
= unchecked((int)0xC00E0010);
173 public const int MQ_ERROR_ILLEGAL_USER
= unchecked((int)0xC00E0011);
174 public const int MQ_ERROR_NO_DS
= unchecked((int)0xC00E0013);
175 public const int MQ_ERROR_ILLEGAL_QUEUE_PATHNAME
= unchecked((int)0xC00E0014);
176 public const int MQ_ERROR_ILLEGAL_PROPERTY_VALUE
= unchecked((int)0xC00E0018);
177 public const int MQ_ERROR_ILLEGAL_PROPERTY_VT
= unchecked((int)0xC00E0019);
178 public const int MQ_ERROR_BUFFER_OVERFLOW
= unchecked((int)0xC00E001A);
179 public const int MQ_ERROR_IO_TIMEOUT
= unchecked((int)0xC00E001B);
180 public const int MQ_ERROR_ILLEGAL_CURSOR_ACTION
= unchecked((int)0xC00E001C);
181 public const int MQ_ERROR_MESSAGE_ALREADY_RECEIVED
= unchecked((int)0xC00E001D);
182 public const int MQ_ERROR_ILLEGAL_FORMATNAME
= unchecked((int)0xC00E001E);
183 public const int MQ_ERROR_FORMATNAME_BUFFER_TOO_SMALL
= unchecked((int)0xC00E001F);
184 public const int MQ_ERROR_UNSUPPORTED_FORMATNAME_OPERATION
= unchecked((int)0xC00E0020);
185 public const int MQ_ERROR_ILLEGAL_SECURITY_DESCRIPTOR
= unchecked((int)0xC00E0021);
186 public const int MQ_ERROR_SENDERID_BUFFER_TOO_SMALL
= unchecked((int)0xC00E0022);
187 public const int MQ_ERROR_SECURITY_DESCRIPTOR_TOO_SMALL
= unchecked((int)0xC00E0023);
188 public const int MQ_ERROR_CANNOT_IMPERSONATE_CLIENT
= unchecked((int)0xC00E0024);
189 public const int MQ_ERROR_ACCESS_DENIED
= unchecked((int)0xC00E0025);
190 public const int MQ_ERROR_PRIVILEGE_NOT_HELD
= unchecked((int)0xC00E0026);
191 public const int MQ_ERROR_INSUFFICIENT_RESOURCES
= unchecked((int)0xC00E0027);
192 public const int MQ_ERROR_USER_BUFFER_TOO_SMALL
= unchecked((int)0xC00E0028);
193 public const int MQ_ERROR_MESSAGE_STORAGE_FAILED
= unchecked((int)0xC00E002A);
194 public const int MQ_ERROR_SENDER_CERT_BUFFER_TOO_SMALL
= unchecked((int)0xC00E002B);
195 public const int MQ_ERROR_INVALID_CERTIFICATE
= unchecked((int)0xC00E002C);
196 public const int MQ_ERROR_CORRUPTED_INTERNAL_CERTIFICATE
= unchecked((int)0xC00E002D);
197 public const int MQ_ERROR_INTERNAL_USER_CERT_EXIST
= unchecked((int)0xC00E002E);
198 public const int MQ_ERROR_NO_INTERNAL_USER_CERT
= unchecked((int)0xC00E002F);
199 public const int MQ_ERROR_CORRUPTED_SECURITY_DATA
= unchecked((int)0xC00E0030);
200 public const int MQ_ERROR_CORRUPTED_PERSONAL_CERT_STORE
= unchecked((int)0xC00E0031);
201 public const int MQ_ERROR_COMPUTER_DOES_NOT_SUPPORT_ENCRYPTION
= unchecked((int)0xC00E0033);
202 public const int MQ_ERROR_BAD_SECURITY_CONTEXT
= unchecked((int)0xC00E0035);
203 public const int MQ_ERROR_COULD_NOT_GET_USER_SID
= unchecked((int)0xC00E0036);
204 public const int MQ_ERROR_COULD_NOT_GET_ACCOUNT_INFO
= unchecked((int)0xC00E0037);
205 public const int MQ_ERROR_ILLEGAL_MQCOLUMNS
= unchecked((int)0xC00E0038);
206 public const int MQ_ERROR_ILLEGAL_PROPID
= unchecked((int)0xC00E0039);
207 public const int MQ_ERROR_ILLEGAL_RELATION
= unchecked((int)0xC00E003A);
208 public const int MQ_ERROR_ILLEGAL_PROPERTY_SIZE
= unchecked((int)0xC00E003B);
209 public const int MQ_ERROR_ILLEGAL_RESTRICTION_PROPID
= unchecked((int)0xC00E003C);
210 public const int MQ_ERROR_ILLEGAL_MQQUEUEPROPS
= unchecked((int)0xC00E003D);
211 public const int MQ_ERROR_PROPERTY_NOTALLOWED
= unchecked((int)0xC00E003E);
212 public const int MQ_ERROR_INSUFFICIENT_PROPERTIES
= unchecked((int)0xC00E003F);
213 public const int MQ_ERROR_MACHINE_EXISTS
= unchecked((int)0xC00E0040);
214 public const int MQ_ERROR_ILLEGAL_MQQMPROPS
= unchecked((int)0xC00E0041);
215 public const int MQ_ERROR_DS_IS_FULL
= unchecked((int)0xC00E0042);
216 public const int MQ_ERROR_DS_ERROR
= unchecked((int)0xC00E0043);
217 public const int MQ_ERROR_INVALID_OWNER
= unchecked((int)0xC00E0044);
218 public const int MQ_ERROR_UNSUPPORTED_ACCESS_MODE
= unchecked((int)0xC00E0045);
219 public const int MQ_ERROR_RESULT_BUFFER_TOO_SMALL
= unchecked((int)0xC00E0046);
220 public const int MQ_ERROR_DELETE_CN_IN_USE
= unchecked((int)0xC00E0048);
221 public const int MQ_ERROR_NO_RESPONSE_FROM_OBJECT_SERVER
= unchecked((int)0xC00E0049);
222 public const int MQ_ERROR_OBJECT_SERVER_NOT_AVAILABLE
= unchecked((int)0xC00E004A);
223 public const int MQ_ERROR_QUEUE_NOT_AVAILABLE
= unchecked((int)0xC00E004B);
224 public const int MQ_ERROR_DTC_CONNECT
= unchecked((int)0xC00E004C);
225 public const int MQ_ERROR_TRANSACTION_IMPORT
= unchecked((int)0xC00E004E);
226 public const int MQ_ERROR_TRANSACTION_USAGE
= unchecked((int)0xC00E0050);
227 public const int MQ_ERROR_TRANSACTION_SEQUENCE
= unchecked((int)0xC00E0051);
228 public const int MQ_ERROR_MISSING_CONNECTOR_TYPE
= unchecked((int)0xC00E0055);
229 public const int MQ_ERROR_STALE_HANDLE
= unchecked((int)0xC00E0056);
230 public const int MQ_ERROR_TRANSACTION_ENLIST
= unchecked((int)0xC00E0058);
231 public const int MQ_ERROR_QUEUE_DELETED
= unchecked((int)0xC00E005A);
232 public const int MQ_ERROR_ILLEGAL_CONTEXT
= unchecked((int)0xC00E005B);
233 public const int MQ_ERROR_ILLEGAL_SORT_PROPID
= unchecked((int)0xC00E005C);
234 public const int MQ_ERROR_LABEL_TOO_LONG
= unchecked((int)0xC00E005D);
235 public const int MQ_ERROR_LABEL_BUFFER_TOO_SMALL
= unchecked((int)0xC00E005E);
236 public const int MQ_ERROR_MQIS_SERVER_EMPTY
= unchecked((int)0xC00E005F);
237 public const int MQ_ERROR_MQIS_READONLY_MODE
= unchecked((int)0xC00E0060);
238 public const int MQ_ERROR_SYMM_KEY_BUFFER_TOO_SMALL
= unchecked((int)0xC00E0061);
239 public const int MQ_ERROR_SIGNATURE_BUFFER_TOO_SMALL
= unchecked((int)0xC00E0062);
240 public const int MQ_ERROR_PROV_NAME_BUFFER_TOO_SMALL
= unchecked((int)0xC00E0063);
241 public const int MQ_ERROR_ILLEGAL_OPERATION
= unchecked((int)0xC00E0064);
242 public const int MQ_ERROR_WRITE_NOT_ALLOWED
= unchecked((int)0xC00E0065);
243 public const int MQ_ERROR_WKS_CANT_SERVE_CLIENT
= unchecked((int)0xC00E0066);
244 public const int MQ_ERROR_DEPEND_WKS_LICENSE_OVERFLOW
= unchecked((int)0xC00E0067);
245 public const int MQ_ERROR_REMOTE_MACHINE_NOT_AVAILABLE
= unchecked((int)0xC00E0069);
246 public const int MQ_ERROR_UNSUPPORTED_OPERATION
= unchecked((int)0xC00E006A);
247 public const int MQ_ERROR_ENCRYPTION_PROVIDER_NOT_SUPPORTED
= unchecked((int)0xC00E006B);
248 public const int MQ_ERROR_CANNOT_SET_CRYPTO_SEC_DESCR
= unchecked((int)0xC00E006C);
249 public const int MQ_ERROR_CERTIFICATE_NOT_PROVIDED
= unchecked((int)0xC00E006D);
250 public const int MQ_ERROR_Q_DNS_PROPERTY_NOT_SUPPORTED
= unchecked((int)0xC00E006E);
251 public const int MQ_ERROR_CANNOT_CREATE_CERT_STORE
= unchecked((int)0xC00E006F);
252 public const int MQ_ERROR_CANNOT_OPEN_CERT_STORE
= unchecked((int)0xC00E0070);
253 public const int MQ_ERROR_ILLEGAL_ENTERPRISE_OPERATION
= unchecked((int)0xC00E0071);
254 public const int MQ_ERROR_CANNOT_GRANT_ADD_GUID
= unchecked((int)0xC00E0072);
255 public const int MQ_ERROR_CANNOT_LOAD_MSMQOCM
= unchecked((int)0xC00E0073);
256 public const int MQ_ERROR_NO_ENTRY_POINT_MSMQOCM
= unchecked((int)0xC00E0074);
257 public const int MQ_ERROR_NO_MSMQ_SERVERS_ON_DC
= unchecked((int)0xC00E0075);
258 public const int MQ_ERROR_CANNOT_JOIN_DOMAIN
= unchecked((int)0xC00E0076);
259 public const int MQ_ERROR_CANNOT_CREATE_ON_GC
= unchecked((int)0xC00E0077);
260 public const int MQ_ERROR_GUID_NOT_MATCHING
= unchecked((int)0xC00E0078);
261 public const int MQ_ERROR_PUBLIC_KEY_NOT_FOUND
= unchecked((int)0xC00E0079);
262 public const int MQ_ERROR_PUBLIC_KEY_DOES_NOT_EXIST
= unchecked((int)0xC00E007A);
263 public const int MQ_ERROR_ILLEGAL_MQPRIVATEPROPS
= unchecked((int)0xC00E007B);
264 public const int MQ_ERROR_NO_GC_IN_DOMAIN
= unchecked((int)0xC00E007C);
265 public const int MQ_ERROR_NO_MSMQ_SERVERS_ON_GC
= unchecked((int)0xC00E007D);
266 public const int MQ_ERROR_CANNOT_GET_DN
= unchecked((int)0xC00E007E);
267 public const int MQ_ERROR_CANNOT_HASH_DATA_EX
= unchecked((int)0xC00E007F);
268 public const int MQ_ERROR_CANNOT_SIGN_DATA_EX
= unchecked((int)0xC00E0080);
269 public const int MQ_ERROR_CANNOT_CREATE_HASH_EX
= unchecked((int)0xC00E0081);
270 public const int MQ_ERROR_FAIL_VERIFY_SIGNATURE_EX
= unchecked((int)0xC00E0082);
271 public const int MQ_ERROR_CANNOT_DELETE_PSC_OBJECTS
= unchecked((int)0xC00E0083);
272 public const int MQ_ERROR_NO_MQUSER_OU
= unchecked((int)0xC00E0084);
273 public const int MQ_ERROR_CANNOT_LOAD_MQAD
= unchecked((int)0xC00E0085);
274 public const int MQ_ERROR_CANNOT_LOAD_MQDSSRV
= unchecked((int)0xC00E0086);
275 public const int MQ_ERROR_PROPERTIES_CONFLICT
= unchecked((int)0xC00E0087);
276 public const int MQ_ERROR_MESSAGE_NOT_FOUND
= unchecked((int)0xC00E0088);
277 public const int MQ_ERROR_CANT_RESOLVE_SITES
= unchecked((int)0xC00E0089);
278 public const int MQ_ERROR_NOT_SUPPORTED_BY_DEPENDENT_CLIENTS
= unchecked((int)0xC00E008A);
279 public const int MQ_ERROR_OPERATION_NOT_SUPPORTED_BY_REMOTE_COMPUTER
= unchecked((int)0xC00E008B);
280 public const int MQ_ERROR_NOT_A_CORRECT_OBJECT_CLASS
= unchecked((int)0xC00E008C);
281 public const int MQ_ERROR_MULTI_SORT_KEYS
= unchecked((int)0xC00E008D);
282 public const int MQ_ERROR_GC_NEEDED
= unchecked((int)0xC00E008E);
283 public const int MQ_ERROR_DS_BIND_ROOT_FOREST
= unchecked((int)0xC00E008F);
284 public const int MQ_ERROR_DS_LOCAL_USER
= unchecked((int)0xC00E0090);
285 public const int MQ_ERROR_Q_ADS_PROPERTY_NOT_SUPPORTED
= unchecked((int)0xC00E0091);
286 public const int MQ_ERROR_BAD_XML_FORMAT
= unchecked((int)0xC00E0092);
287 public const int MQ_ERROR_UNSUPPORTED_CLASS
= unchecked((int)0xC00E0093);
288 public const int MQ_ERROR_UNINITIALIZED_OBJECT
= unchecked((int)0xC00E0094);
289 public const int MQ_ERROR_CANNOT_CREATE_PSC_OBJECTS
= unchecked((int)0xC00E0095);
290 public const int MQ_ERROR_CANNOT_UPDATE_PSC_OBJECTS
= unchecked((int)0xC00E0096);
291 public const int MQ_ERROR_MESSAGE_LOCKED_UNDER_TRANSACTION
= unchecked((int)0xC00E009C);
293 public const int MQMSG_DELIVERY_EXPRESS
= 0;
294 public const int MQMSG_DELIVERY_RECOVERABLE
= 1;
296 public const int PROPID_M_MSGID_SIZE
= 20;
297 public const int PROPID_M_CORRELATIONID_SIZE
= 20;
299 public const int MQ_MAX_MSG_LABEL_LEN
= 250;
301 public const int MQMSG_JOURNAL_NONE
= 0;
302 public const int MQMSG_DEADLETTER
= 1;
303 public const int MQMSG_JOURNAL
= 2;
305 public const int MQMSG_ACKNOWLEDGMENT_NONE
= 0x00;
306 public const int MQMSG_ACKNOWLEDGMENT_POS_ARRIVAL
= 0x01;
307 public const int MQMSG_ACKNOWLEDGMENT_POS_RECEIVE
= 0x02;
308 public const int MQMSG_ACKNOWLEDGMENT_NEG_ARRIVAL
= 0x04;
309 public const int MQMSG_ACKNOWLEDGMENT_NEG_RECEIVE
= 0x08;
311 public const int MQMSG_CLASS_NORMAL
= 0x0;
312 public const int MQMSG_CLASS_REPORT
= 0x1;
314 public const int MQMSG_SENDERID_TYPE_NONE
= 0;
315 public const int MQMSG_SENDERID_TYPE_SID
= 1;
317 public const int MQMSG_AUTH_LEVEL_NONE
= 0;
318 public const int MQMSG_AUTH_LEVEL_ALWAYS
= 1;
320 public const int MQMSG_PRIV_LEVEL_NONE
= 0;
321 public const int MQMSG_PRIV_LEVEL_BODY_BASE
= 0x01;
322 public const int MQMSG_PRIV_LEVEL_BODY_ENHANCED
= 0x03;
324 public const int MQMSG_TRACE_NONE
= 0;
325 public const int MQMSG_SEND_ROUTE_TO_REPORT_QUEUE
= 1;
327 public const int PROPID_M_BASE
= 0;
328 public const int PROPID_M_CLASS
= (PROPID_M_BASE
+ 1); /* VT_UI2 */
329 public const int PROPID_M_MSGID
= (PROPID_M_BASE
+ 2); /* VT_UI1|VT_VECTOR */
330 public const int PROPID_M_CORRELATIONID
= (PROPID_M_BASE
+ 3); /* VT_UI1|VT_VECTOR */
331 public const int PROPID_M_PRIORITY
= (PROPID_M_BASE
+ 4); /* VT_UI1 */
332 public const int PROPID_M_DELIVERY
= (PROPID_M_BASE
+ 5); /* VT_UI1 */
333 public const int PROPID_M_ACKNOWLEDGE
= (PROPID_M_BASE
+ 6); /* VT_UI1 */
334 public const int PROPID_M_JOURNAL
= (PROPID_M_BASE
+ 7); /* VT_UI1 */
335 public const int PROPID_M_APPSPECIFIC
= (PROPID_M_BASE
+ 8); /* VT_UI4 */
336 public const int PROPID_M_BODY
= (PROPID_M_BASE
+ 9); /* VT_UI1|VT_VECTOR */
337 public const int PROPID_M_BODY_SIZE
= (PROPID_M_BASE
+ 10); /* VT_UI4 */
338 public const int PROPID_M_LABEL
= (PROPID_M_BASE
+ 11); /* VT_LPWSTR */
339 public const int PROPID_M_LABEL_LEN
= (PROPID_M_BASE
+ 12); /* VT_UI4 */
340 public const int PROPID_M_TIME_TO_REACH_QUEUE
= (PROPID_M_BASE
+ 13); /* VT_UI4 */
341 public const int PROPID_M_TIME_TO_BE_RECEIVED
= (PROPID_M_BASE
+ 14); /* VT_UI4 */
342 public const int PROPID_M_RESP_QUEUE
= (PROPID_M_BASE
+ 15); /* VT_LPWSTR */
343 public const int PROPID_M_RESP_QUEUE_LEN
= (PROPID_M_BASE
+ 16); /* VT_UI4 */
344 public const int PROPID_M_ADMIN_QUEUE
= (PROPID_M_BASE
+ 17); /* VT_LPWSTR */
345 public const int PROPID_M_ADMIN_QUEUE_LEN
= (PROPID_M_BASE
+ 18); /* VT_UI4 */
346 public const int PROPID_M_VERSION
= (PROPID_M_BASE
+ 19); /* VT_UI4 */
347 public const int PROPID_M_SENDERID
= (PROPID_M_BASE
+ 20); /* VT_UI1|VT_VECTOR */
348 public const int PROPID_M_SENDERID_LEN
= (PROPID_M_BASE
+ 21); /* VT_UI4 */
349 public const int PROPID_M_SENDERID_TYPE
= (PROPID_M_BASE
+ 22); /* VT_UI4 */
350 public const int PROPID_M_PRIV_LEVEL
= (PROPID_M_BASE
+ 23); /* VT_UI4 */
351 public const int PROPID_M_AUTH_LEVEL
= (PROPID_M_BASE
+ 24); /* VT_UI4 */
352 public const int PROPID_M_AUTHENTICATED
= (PROPID_M_BASE
+ 25); /* VT_UI1 */
353 public const int PROPID_M_HASH_ALG
= (PROPID_M_BASE
+ 26); /* VT_UI4 */
354 public const int PROPID_M_ENCRYPTION_ALG
= (PROPID_M_BASE
+ 27); /* VT_UI4 */
355 public const int PROPID_M_SENDER_CERT
= (PROPID_M_BASE
+ 28); /* VT_UI1|VT_VECTOR */
356 public const int PROPID_M_SENDER_CERT_LEN
= (PROPID_M_BASE
+ 29); /* VT_UI4 */
357 public const int PROPID_M_SRC_MACHINE_ID
= (PROPID_M_BASE
+ 30); /* VT_CLSID */
358 public const int PROPID_M_SENTTIME
= (PROPID_M_BASE
+ 31); /* VT_UI4 */
359 public const int PROPID_M_ARRIVEDTIME
= (PROPID_M_BASE
+ 32); /* VT_UI4 */
360 public const int PROPID_M_DEST_QUEUE
= (PROPID_M_BASE
+ 33); /* VT_LPWSTR */
361 public const int PROPID_M_DEST_QUEUE_LEN
= (PROPID_M_BASE
+ 34); /* VT_UI4 */
362 public const int PROPID_M_EXTENSION
= (PROPID_M_BASE
+ 35); /* VT_UI1|VT_VECTOR */
363 public const int PROPID_M_EXTENSION_LEN
= (PROPID_M_BASE
+ 36); /* VT_UI4 */
364 public const int PROPID_M_SECURITY_CONTEXT
= (PROPID_M_BASE
+ 37); /* VT_UI4 */
365 public const int PROPID_M_CONNECTOR_TYPE
= (PROPID_M_BASE
+ 38); /* VT_CLSID */
366 public const int PROPID_M_XACT_STATUS_QUEUE
= (PROPID_M_BASE
+ 39); /* VT_LPWSTR */
367 public const int PROPID_M_XACT_STATUS_QUEUE_LEN
= (PROPID_M_BASE
+ 40); /* VT_UI4 */
368 public const int PROPID_M_TRACE
= (PROPID_M_BASE
+ 41); /* VT_UI1 */
369 public const int PROPID_M_BODY_TYPE
= (PROPID_M_BASE
+ 42); /* VT_UI4 */
370 public const int PROPID_M_DEST_SYMM_KEY
= (PROPID_M_BASE
+ 43); /* VT_UI1|VT_VECTOR */
371 public const int PROPID_M_DEST_SYMM_KEY_LEN
= (PROPID_M_BASE
+ 44); /* VT_UI4 */
372 public const int PROPID_M_SIGNATURE
= (PROPID_M_BASE
+ 45); /* VT_UI1|VT_VECTOR */
373 public const int PROPID_M_SIGNATURE_LEN
= (PROPID_M_BASE
+ 46); /* VT_UI4 */
374 public const int PROPID_M_PROV_TYPE
= (PROPID_M_BASE
+ 47); /* VT_UI4 */
375 public const int PROPID_M_PROV_NAME
= (PROPID_M_BASE
+ 48); /* VT_LPWSTR */
376 public const int PROPID_M_PROV_NAME_LEN
= (PROPID_M_BASE
+ 49); /* VT_UI4 */
377 public const int PROPID_M_FIRST_IN_XACT
= (PROPID_M_BASE
+ 50); /* VT_UI1 */
378 public const int PROPID_M_LAST_IN_XACT
= (PROPID_M_BASE
+ 51); /* VT_UI1 */
379 public const int PROPID_M_XACTID
= (PROPID_M_BASE
+ 52); /* VT_UI1|VT_VECTOR */
380 public const int PROPID_M_AUTHENTICATED_EX
= (PROPID_M_BASE
+ 53); /* VT_UI1 */
382 public const int PROPID_M_RESP_FORMAT_NAME
= (PROPID_M_BASE
+ 54); /* VT_LPWSTR */
383 public const int PROPID_M_RESP_FORMAT_NAME_LEN
= (PROPID_M_BASE
+ 55); /* VT_UI4 */
384 public const int PROPID_M_DEST_FORMAT_NAME
= (PROPID_M_BASE
+ 58); /* VT_LPWSTR */
385 public const int PROPID_M_DEST_FORMAT_NAME_LEN
= (PROPID_M_BASE
+ 59); /* VT_UI4 */
386 public const int PROPID_M_LOOKUPID
= (PROPID_M_BASE
+ 60); /* VT_UI8 */
387 public const int PROPID_M_SOAP_ENVELOPE
= (PROPID_M_BASE
+ 61); /* VT_LPWSTR */
388 public const int PROPID_M_SOAP_ENVELOPE_LEN
= (PROPID_M_BASE
+ 62); /* VT_UI4 */
389 public const int PROPID_M_COMPOUND_MESSAGE
= (PROPID_M_BASE
+ 63); /* VT_UI1|VT_VECTOR */
390 public const int PROPID_M_COMPOUND_MESSAGE_SIZE
= (PROPID_M_BASE
+ 64); /* VT_UI4 */
391 public const int PROPID_M_SOAP_HEADER
= (PROPID_M_BASE
+ 65); /* VT_LPWSTR */
392 public const int PROPID_M_SOAP_BODY
= (PROPID_M_BASE
+ 66); /* VT_LPWSTR */
393 public const int PROPID_M_DEADLETTER_QUEUE
= (PROPID_M_BASE
+ 67); /* VT_LPWSTR */
394 public const int PROPID_M_DEADLETTER_QUEUE_LEN
= (PROPID_M_BASE
+ 68); /* VT_UI4 */
395 public const int PROPID_M_ABORT_COUNT
= (PROPID_M_BASE
+ 69); /* VT_UI4 */
396 public const int PROPID_M_MOVE_COUNT
= (PROPID_M_BASE
+ 70); /* VT_UI4 */
397 public const int PROPID_M_GROUP_ID
= (PROPID_M_BASE
+ 71); /* VT_LPWSTR */
398 public const int PROPID_M_GROUP_ID_LEN
= (PROPID_M_BASE
+ 72); /* VT_UI4 */
399 public const int PROPID_M_FIRST_IN_GROUP
= (PROPID_M_BASE
+ 73); /* VT_UI1 */
400 public const int PROPID_M_LAST_IN_GROUP
= (PROPID_M_BASE
+ 74); /* VT_UI1 */
401 public const int PROPID_M_LAST_MOVE_TIME
= (PROPID_M_BASE
+ 75); /* VT_UI4 */
403 public const int PROPID_Q_BASE
= 100;
404 public const int PROPID_Q_INSTANCE
= (PROPID_Q_BASE
+ 1); /* VT_CLSID */
405 public const int PROPID_Q_TYPE
= (PROPID_Q_BASE
+ 2); /* VT_CLSID */
406 public const int PROPID_Q_PATHNAME
= (PROPID_Q_BASE
+ 3); /* VT_LPWSTR */
407 public const int PROPID_Q_JOURNAL
= (PROPID_Q_BASE
+ 4); /* VT_UI1 */
408 public const int PROPID_Q_QUOTA
= (PROPID_Q_BASE
+ 5); /* VT_UI4 */
409 public const int PROPID_Q_BASEPRIORITY
= (PROPID_Q_BASE
+ 6); /* VT_I2 */
410 public const int PROPID_Q_JOURNAL_QUOTA
= (PROPID_Q_BASE
+ 7); /* VT_UI4 */
411 public const int PROPID_Q_LABEL
= (PROPID_Q_BASE
+ 8); /* VT_LPWSTR */
412 public const int PROPID_Q_CREATE_TIME
= (PROPID_Q_BASE
+ 9); /* VT_I4 */
413 public const int PROPID_Q_MODIFY_TIME
= (PROPID_Q_BASE
+ 10); /* VT_I4 */
414 public const int PROPID_Q_AUTHENTICATE
= (PROPID_Q_BASE
+ 11); /* VT_UI1 */
415 public const int PROPID_Q_PRIV_LEVEL
= (PROPID_Q_BASE
+ 12); /* VT_UI4 */
416 public const int PROPID_Q_TRANSACTION
= (PROPID_Q_BASE
+ 13); /* VT_UI1 */
417 public const int PROPID_Q_PATHNAME_DNS
= (PROPID_Q_BASE
+ 24); /* VT_LPWSTR */
418 public const int PROPID_Q_MULTICAST_ADDRESS
= (PROPID_Q_BASE
+ 25); /* VT_LPWSTR */
419 public const int PROPID_Q_ADS_PATH
= (PROPID_Q_BASE
+ 26); /* VT_LPWSTR */
421 public const int PROPID_PC_BASE
= 5800;
422 public const int PROPID_PC_VERSION
= (PROPID_PC_BASE
+ 1); /* VT_UI4 */
423 public const int PROPID_PC_DS_ENABLED
= (PROPID_PC_BASE
+ 2); /* VT_BOOL */
425 public const int PROPID_MGMT_QUEUE_BASE
= 0;
426 public const int PROPID_MGMT_QUEUE_SUBQUEUE_NAMES
= (PROPID_MGMT_QUEUE_BASE
+ 27); /* VT_LPWSTR|VT_VECTOR */
428 public const int MQ_TRANSACTIONAL_NONE
= 0;
429 public const int MQ_TRANSACTIONAL
= 1;
431 public const int ALG_CLASS_HASH
= (4 << 13);
432 public const int ALG_CLASS_DATA_ENCRYPT
= (3 << 13);
434 public const int ALG_TYPE_ANY
= 0;
435 public const int ALG_TYPE_STREAM
= (4 << 9);
436 public const int ALG_TYPE_BLOCK
= (3 << 9);
438 public const int ALG_SID_MD5
= 3;
439 public const int ALG_SID_SHA1
= 4;
440 public const int ALG_SID_SHA_256
= 12;
441 public const int ALG_SID_SHA_512
= 14;
443 public const int ALG_SID_RC4
= 1;
444 public const int ALG_SID_AES
= 17;
446 public const int CALG_MD5
= ALG_CLASS_HASH
| ALG_TYPE_ANY
| ALG_SID_MD5
;
447 public const int CALG_SHA1
= ALG_CLASS_HASH
| ALG_TYPE_ANY
| ALG_SID_SHA1
;
448 public const int CALG_SHA_256
= ALG_CLASS_HASH
| ALG_TYPE_ANY
| ALG_SID_SHA_256
;
449 public const int CALG_SHA_512
= ALG_CLASS_HASH
| ALG_TYPE_ANY
| ALG_SID_SHA_512
;
451 public const int CALG_RC4
= ALG_CLASS_DATA_ENCRYPT
| ALG_TYPE_STREAM
| ALG_SID_RC4
;
452 public const int CALG_AES
= ALG_CLASS_DATA_ENCRYPT
| ALG_TYPE_BLOCK
| ALG_SID_AES
;
454 public const int PROV_RSA_AES
= 24;
455 public const string MS_ENH_RSA_AES_PROV
= "Microsoft Enhanced RSA and AES Cryptographic Provider";
457 public const ushort VT_NULL
= 1;
458 public const ushort VT_BOOL
= 11;
459 public const ushort VT_UI1
= 17;
460 public const ushort VT_UI2
= 18;
461 public const ushort VT_UI4
= 19;
462 public const ushort VT_UI8
= 21;
463 public const ushort VT_LPWSTR
= 31;
464 public const ushort VT_VECTOR
= 0x1000;
466 public const uint MAX_PATH
= 260;
468 public const uint LOAD_LIBRARY_AS_DATAFILE
= 0x00000002;
469 public const uint LOAD_LIBRARY_SEARCH_SYSTEM32
= 0x00000800;
471 [StructLayout(LayoutKind
.Sequential
)]
472 internal class SECURITY_ATTRIBUTES
474 internal int nLength
= Marshal
.SizeOf(typeof(SECURITY_ATTRIBUTES
));
475 internal IntPtr lpSecurityDescriptor
= IntPtr
.Zero
;
476 internal bool bInheritHandle
= false;
479 public unsafe delegate void MQReceiveCallback(int error
, IntPtr handle
, int timeout
,
480 int action
, IntPtr props
, NativeOverlapped
* nativeOverlapped
, IntPtr cursor
);
482 [DllImport(KERNEL32
), ReliabilityContract(Consistency
.WillNotCorruptState
, Cer
.Success
)]
483 [ResourceExposure(ResourceScope
.None
)]
484 internal static extern int CloseHandle
489 [DllImport(SECUR32
), ReliabilityContract(Consistency
.WillNotCorruptState
, Cer
.Success
)]
490 [ResourceExposure(ResourceScope
.None
)]
491 internal static extern int SspiFreeAuthIdentity(
492 [In
] IntPtr ppAuthIdentity
495 [DllImport(SECUR32
), ReliabilityContract(Consistency
.WillNotCorruptState
, Cer
.Success
)]
496 [ResourceExposure(ResourceScope
.None
)]
497 internal static extern uint SspiExcludePackage(
498 [In
] IntPtr AuthIdentity
,
499 [MarshalAs(UnmanagedType
.LPWStr
)]
500 [In
] string pszPackageName
,
501 [Out
] out IntPtr ppNewAuthIdentity
);
503 [DllImport(KERNEL32
, SetLastError
= true)]
504 [ResourceExposure(ResourceScope
.None
)]
505 internal unsafe static extern int ConnectNamedPipe
508 NativeOverlapped
* lpOverlapped
511 [DllImport(KERNEL32
, CharSet
= CharSet
.Unicode
, SetLastError
= true)]
512 [ResourceExposure(ResourceScope
.Machine
)]
513 internal static extern PipeHandle CreateFile
518 IntPtr lpSECURITY_ATTRIBUTES
,
519 int dwCreationDisposition
,
520 int dwFlagsAndAttributes
,
524 [DllImport(KERNEL32
, CharSet
= CharSet
.Unicode
, SetLastError
= true)]
525 [ResourceExposure(ResourceScope
.None
)]
526 internal static extern SafeFileMappingHandle
CreateFileMapping(
528 SECURITY_ATTRIBUTES securityAttributes
,
535 [DllImport(KERNEL32
, CharSet
= CharSet
.Unicode
, SetLastError
= true)]
536 [ResourceExposure(ResourceScope
.Machine
)]
537 internal unsafe static extern PipeHandle CreateNamedPipe
546 SECURITY_ATTRIBUTES securityAttributes
549 [DllImport(KERNEL32
, SetLastError
= true)]
550 [ResourceExposure(ResourceScope
.None
)]
551 internal unsafe static extern int DisconnectNamedPipe
556 [DllImport(KERNEL32
, ExactSpelling
= true, SetLastError
= true)]
557 [ResourceExposure(ResourceScope
.None
)]
558 internal static extern bool DuplicateHandle(
559 IntPtr hSourceProcessHandle
,
560 PipeHandle hSourceHandle
,
561 SafeCloseHandle hTargetProcessHandle
,
562 out IntPtr lpTargetHandle
,
568 [DllImport(KERNEL32
, CharSet
= CharSet
.Unicode
)]
569 [ResourceExposure(ResourceScope
.None
)]
570 internal static extern int FormatMessage
576 StringBuilder lpBuffer
,
581 [DllImport(KERNEL32
, CharSet
= CharSet
.Unicode
)]
582 [ResourceExposure(ResourceScope
.None
)]
583 internal static extern int FormatMessage
586 SafeLibraryHandle lpSource
,
589 StringBuilder lpBuffer
,
594 [DllImport(KERNEL32
, SetLastError
= true)]
595 [ResourceExposure(ResourceScope
.None
)]
596 unsafe internal static extern int GetOverlappedResult
599 NativeOverlapped
* overlapped
,
600 out int bytesTransferred
,
604 // This p/invoke is for perf-sensitive codepaths which can guarantee a valid handle via custom locking.
605 [DllImport(KERNEL32
, SetLastError
= true)]
606 [ResourceExposure(ResourceScope
.None
)]
607 unsafe internal static extern int GetOverlappedResult
610 NativeOverlapped
* overlapped
,
611 out int bytesTransferred
,
615 // NOTE: a macro in win32
616 [PermissionSet(SecurityAction
.Demand
, Unrestricted
= true), SecuritySafeCritical
]
617 internal unsafe static bool HasOverlappedIoCompleted(
618 NativeOverlapped
* overlapped
)
620 return overlapped
->InternalLow
!= (IntPtr
)STATUS_PENDING
;
623 [DllImport(KERNEL32
, SetLastError
= true, CharSet
= CharSet
.Unicode
)]
624 [ResourceExposure(ResourceScope
.Machine
)]
625 internal static extern SafeFileMappingHandle OpenFileMapping
632 [DllImport(KERNEL32
, SetLastError
= true)]
633 [ResourceExposure(ResourceScope
.None
)]
634 internal static extern SafeViewOfFileHandle MapViewOfFile
636 SafeFileMappingHandle handle
,
638 int dwFileOffsetHigh
,
640 IntPtr dwNumberOfBytesToMap
643 [DllImport(KERNEL32
, SetLastError
= true)]
644 [ResourceExposure(ResourceScope
.None
)]
645 [SuppressUnmanagedCodeSecurity
, HostProtection(SecurityAction
.LinkDemand
)]
646 public static extern int QueryPerformanceCounter(out long time
);
648 // This p/invoke is for perf-sensitive codepaths which can guarantee a valid handle via custom locking.
649 [DllImport(KERNEL32
, SetLastError
= true)]
650 [ResourceExposure(ResourceScope
.None
)]
651 unsafe internal static extern int ReadFile
656 IntPtr numBytesRead_mustBeZero
,
657 NativeOverlapped
* overlapped
660 [DllImport(KERNEL32
, SetLastError
= true)]
661 [ResourceExposure(ResourceScope
.None
)]
662 internal static extern int SetNamedPipeHandleState
666 IntPtr collectionCount
,
667 IntPtr collectionDataTimeout
670 // This p/invoke is for perf-sensitive codepaths which can guarantee a valid handle via custom locking.
671 [DllImport(KERNEL32
, SetLastError
= true)]
672 [ResourceExposure(ResourceScope
.None
)]
673 internal static unsafe extern int WriteFile
678 IntPtr numBytesWritten_mustBeZero
,
679 NativeOverlapped
* lpOverlapped
682 [DllImport(KERNEL32
, SetLastError
= true)]
683 [ResourceExposure(ResourceScope
.None
)]
684 internal static unsafe extern bool GetNamedPipeClientProcessId(PipeHandle handle
, out int id
);
686 [DllImport(KERNEL32
, SetLastError
= true)]
687 [ResourceExposure(ResourceScope
.None
)]
688 internal static unsafe extern bool GetNamedPipeServerProcessId(PipeHandle handle
, out int id
);
690 [DllImport(KERNEL32
, ExactSpelling
= true),
691 ReliabilityContract(Consistency
.WillNotCorruptState
, Cer
.Success
)]
692 [ResourceExposure(ResourceScope
.None
)]
693 internal static extern int UnmapViewOfFile
698 [DllImport(KERNEL32
, ExactSpelling
= true)]
699 [ResourceExposure(ResourceScope
.None
)]
700 public static extern bool SetWaitableTimer(SafeWaitHandle handle
, ref long dueTime
, int period
, IntPtr mustBeZero
, IntPtr mustBeZeroAlso
, bool resume
);
702 [DllImport(KERNEL32
, BestFitMapping
= false, CharSet
= CharSet
.Auto
)]
703 [ResourceExposure(ResourceScope
.Machine
)]
704 public static extern SafeWaitHandle
CreateWaitableTimer(IntPtr mustBeZero
, bool manualReset
, string timerName
);
707 [DllImport(WS2_32
, SetLastError
= true)]
708 [ResourceExposure(ResourceScope
.None
)]
709 internal static unsafe extern int WSARecv
711 IntPtr handle
, WSABuffer
* buffers
, int bufferCount
, out int bytesTransferred
,
713 NativeOverlapped
* nativeOverlapped
,
714 IntPtr completionRoutine
717 [DllImport(WS2_32
, SetLastError
= true)]
718 [ResourceExposure(ResourceScope
.None
)]
719 internal static unsafe extern bool WSAGetOverlappedResult(
721 NativeOverlapped
* overlapped
,
722 out int bytesTransferred
,
727 [StructLayout(LayoutKind
.Sequential
)]
728 internal struct WSABuffer
731 public IntPtr buffer
;
735 internal static string GetComputerName(ComputerNameFormat nameType
)
737 return System
.Runtime
.Interop
.UnsafeNativeMethods
.GetComputerName(nameType
);
741 [DllImport(USERENV
, SetLastError
= true)]
742 internal static extern int DeriveAppContainerSidFromAppContainerName
744 [In
, MarshalAs(UnmanagedType
.LPWStr
)] string appContainerName
,
745 out IntPtr appContainerSid
748 [DllImport(ADVAPI32
, SetLastError
= true)]
749 internal static extern IntPtr FreeSid
754 // If the function succeeds, the return value is ERROR_SUCCESS and 'packageFamilyNameLength' contains the size of the data copied
755 // to 'packageFamilyName' (in WCHARs, including the null-terminator). If the function fails, the return value is a Win32 error code.
756 [DllImport(KERNEL32
)]
757 internal static extern int PackageFamilyNameFromFullName
759 [In
, MarshalAs(UnmanagedType
.LPWStr
)] string packageFullName
,
760 ref uint packageFamilyNameLength
,
761 [In
, Out
, MarshalAs(UnmanagedType
.LPWStr
)] StringBuilder packageFamilyName
764 [DllImport(KERNEL32
, SetLastError
= true)]
765 internal static extern bool GetAppContainerNamedObjectPath
768 IntPtr appContainerSid
,
769 uint objectPathLength
,
770 [In
, Out
, MarshalAs(UnmanagedType
.LPWStr
)] StringBuilder objectPath
,
771 ref uint returnLength
774 [DllImport(KERNEL32
, SetLastError
= true)]
775 internal static extern IntPtr
GetCurrentProcess();
777 [DllImport(ADVAPI32
, SetLastError
= true)]
778 [ResourceExposure(ResourceScope
.None
)]
779 internal static extern bool OpenProcessToken
781 IntPtr ProcessHandle
,
782 TokenAccessLevels DesiredAccess
,
783 out SafeCloseHandle TokenHandle
786 // Token marshalled as byte[]
787 [DllImport(ADVAPI32
, SetLastError
= true)]
788 static extern unsafe bool GetTokenInformation
790 SafeCloseHandle tokenHandle
,
791 TOKEN_INFORMATION_CLASS tokenInformationClass
,
792 byte[] tokenInformation
,
793 uint tokenInformationLength
,
794 out uint returnLength
797 // Token marshalled as uint
798 [DllImport(ADVAPI32
, SetLastError
= true)]
799 static extern bool GetTokenInformation
801 SafeCloseHandle tokenHandle
,
802 TOKEN_INFORMATION_CLASS tokenInformationClass
,
803 out uint tokenInformation
,
804 uint tokenInformationLength
,
805 out uint returnLength
808 internal static unsafe SecurityIdentifier
GetAppContainerSid(SafeCloseHandle tokenHandle
)
810 // Get length of buffer needed for sid.
811 uint returnLength
= UnsafeNativeMethods
.GetTokenInformationLength(
813 TOKEN_INFORMATION_CLASS
.TokenAppContainerSid
);
815 byte[] tokenInformation
= new byte[returnLength
];
816 fixed (byte* pTokenInformation
= tokenInformation
)
818 if (!UnsafeNativeMethods
.GetTokenInformation(
820 TOKEN_INFORMATION_CLASS
.TokenAppContainerSid
,
825 int errorCode
= Marshal
.GetLastWin32Error();
826 throw FxTrace
.Exception
.AsError(new Win32Exception(errorCode
));
829 TokenAppContainerInfo
* ptg
= (TokenAppContainerInfo
*)pTokenInformation
;
830 return new SecurityIdentifier(ptg
->psid
);
834 [StructLayout(LayoutKind
.Sequential
)]
835 struct TokenAppContainerInfo
840 static uint GetTokenInformationLength(SafeCloseHandle token
, TOKEN_INFORMATION_CLASS tokenInformationClass
)
844 if (!(success
= GetTokenInformation(
846 tokenInformationClass
,
851 int error
= Marshal
.GetLastWin32Error();
852 if (error
!= UnsafeNativeMethods
.ERROR_INSUFFICIENT_BUFFER
)
854 throw FxTrace
.Exception
.AsError(new Win32Exception(error
));
858 Fx
.Assert(!success
, "Retreving the length should always fail.");
863 internal static int GetSessionId(SafeCloseHandle tokenHandle
)
868 if (!UnsafeNativeMethods
.GetTokenInformation(
870 TOKEN_INFORMATION_CLASS
.TokenSessionId
,
875 int errorCode
= Marshal
.GetLastWin32Error();
876 throw FxTrace
.Exception
.AsError(new Win32Exception(errorCode
));
879 return (int)sessionId
;
882 internal static bool RunningInAppContainer(SafeCloseHandle tokenHandle
)
884 uint runningInAppContainer
;
886 if (!UnsafeNativeMethods
.GetTokenInformation(
888 TOKEN_INFORMATION_CLASS
.TokenIsAppContainer
,
889 out runningInAppContainer
,
893 int errorCode
= Marshal
.GetLastWin32Error();
894 throw FxTrace
.Exception
.AsError(new Win32Exception(errorCode
));
897 return runningInAppContainer
== 1;
900 [StructLayout(LayoutKind
.Sequential
)]
901 public class MQMSGPROPS
905 public IntPtr variants
;
906 public IntPtr status
;
909 [StructLayout(LayoutKind
.Explicit
)]
910 public struct MQPROPVARIANT
915 public ushort reserved1
;
917 public ushort reserved2
;
919 public ushort reserved3
;
921 public byte byteValue
;
923 public short shortValue
;
927 public long longValue
;
929 public IntPtr intPtr
;
931 public CAUI1 byteArrayValue
;
933 public CALPWSTR stringArraysValue
;
935 [StructLayout(LayoutKind
.Sequential
)]
939 public IntPtr intPtr
;
942 [StructLayout(LayoutKind
.Sequential
)]
943 public struct CALPWSTR
946 public IntPtr stringArrays
;
950 [DllImport(MQRT
, CharSet
= CharSet
.Unicode
)]
951 [ResourceExposure(ResourceScope
.Machine
)]
952 public static extern int MQOpenQueue(string formatName
, int access
, int shareMode
, out MsmqQueueHandle handle
);
954 [DllImport(MQRT
, CharSet
= CharSet
.Unicode
)]
955 [ResourceExposure(ResourceScope
.None
)]
956 public static extern int MQBeginTransaction(out ITransaction refTransaction
);
958 [DllImport(MQRT
, CharSet
= CharSet
.Unicode
)]
959 [ReliabilityContract(Consistency
.WillNotCorruptState
, Cer
.Success
)]
960 [ResourceExposure(ResourceScope
.None
)]
961 public static extern int MQCloseQueue(IntPtr handle
);
963 [DllImport(MQRT
, CharSet
= CharSet
.Unicode
)]
964 [ResourceExposure(ResourceScope
.None
)]
965 public static extern int MQSendMessage(MsmqQueueHandle handle
, IntPtr properties
, IntPtr transaction
);
967 [DllImport(MQRT
, CharSet
= CharSet
.Unicode
)]
968 [ResourceExposure(ResourceScope
.None
)]
969 public static extern int MQSendMessage(MsmqQueueHandle handle
, IntPtr properties
, IDtcTransaction transaction
);
971 [DllImport(MQRT
, CharSet
= CharSet
.Unicode
)]
972 [ResourceExposure(ResourceScope
.None
)]
973 public unsafe static extern int MQReceiveMessage(MsmqQueueHandle handle
, int timeout
, int action
, IntPtr properties
,
974 NativeOverlapped
* nativeOverlapped
, IntPtr receiveCallback
, IntPtr cursorHandle
, IntPtr transaction
);
976 [DllImport(MQRT
, CharSet
= CharSet
.Unicode
)]
977 [ResourceExposure(ResourceScope
.None
)]
978 public unsafe static extern int MQReceiveMessage(IntPtr handle
, int timeout
, int action
, IntPtr properties
,
979 NativeOverlapped
* nativeOverlapped
, IntPtr receiveCallback
, IntPtr cursorHandle
, IntPtr transaction
);
981 [DllImport(MQRT
, CharSet
= CharSet
.Unicode
)]
982 [ResourceExposure(ResourceScope
.None
)]
983 public unsafe static extern int MQReceiveMessage(MsmqQueueHandle handle
, int timeout
, int action
, IntPtr properties
,
984 NativeOverlapped
* nativeOverlapped
, IntPtr receiveCallback
, IntPtr cursorHandle
, IDtcTransaction transaction
);
986 [DllImport(MQRT
, CharSet
= CharSet
.Unicode
)]
987 [ResourceExposure(ResourceScope
.None
)]
988 public unsafe static extern int MQReceiveMessage(IntPtr handle
, int timeout
, int action
, IntPtr properties
,
989 NativeOverlapped
* nativeOverlapped
, IntPtr receiveCallback
, IntPtr cursorHandle
, IDtcTransaction transaction
);
991 [DllImport(MQRT
, CharSet
= CharSet
.Unicode
)]
992 [ResourceExposure(ResourceScope
.None
)]
993 public unsafe static extern int MQReceiveMessage(MsmqQueueHandle handle
, int timeout
, int action
, IntPtr properties
,
994 NativeOverlapped
* nativeOverlapped
, MQReceiveCallback receiveCallback
, IntPtr cursorHandle
, IntPtr transaction
);
996 [DllImport(MQRT
, CharSet
= CharSet
.Unicode
)]
997 [ResourceExposure(ResourceScope
.None
)]
998 public unsafe static extern int MQReceiveMessage(IntPtr handle
, int timeout
, int action
, IntPtr properties
,
999 NativeOverlapped
* nativeOverlapped
, IntPtr receiveCallback
, IntPtr cursorHandle
, ITransaction transaction
);
1001 [DllImport(MQRT
, CharSet
= CharSet
.Unicode
)]
1002 [ResourceExposure(ResourceScope
.None
)]
1003 public unsafe static extern int MQReceiveMessageByLookupId(MsmqQueueHandle handle
, long lookupId
, int action
,
1004 IntPtr properties
, NativeOverlapped
* nativeOverlapped
, IntPtr receiveCallback
, IDtcTransaction transaction
);
1006 [DllImport(MQRT
, CharSet
= CharSet
.Unicode
)]
1007 [ResourceExposure(ResourceScope
.None
)]
1008 public unsafe static extern int MQReceiveMessageByLookupId(MsmqQueueHandle handle
, long lookupId
, int action
,
1009 IntPtr properties
, NativeOverlapped
* nativeOverlapped
, IntPtr receiveCallback
, IntPtr transaction
);
1011 [DllImport(MQRT
, CharSet
= CharSet
.Unicode
)]
1012 [ResourceExposure(ResourceScope
.None
)]
1013 public unsafe static extern int MQReceiveMessageByLookupId(MsmqQueueHandle handle
, long lookupId
, int action
,
1014 IntPtr properties
, NativeOverlapped
* nativeOverlapped
, IntPtr receiveCallback
, ITransaction transaction
);
1016 [DllImport(MQRT
, CharSet
= CharSet
.Unicode
)]
1017 [ResourceExposure(ResourceScope
.None
)]
1018 public unsafe static extern int MQGetPrivateComputerInformation(string computerName
, IntPtr properties
);
1020 [DllImport(MQRT
, CharSet
= CharSet
.Unicode
)]
1021 [ResourceExposure(ResourceScope
.None
)]
1022 public unsafe static extern int MQMarkMessageRejected(MsmqQueueHandle handle
, long lookupId
);
1024 [DllImport(MQRT
, CharSet
= CharSet
.Unicode
)]
1025 [ResourceExposure(ResourceScope
.None
)]
1026 public static extern int MQMoveMessage(MsmqQueueHandle sourceQueueHandle
,
1027 MsmqQueueHandle destinationQueueHandle
,
1029 IntPtr transaction
);
1031 [DllImport(MQRT
, CharSet
= CharSet
.Unicode
)]
1032 [ResourceExposure(ResourceScope
.None
)]
1033 public static extern int MQMoveMessage(MsmqQueueHandle sourceQueueHandle
,
1034 MsmqQueueHandle destinationQueueHandle
,
1036 IDtcTransaction transaction
);
1038 [DllImport(MQRT
, CharSet
= CharSet
.Unicode
)]
1039 [ResourceExposure(ResourceScope
.None
)]
1040 public unsafe static extern int MQGetOverlappedResult(NativeOverlapped
* nativeOverlapped
);
1042 [DllImport(MQRT
, CharSet
= CharSet
.Unicode
)]
1043 [ResourceExposure(ResourceScope
.None
)]
1044 public unsafe static extern int MQGetQueueProperties(string formatName
, IntPtr properties
);
1046 [DllImport(MQRT
, CharSet
= CharSet
.Unicode
)]
1047 [ResourceExposure(ResourceScope
.None
)]
1048 public unsafe static extern int MQPathNameToFormatName(string pathName
, StringBuilder formatName
, ref int count
);
1050 [DllImport(MQRT
, CharSet
= CharSet
.Unicode
)]
1051 [ResourceExposure(ResourceScope
.None
)]
1052 public unsafe static extern int MQMgmtGetInfo(string computerName
, string objectName
, IntPtr properties
);
1054 [DllImport(MQRT
, CharSet
= CharSet
.Unicode
)]
1055 [ResourceExposure(ResourceScope
.None
)]
1056 public unsafe static extern void MQFreeMemory(IntPtr nativeBuffer
);
1058 [DllImport(KERNEL32
, SetLastError
= true)]
1059 [ResourceExposure(ResourceScope
.None
)]
1060 public unsafe static extern int GetHandleInformation(MsmqQueueHandle handle
, out int flags
);
1062 [StructLayout(LayoutKind
.Sequential
)]
1063 public struct MEMORYSTATUSEX
1065 public uint dwLength
;
1066 public uint dwMemoryLoad
;
1067 public ulong ullTotalPhys
;
1068 public ulong ullAvailPhys
;
1069 public ulong ullTotalPageFile
;
1070 public ulong ullAvailPageFile
;
1071 public ulong ullTotalVirtual
;
1072 public ulong ullAvailVirtual
;
1073 public ulong ullAvailExtendedVirtual
;
1076 [DllImport(KERNEL32
, SetLastError
= true)]
1077 [ResourceExposure(ResourceScope
.None
)]
1078 public static extern bool GlobalMemoryStatusEx(ref MEMORYSTATUSEX lpBuffer
);
1080 [ReliabilityContract(Consistency
.WillNotCorruptState
, Cer
.Success
)]
1081 [DllImport(KERNEL32
, CallingConvention
= CallingConvention
.StdCall
, SetLastError
= true)]
1082 [ResourceExposure(ResourceScope
.None
)]
1083 internal static extern IntPtr
VirtualAlloc(IntPtr lpAddress
, UIntPtr dwSize
, uint flAllocationType
, uint flProtect
);
1085 [ReliabilityContract(Consistency
.WillNotCorruptState
, Cer
.Success
)]
1086 [DllImport(KERNEL32
, CallingConvention
= CallingConvention
.StdCall
, SetLastError
= true)]
1087 [ResourceExposure(ResourceScope
.None
)]
1088 internal static extern bool VirtualFree(IntPtr lpAddress
, UIntPtr dwSize
, uint dwFreeType
);
1090 [DllImport(KERNEL32
, CharSet
= CharSet
.Auto
)]
1091 [ResourceExposure(ResourceScope
.None
)]
1092 internal static extern IntPtr
GetProcAddress(SafeLibraryHandle hModule
, [In
, MarshalAs(UnmanagedType
.LPStr
)]string lpProcName
);
1094 [DllImport(KERNEL32
, CharSet
= CharSet
.Unicode
, SetLastError
= true)]
1095 [ResourceExposure(ResourceScope
.Process
)]
1096 internal static extern SafeLibraryHandle
LoadLibrary(string libFilename
);
1098 [DllImport(KERNEL32
, CharSet
= CharSet
.Auto
, SetLastError
= true)]
1099 [ResourceExposure(ResourceScope
.Process
)]
1100 internal static extern SafeLibraryHandle
LoadLibraryEx(string lpModuleName
, IntPtr hFile
, uint dwFlags
);
1102 // On Vista and higher, check the value of the machine FIPS policy
1103 [DllImport(BCRYPT
, SetLastError
= true)]
1104 [ResourceExposure(ResourceScope
.None
)]
1105 internal static extern int BCryptGetFipsAlgorithmMode(
1106 [MarshalAs(UnmanagedType
.U1
), Out
] out bool pfEnabled
1109 #if !FEATURE_CORECLR
1110 private static IntPtr
GetCurrentProcessToken() { return new IntPtr(-4); }
1112 enum AppPolicyClrCompat
1114 AppPolicyClrCompat_Others
= 0,
1115 AppPolicyClrCompat_ClassicDesktop
= 1,
1116 AppPolicyClrCompat_Universal
= 2,
1117 AppPolicyClrCompat_PackagedDesktop
= 3
1120 [DllImport(KERNEL32
, CharSet
= CharSet
.None
, EntryPoint
= "AppPolicyGetClrCompat")]
1121 [System
.Security
.SecuritySafeCritical
]
1122 [return: MarshalAs(UnmanagedType
.I4
)]
1123 private static extern Int32
_AppPolicyGetClrCompat(IntPtr processToken
, out AppPolicyClrCompat appPolicyClrCompat
);
1125 // AppModel.h functions (Win8+)
1126 [DllImport(KERNEL32
, CharSet
= CharSet
.None
, EntryPoint
= "GetCurrentPackageId")]
1127 [System
.Security
.SecuritySafeCritical
]
1128 [return: MarshalAs(UnmanagedType
.I4
)]
1129 private static extern Int32
_GetCurrentPackageId(ref Int32 pBufferLength
, Byte
[] pBuffer
);
1131 [DllImport(KERNEL32
, CharSet
=System
.Runtime
.InteropServices
.CharSet
.Auto
, BestFitMapping
=false)]
1132 [ResourceExposure(ResourceScope
.Machine
)]
1133 private static extern IntPtr
GetModuleHandle(string modName
);
1135 // Copied from Win32Native.cs
1136 // Note - do NOT use this to call methods. Use P/Invoke, which will
1137 // do much better things w.r.t. marshaling, pinning memory, security
1138 // stuff, better interactions with thread aborts, etc. This is used
1139 // solely by DoesWin32MethodExist for avoiding try/catch EntryPointNotFoundException
1140 // in scenarios where an OS Version check is insufficient
1141 [DllImport(KERNEL32
, CharSet
=CharSet
.Ansi
, BestFitMapping
=false, SetLastError
=true, ExactSpelling
=true)]
1142 [ResourceExposure(ResourceScope
.None
)]
1143 private static extern IntPtr
GetProcAddress(IntPtr hModule
, String methodName
);
1145 [System
.Security
.SecurityCritical
] // auto-generated
1146 private static bool DoesWin32MethodExist(String moduleName
, String methodName
)
1148 // GetModuleHandle does not increment the module's ref count, so we don't need to call FreeLibrary.
1149 IntPtr hModule
= GetModuleHandle(moduleName
);
1150 if (hModule
== IntPtr
.Zero
) {
1151 System
.Diagnostics
.Debug
.Assert(hModule
!= IntPtr
.Zero
, "GetModuleHandle failed. Dll isn't loaded?");
1154 IntPtr functionPointer
= GetProcAddress(hModule
, methodName
);
1155 return (functionPointer
!= IntPtr
.Zero
);
1158 // On CoreCLR this is not the way to determine if a process is a tailored application (which means APPX).
1159 // On CoreCLR AppX is determined by a flag past to the host which is exposed by AppDomain.IsAppXProcess in mscorlib.
1160 // The reason for this if-def is to ensure nobody takes a dependency on this on CoreCLR.
1161 [System
.Security
.SecuritySafeCritical
]
1162 private static bool _IsTailoredApplication()
1164 Version windows8Version
= new Version(6, 2, 0, 0);
1165 OperatingSystem os
= Environment
.OSVersion
;
1166 bool osSupportsPackagedProcesses
= os
.Platform
== PlatformID
.Win32NT
&& os
.Version
>= windows8Version
;
1168 if (osSupportsPackagedProcesses
&& DoesWin32MethodExist(KERNEL32
, "AppPolicyGetClrCompat"))
1170 // Use AppPolicyGetClrCompat if it is available. Return true if and only if this is a UWA which means if
1171 // this is packaged desktop app this method will return false. This may cause some confusion however
1172 // this is necessary to make the behavior of packaged desktop apps identical to desktop apps.
1173 AppPolicyClrCompat appPolicyClrCompat
;
1174 return _AppPolicyGetClrCompat(GetCurrentProcessToken(), out appPolicyClrCompat
) == ERROR_SUCCESS
&&
1175 appPolicyClrCompat
== AppPolicyClrCompat
.AppPolicyClrCompat_Universal
;
1177 else if(osSupportsPackagedProcesses
&& DoesWin32MethodExist(KERNEL32
, "GetCurrentPackageId"))
1180 // Will return ERROR_INSUFFICIENT_BUFFER when running within a packaged application,
1181 // and will return ERROR_NO_PACKAGE_IDENTITY otherwise.
1182 return _GetCurrentPackageId(ref bufLen
, null) == ERROR_INSUFFICIENT_BUFFER
;
1185 { // We must be running on a downlevel OS.
1191 /// Indicates weather the running application is an immersive (or modern) Windows 8 (or later) application.
1193 internal static Lazy
<bool> IsTailoredApplication
= new Lazy
<bool>(() => _IsTailoredApplication());
1194 #endif //!FEATURE_CORECLR
1197 [SuppressUnmanagedCodeSecurity
]
1198 class PipeHandle
: SafeHandleMinusOneIsInvalid
1200 internal PipeHandle() : base(true) { }
1202 // This is unsafe, but is useful for a duplicated handle, which is inherently unsafe already.
1203 internal PipeHandle(IntPtr handle
)
1209 internal int GetClientPid()
1212 #pragma warning suppress 56523 // Microsoft, Win32Exception ctor calls Marshal.GetLastWin32Error()
1213 bool success
= UnsafeNativeMethods
.GetNamedPipeClientProcessId(this, out pid
);
1216 throw DiagnosticUtility
.ExceptionUtility
.ThrowHelperError(new Win32Exception());
1221 protected override bool ReleaseHandle()
1223 return UnsafeNativeMethods
.CloseHandle(handle
) != 0;
1227 [SuppressUnmanagedCodeSecurityAttribute()]
1228 sealed class SafeFileMappingHandle
: SafeHandleZeroOrMinusOneIsInvalid
1230 internal SafeFileMappingHandle()
1235 override protected bool ReleaseHandle()
1237 return UnsafeNativeMethods
.CloseHandle(handle
) != 0;
1241 [SuppressUnmanagedCodeSecurityAttribute
]
1242 sealed class SafeLibraryHandle
: SafeHandleZeroOrMinusOneIsInvalid
1244 bool doNotfreeLibraryOnRelease
;
1246 internal SafeLibraryHandle()
1249 doNotfreeLibraryOnRelease
= false;
1252 public void DoNotFreeLibraryOnRelease()
1254 this.doNotfreeLibraryOnRelease
= true;
1257 [DllImport(UnsafeNativeMethods
.KERNEL32
, CharSet
= CharSet
.Unicode
)]
1258 [ReliabilityContract(Consistency
.WillNotCorruptState
, Cer
.Success
)]
1259 private static extern bool FreeLibrary(IntPtr hModule
);
1261 override protected bool ReleaseHandle()
1263 if (doNotfreeLibraryOnRelease
)
1265 handle
= IntPtr
.Zero
;
1269 return FreeLibrary(handle
);
1273 [SuppressUnmanagedCodeSecurityAttribute
]
1274 sealed class SafeViewOfFileHandle
: SafeHandleZeroOrMinusOneIsInvalid
1276 internal SafeViewOfFileHandle()
1281 override protected bool ReleaseHandle()
1283 if (UnsafeNativeMethods
.UnmapViewOfFile(handle
) != 0)
1285 handle
= IntPtr
.Zero
;
1292 [SuppressUnmanagedCodeSecurity
]
1293 sealed class MsmqQueueHandle
: SafeHandleZeroOrMinusOneIsInvalid
1295 internal MsmqQueueHandle() : base(true) { }
1297 protected override bool ReleaseHandle()
1299 return UnsafeNativeMethods
.MQCloseQueue(handle
) >= 0;