1 //------------------------------------------------------------------------------
2 // <copyright file="IISUnsafeMethods.cs" company="Microsoft">
3 //     Copyright (c) Microsoft Corporation.  All rights reserved.
4 // </copyright>
5 //------------------------------------------------------------------------------
6 
7 namespace System.Web.Hosting {
8     using System;
9     using System.Configuration;
10     using System.Text;
11     using System.Runtime.InteropServices;
12     using System.Diagnostics.CodeAnalysis;
13 
14     [
15     System.Runtime.InteropServices.ComVisible(false),
16     System.Security.SuppressUnmanagedCodeSecurityAttribute()
17     ]
18     // contains only method decls and data, so no instantiation
19     internal unsafe static class UnsafeIISMethods {
20 
21         const string _IIS_NATIVE_DLL = ModName.MGDENG_FULL_NAME;
22         static internal readonly IntPtr INVALID_HANDLE_VALUE = new IntPtr(-1);
23 
24         [DllImport(_IIS_NATIVE_DLL)]
MgdGetRequestBasics( IntPtr pRequestContext, out int pContentType, out int pContentTotalLength, out IntPtr pPathTranslated, out int pcchPathTranslated, out IntPtr pCacheUrl, out int pcchCacheUrl, out IntPtr pHttpMethod, out IntPtr pCookedUrl)25         internal unsafe static extern int MgdGetRequestBasics(
26             IntPtr           pRequestContext,
27             out int          pContentType,
28             out int          pContentTotalLength,
29             out IntPtr       pPathTranslated,
30             out int          pcchPathTranslated,
31             out IntPtr       pCacheUrl,
32             out int          pcchCacheUrl,
33             out IntPtr       pHttpMethod,
34             out IntPtr       pCookedUrl);
35 
36         [DllImport(_IIS_NATIVE_DLL)]
MgdGetHeaderChanges( IntPtr pRequestContext, bool fResponse, out IntPtr knownHeaderSnapshot, out int unknownHeaderSnapshotCount, out IntPtr unknownHeaderSnapshotNames, out IntPtr unknownHeaderSnapshotValues, out IntPtr diffKnownIndicies, out int diffUnknownCount, out IntPtr diffUnknownIndicies)37         internal static extern int MgdGetHeaderChanges(
38             IntPtr           pRequestContext,
39             bool             fResponse,
40             out IntPtr       knownHeaderSnapshot,
41             out int          unknownHeaderSnapshotCount,
42             out IntPtr       unknownHeaderSnapshotNames,
43             out IntPtr       unknownHeaderSnapshotValues,
44             out IntPtr       diffKnownIndicies,
45             out int          diffUnknownCount,
46             out IntPtr       diffUnknownIndicies);
47 
48         [DllImport(_IIS_NATIVE_DLL)]
MgdGetServerVarChanges( IntPtr pRequestContext, out int count, out IntPtr names, out IntPtr values, out int diffCount, out IntPtr diffIndicies)49         internal static extern int MgdGetServerVarChanges(
50             IntPtr           pRequestContext,
51             out int          count,
52             out IntPtr       names,
53             out IntPtr       values,
54             out int          diffCount,
55             out IntPtr       diffIndicies);
56 
57         [DllImport(_IIS_NATIVE_DLL, CharSet=CharSet.Unicode)]
MgdGetServerVariableW( IntPtr pHandler, string pszVarName, out IntPtr ppBuffer, out int pcchBufferSize)58         internal static extern int MgdGetServerVariableW(
59             IntPtr           pHandler,
60             string           pszVarName,
61             out IntPtr       ppBuffer,
62             out int          pcchBufferSize);
63 
64         [DllImport(_IIS_NATIVE_DLL, CharSet=CharSet.Unicode)]
MgdGetServerVariableA( IntPtr pHandler, string pszVarName, out IntPtr ppBuffer, out int pcchBufferSize)65         internal static extern int MgdGetServerVariableA(
66             IntPtr           pHandler,
67             string           pszVarName,
68             out IntPtr       ppBuffer,
69             out int          pcchBufferSize);
70 
71         [DllImport(_IIS_NATIVE_DLL)]
MgdGetStopListeningEventHandle()72         internal static extern IntPtr MgdGetStopListeningEventHandle();
73 
74         [DllImport(_IIS_NATIVE_DLL)]
MgdSetBadRequestStatus( IntPtr pHandler)75         internal static extern void MgdSetBadRequestStatus(
76             IntPtr           pHandler);
77 
78         [DllImport(_IIS_NATIVE_DLL)]
MgdSetManagedHttpContext( IntPtr pHandler, IntPtr pManagedHttpContext)79         internal static extern void MgdSetManagedHttpContext(
80             IntPtr           pHandler,
81             IntPtr           pManagedHttpContext);
82 
83         [DllImport(_IIS_NATIVE_DLL, CharSet=CharSet.Unicode)]
MgdSetStatusW( IntPtr pRequestContext, int dwStatusCode, int dwSubStatusCode, string pszReason, string pszErrorDescription , bool fTrySkipCustomErrors)84         internal static extern int MgdSetStatusW(
85             IntPtr           pRequestContext,
86             int              dwStatusCode,
87             int              dwSubStatusCode,
88             string           pszReason,
89             string           pszErrorDescription /* optional, can be null */,
90             bool             fTrySkipCustomErrors);
91 
92         [DllImport(_IIS_NATIVE_DLL)]
MgdSetKnownHeader( IntPtr pRequestContext, bool fRequest, bool fReplace, ushort uHeaderIndex, byte[] value, ushort valueSize)93         internal static extern int MgdSetKnownHeader(
94             IntPtr           pRequestContext,
95             bool             fRequest,
96             bool             fReplace,
97             ushort           uHeaderIndex,
98             byte[]           value,
99             ushort           valueSize);
100 
101         [DllImport(_IIS_NATIVE_DLL)]
MgdSetUnknownHeader( IntPtr pRequestContext, bool fRequest, bool fReplace, byte [] header, byte [] value, ushort valueSize)102         internal static extern int MgdSetUnknownHeader(
103             IntPtr           pRequestContext,
104             bool             fRequest,
105             bool             fReplace,
106             byte []          header,
107             byte []          value,
108             ushort           valueSize);
109 
110         [DllImport(_IIS_NATIVE_DLL)]
MgdFlushCore( IntPtr pRequestContext, bool keepConnected, int numBodyFragments, IntPtr[] bodyFragments, int[] bodyFragmentLengths, int[] fragmentsNative)111         internal static extern int MgdFlushCore(
112             IntPtr    pRequestContext,
113             bool      keepConnected,
114             int       numBodyFragments,
115             IntPtr[]  bodyFragments,
116             int[]     bodyFragmentLengths,
117             int[]     fragmentsNative);
118 
119         [DllImport(_IIS_NATIVE_DLL)]
MgdSetKernelCachePolicy( IntPtr pHandler, int secondsToLive)120         internal static extern int MgdSetKernelCachePolicy(
121             IntPtr    pHandler,
122             int       secondsToLive);
123 
124         [DllImport(_IIS_NATIVE_DLL, CharSet=CharSet.Unicode)]
MgdFlushKernelCache( string cacheKey)125         internal static extern int MgdFlushKernelCache(
126             string    cacheKey);
127 
128         [DllImport(_IIS_NATIVE_DLL)]
MgdDisableKernelCache( IntPtr pHandler)129         internal static extern void MgdDisableKernelCache(
130             IntPtr    pHandler);
131 
132         [DllImport(_IIS_NATIVE_DLL)]
MgdDisableUserCache( IntPtr pHandler)133         internal static extern void MgdDisableUserCache(
134             IntPtr pHandler);
135 
136         [DllImport(_IIS_NATIVE_DLL, CharSet=CharSet.Unicode)]
MgdRegisterEventSubscription( IntPtr pAppContext, string pszModuleName, [MarshalAs(UnmanagedType.U4)] RequestNotification requestNotifications, [MarshalAs(UnmanagedType.U4)] RequestNotification postRequestNotifications, string pszModuleType, string pszModulePrecondition, IntPtr moduleSpecificData, bool useHighPriority)137         internal static extern int MgdRegisterEventSubscription(
138             IntPtr           pAppContext,
139             string           pszModuleName,
140             [MarshalAs(UnmanagedType.U4)]
141             RequestNotification requestNotifications,
142             [MarshalAs(UnmanagedType.U4)]
143             RequestNotification postRequestNotifications,
144             string           pszModuleType,
145             string           pszModulePrecondition,
146             IntPtr           moduleSpecificData,
147             bool             useHighPriority);
148 
149         [DllImport(_IIS_NATIVE_DLL)]
MgdIndicateCompletion( IntPtr pHandler, [MarshalAs(UnmanagedType.U4)] ref RequestNotificationStatus notificationStatus )150         internal static extern void MgdIndicateCompletion(
151             IntPtr           pHandler,
152             [MarshalAs(UnmanagedType.U4)]
153             ref RequestNotificationStatus notificationStatus );
154 
155         [DllImport(_IIS_NATIVE_DLL)]
MgdInsertEntityBody( IntPtr pHandler, byte[] buffer, int offset, int count)156         internal static extern int MgdInsertEntityBody(
157             IntPtr           pHandler,
158             byte[]           buffer,
159             int              offset,
160             int              count);
161 
162         [DllImport(_IIS_NATIVE_DLL, CharSet=CharSet.Unicode)]
MgdPostCompletion( IntPtr pHandler, [MarshalAs(UnmanagedType.U4)] RequestNotificationStatus notificationStatus )163         internal static extern int MgdPostCompletion(
164             IntPtr           pHandler,
165             [MarshalAs(UnmanagedType.U4)]
166             RequestNotificationStatus notificationStatus );
167 
168         [DllImport(_IIS_NATIVE_DLL, CharSet=CharSet.Unicode)]
MgdReadEntityBody( IntPtr pHandler, byte[] pBuffer, int dwOffset, int dwBytesToRead, bool fAsync, out int pBytesRead, out IntPtr ppAsyncReceiveBuffer )169         internal static extern int MgdReadEntityBody(
170             IntPtr           pHandler,
171             byte[]           pBuffer,
172             int              dwOffset,
173             int              dwBytesToRead,
174             bool             fAsync,
175             out int          pBytesRead,
176             out IntPtr       ppAsyncReceiveBuffer );
177 
178         [DllImport(_IIS_NATIVE_DLL, CharSet=CharSet.Unicode)]
MgdGetCorrelationIdHeader( IntPtr pHandler, out IntPtr correlationId, out ushort correlationIdLength, out bool base64BinaryFormat)179         internal static extern int MgdGetCorrelationIdHeader(
180             IntPtr          pHandler,
181             out IntPtr      correlationId,
182             out ushort      correlationIdLength,
183             out bool        base64BinaryFormat);
184 
185         [DllImport(_IIS_NATIVE_DLL, CharSet=CharSet.Unicode)]
MgdGetUserToken( IntPtr pHandler, out IntPtr pToken )186         internal static extern int MgdGetUserToken(
187             IntPtr           pHandler,
188             out IntPtr       pToken );
189 
190         [DllImport(_IIS_NATIVE_DLL, CharSet=CharSet.Unicode)]
MgdGetVirtualToken( IntPtr pHandler, out IntPtr pToken )191         internal static extern int MgdGetVirtualToken(
192             IntPtr           pHandler,
193             out IntPtr       pToken );
194 
195         [DllImport(_IIS_NATIVE_DLL, CharSet=CharSet.Unicode)]
MgdIsClientConnected( IntPtr pHandler)196         internal static extern bool MgdIsClientConnected(
197             IntPtr           pHandler);
198 
199         [DllImport(_IIS_NATIVE_DLL)]
MgdIsHandlerExecutionDenied( IntPtr pHandler)200         internal static extern bool MgdIsHandlerExecutionDenied(
201             IntPtr           pHandler);
202 
203         [DllImport(_IIS_NATIVE_DLL, CharSet=CharSet.Unicode)]
204         [SuppressMessage("Microsoft.Security", "CA2118:ReviewSuppressUnmanagedCodeSecurityUsage", Justification = "We carefully control this method's sole caller.")]
MgdAbortConnection( IntPtr pHandler)205         internal static extern void MgdAbortConnection(
206             IntPtr           pHandler);
207 
208         [DllImport(_IIS_NATIVE_DLL, CharSet=CharSet.Unicode)]
MgdCloseConnection( IntPtr pHandler)209         internal static extern void MgdCloseConnection(
210             IntPtr           pHandler);
211 
212         [DllImport(_IIS_NATIVE_DLL, CharSet=CharSet.Unicode)]
MgdGetHandlerTypeString( IntPtr pHandler, out IntPtr ppszTypeString, out int pcchTypeString)213         internal static extern int MgdGetHandlerTypeString(
214             IntPtr           pHandler,
215             out IntPtr       ppszTypeString,
216             out int          pcchTypeString);
217 
218         [DllImport(_IIS_NATIVE_DLL, CharSet=CharSet.Unicode)]
MgdGetApplicationInfo( IntPtr pHandler, out IntPtr pVirtualPath, out int cchVirtualPath, out IntPtr pPhysPath, out int cchPhysPath)219         internal static extern int MgdGetApplicationInfo(
220             IntPtr           pHandler,
221             out IntPtr       pVirtualPath,
222             out int          cchVirtualPath,
223             out IntPtr       pPhysPath,
224             out int          cchPhysPath);
225 
226         [DllImport(_IIS_NATIVE_DLL, CharSet=CharSet.Unicode)]
MgdGetUriPath( IntPtr pHandler, out IntPtr ppPath, out int pcchPath, bool fIncludePathInfo, bool fUseParentContext)227         internal static extern int MgdGetUriPath(
228             IntPtr           pHandler,
229             out IntPtr       ppPath,
230             out int          pcchPath,
231             bool             fIncludePathInfo,
232             bool             fUseParentContext);
233 
234         [DllImport(_IIS_NATIVE_DLL)]
MgdGetPreloadedContent( IntPtr pHandler, byte[] pBuffer, int lOffset, int cbLen, out int pcbReceived)235         internal static extern int MgdGetPreloadedContent(
236             IntPtr           pHandler,
237             byte[]           pBuffer,
238             int              lOffset,
239             int              cbLen,
240             out int          pcbReceived);
241 
242         [DllImport(_IIS_NATIVE_DLL)]
MgdGetPreloadedSize( IntPtr pHandler, out int pcbAvailable)243         internal static extern int MgdGetPreloadedSize(
244             IntPtr           pHandler,
245             out int          pcbAvailable);
246 
247         [DllImport(_IIS_NATIVE_DLL, CharSet=CharSet.Unicode)]
MgdGetPrincipal( IntPtr pHandler, int dwRequestingAppDomainId, out IntPtr pToken, out IntPtr ppAuthType, ref int pcchAuthType, out IntPtr ppUserName, ref int pcchUserName, out IntPtr pManagedPrincipal)248         internal static extern int MgdGetPrincipal(
249             IntPtr           pHandler,
250             int              dwRequestingAppDomainId,
251             out IntPtr       pToken,
252             out IntPtr       ppAuthType,
253             ref int          pcchAuthType,
254             out IntPtr       ppUserName,
255             ref int          pcchUserName,
256             out IntPtr       pManagedPrincipal);
257 
258         [DllImport(_IIS_NATIVE_DLL, CharSet=CharSet.Unicode)]
MgdIsInRole( IntPtr pHandler, string pszRoleName, out bool pfIsInRole)259         internal static extern int MgdIsInRole(
260             IntPtr           pHandler,
261             string           pszRoleName,
262             out bool         pfIsInRole);
263 
264         [DllImport(_IIS_NATIVE_DLL)]
MgdAllocateRequestMemory( IntPtr pHandler, int cbSize)265         internal static extern IntPtr MgdAllocateRequestMemory(
266             IntPtr           pHandler,
267             int              cbSize);
268 
269         [DllImport(_IIS_NATIVE_DLL, CharSet=CharSet.Unicode)]
MgdAppDomainShutdown( IntPtr appContext )270         internal static extern int MgdAppDomainShutdown(
271             IntPtr appContext );
272 
273 
274         // Buffer pool methods
275         [DllImport(_IIS_NATIVE_DLL, CharSet=CharSet.Unicode)]
276         internal static extern IntPtr /* W3_MGD_BUFFER_POOL* */
MgdGetBufferPool(int cbBufferSize)277             MgdGetBufferPool(int cbBufferSize);
278 
279         [DllImport(_IIS_NATIVE_DLL, CharSet=CharSet.Unicode)]
280         internal static extern IntPtr /* PBYTE * */
MgdGetBuffer(IntPtr pPool)281             MgdGetBuffer(IntPtr pPool);
282 
283         [DllImport(_IIS_NATIVE_DLL, CharSet=CharSet.Unicode)]
284         internal static extern IntPtr /* W3_MGD_BUFFER_POOL* */
MgdReturnBuffer(IntPtr pBuffer)285             MgdReturnBuffer(IntPtr pBuffer);
286 
287 
288         [DllImport(_IIS_NATIVE_DLL, CharSet=CharSet.Unicode)]
289         internal static extern int /* DWORD */
MgdGetLocalPort(IntPtr context)290            MgdGetLocalPort(IntPtr context);
291 
292         [DllImport(_IIS_NATIVE_DLL, CharSet=CharSet.Unicode)]
293         internal static extern int /* DWORD */
MgdGetRemotePort(IntPtr context)294            MgdGetRemotePort(IntPtr context);
295 
296 
297         [DllImport(_IIS_NATIVE_DLL)]
MgdGetUserAgent( IntPtr pRequestContext, out IntPtr pBuffer, out int cbBufferSize)298         internal static extern int MgdGetUserAgent(
299             IntPtr           pRequestContext,
300             out IntPtr       pBuffer,
301             out int          cbBufferSize);
302 
303         [DllImport(_IIS_NATIVE_DLL)]
MgdGetCookieHeader( IntPtr pRequestContext, out IntPtr pBuffer, out int cbBufferSize)304         internal static extern int MgdGetCookieHeader(
305             IntPtr           pRequestContext,
306             out IntPtr       pBuffer,
307             out int          cbBufferSize);
308 
309         [DllImport(_IIS_NATIVE_DLL, CharSet=CharSet.Unicode)]
MgdRewriteUrl( IntPtr pRequestContext, string pszUrl, bool fResetQueryString )310         internal static extern int MgdRewriteUrl(
311             IntPtr           pRequestContext,
312             string           pszUrl,
313             bool             fResetQueryString );
314 
315         [DllImport(_IIS_NATIVE_DLL)]
MgdGetMaxConcurrentRequestsPerCPU()316         internal static extern int MgdGetMaxConcurrentRequestsPerCPU();
317 
318         [DllImport(_IIS_NATIVE_DLL)]
MgdGetMaxConcurrentThreadsPerCPU()319         internal static extern int MgdGetMaxConcurrentThreadsPerCPU();
320 
321         [DllImport(_IIS_NATIVE_DLL)]
MgdSetMaxConcurrentRequestsPerCPU(int value)322         internal static extern int MgdSetMaxConcurrentRequestsPerCPU(int value);
323 
324         [DllImport(_IIS_NATIVE_DLL)]
MgdSetMaxConcurrentThreadsPerCPU(int value)325         internal static extern int MgdSetMaxConcurrentThreadsPerCPU(int value);
326 
327         [DllImport(_IIS_NATIVE_DLL)]
MgdGetCurrentModuleName( IntPtr pHandler, out IntPtr pBuffer, out int cbBufferSize)328         internal static extern int MgdGetCurrentModuleName(
329             IntPtr           pHandler,
330             out IntPtr       pBuffer,
331             out int          cbBufferSize);
332 
333         [DllImport(_IIS_NATIVE_DLL)]
MgdGetCurrentNotification( IntPtr pRequestContext)334         internal static extern int MgdGetCurrentNotification(
335             IntPtr           pRequestContext);
336 
337         [DllImport(_IIS_NATIVE_DLL)]
MgdDisableNotifications( IntPtr pRequestContext, [MarshalAs(UnmanagedType.U4)] RequestNotification notifications, [MarshalAs(UnmanagedType.U4)] RequestNotification postNotifications)338         internal static extern void MgdDisableNotifications(
339             IntPtr           pRequestContext,
340             [MarshalAs(UnmanagedType.U4)]
341             RequestNotification notifications,
342             [MarshalAs(UnmanagedType.U4)]
343             RequestNotification postNotifications);
344 
345         [DllImport(_IIS_NATIVE_DLL)]
MgdSuppressSendResponseNotifications( IntPtr pRequestContext)346         internal static extern void MgdSuppressSendResponseNotifications(
347             IntPtr pRequestContext);
348 
349         [DllImport(_IIS_NATIVE_DLL)]
MgdGetNextNotification( IntPtr pRequestContext, [MarshalAs(UnmanagedType.U4)] RequestNotificationStatus dwStatus)350         internal static extern int MgdGetNextNotification(
351             IntPtr           pRequestContext,
352             [MarshalAs(UnmanagedType.U4)]
353             RequestNotificationStatus dwStatus);
354 
355         [DllImport(_IIS_NATIVE_DLL)]
MgdClearResponse( IntPtr pRequestContext, bool fClearEntity, bool fClearHeaders)356         internal static extern int MgdClearResponse(
357             IntPtr           pRequestContext,
358             bool             fClearEntity,
359             bool             fClearHeaders);
360 
361         [DllImport(_IIS_NATIVE_DLL)]
MgdCreateNativeConfigSystem( out IntPtr ppConfigSystem)362         internal static extern int MgdCreateNativeConfigSystem(
363             out IntPtr       ppConfigSystem);
364 
365         [DllImport(_IIS_NATIVE_DLL)]
MgdReleaseNativeConfigSystem( IntPtr pConfigSystem)366         internal static extern int MgdReleaseNativeConfigSystem(
367             IntPtr           pConfigSystem);
368 
369         [DllImport(_IIS_NATIVE_DLL)]
MgdGetRequestTraceGuid( IntPtr pRequestContext, out Guid traceContextId)370         internal static extern int MgdGetRequestTraceGuid(
371             IntPtr           pRequestContext,
372             out Guid         traceContextId);
373 
374         [DllImport(_IIS_NATIVE_DLL)]
MgdGetStatusChanges( IntPtr pRequestContext, out ushort statusCode, out ushort subStatusCode, out IntPtr pBuffer, out ushort cbBufferSize)375         internal static extern int MgdGetStatusChanges(
376             IntPtr           pRequestContext,
377             out ushort       statusCode,
378             out ushort       subStatusCode,
379             out IntPtr       pBuffer,
380             out ushort       cbBufferSize);
381 
382         [DllImport(_IIS_NATIVE_DLL)]
MgdGetResponseChunks( IntPtr pRequestContext, ref int fragmentCount, IntPtr[] bodyFragments, int[] bodyFragmentLengths, int[] fragmentChunkType)383         internal static extern int MgdGetResponseChunks(
384             IntPtr           pRequestContext,
385             ref int          fragmentCount,
386             IntPtr[]         bodyFragments,
387             int[]            bodyFragmentLengths,
388             int[]            fragmentChunkType);
389 
390         [DllImport(_IIS_NATIVE_DLL)]
MgdEtwGetTraceConfig( IntPtr pRequestContext, out bool providerEnabled, out int flags, out int level)391         internal static extern int MgdEtwGetTraceConfig(
392             IntPtr           pRequestContext,
393             out bool         providerEnabled,
394             out int          flags,
395             out int          level);
396 
397         [DllImport(_IIS_NATIVE_DLL, CharSet=CharSet.Unicode)]
MgdEmitSimpleTrace( IntPtr pRequestContext, int type, string eventData)398         internal static extern int MgdEmitSimpleTrace(
399             IntPtr           pRequestContext,
400             int              type,
401             string           eventData);
402 
403         [DllImport(_IIS_NATIVE_DLL, CharSet = CharSet.Unicode)]
MgdEmitWebEventTrace( IntPtr pRequestContext, int webEventType, int fieldCount, string[] fieldNames, int[] fieldTypes, string[] fieldData)404         internal static extern int MgdEmitWebEventTrace(
405             IntPtr           pRequestContext,
406             int              webEventType,
407             int              fieldCount,
408             string[]         fieldNames,
409             int[]            fieldTypes,
410             string[]         fieldData);
411 
412         [DllImport(_IIS_NATIVE_DLL, CharSet=CharSet.Unicode)]
MgdSetRequestPrincipal( IntPtr pRequestContext, string userName, string authType, IntPtr token)413         internal static extern int MgdSetRequestPrincipal(
414             IntPtr           pRequestContext,
415             string           userName,
416             string           authType,
417             IntPtr           token);
418 
419         [DllImport(_IIS_NATIVE_DLL, CharSet=CharSet.Unicode)]
MgdCanDisposeManagedContext( IntPtr pRequestContext, [MarshalAs(UnmanagedType.U4)] RequestNotificationStatus dwStatus)420         internal static extern bool MgdCanDisposeManagedContext(
421             IntPtr           pRequestContext,
422             [MarshalAs(UnmanagedType.U4)]
423             RequestNotificationStatus dwStatus);
424 
425         [DllImport(_IIS_NATIVE_DLL, CharSet=CharSet.Unicode)]
MgdIsLastNotification( IntPtr pRequestContext, [MarshalAs(UnmanagedType.U4)] RequestNotificationStatus dwStatus)426         internal static extern bool MgdIsLastNotification(
427             IntPtr           pRequestContext,
428             [MarshalAs(UnmanagedType.U4)]
429             RequestNotificationStatus dwStatus);
430 
431         [DllImport(_IIS_NATIVE_DLL, CharSet=CharSet.Unicode)]
MgdIsWithinApp( IntPtr pConfigSystem, string siteName, string appPath, string virtualPath)432         internal static extern bool MgdIsWithinApp(
433             IntPtr            pConfigSystem,
434             string            siteName,
435             string            appPath,
436             string            virtualPath);
437 
438         [DllImport(_IIS_NATIVE_DLL)]
MgdGetSiteNameFromId( IntPtr pConfigSystem, [MarshalAs(UnmanagedType.U4)] uint siteId, out IntPtr bstrSiteName, out int cchSiteName)439         internal static extern int MgdGetSiteNameFromId(
440             IntPtr            pConfigSystem,
441             [MarshalAs(UnmanagedType.U4)]
442             uint              siteId,
443             out IntPtr        bstrSiteName,
444             out int           cchSiteName);
445 
446         [DllImport(_IIS_NATIVE_DLL, CharSet=CharSet.Unicode)]
MgdGetAppPathForPath( IntPtr pConfigSystem, [MarshalAs(UnmanagedType.U4)] uint siteId, string virtualPath, out IntPtr bstrPath, out int cchPath)447         internal static extern int MgdGetAppPathForPath(
448             IntPtr            pConfigSystem,
449             [MarshalAs(UnmanagedType.U4)]
450             uint              siteId,
451             string            virtualPath,
452             out IntPtr        bstrPath,
453             out int           cchPath);
454 
455         [DllImport(_IIS_NATIVE_DLL)]
MgdGetMemoryLimitKB( out long limit)456         internal static extern int MgdGetMemoryLimitKB(
457             out long          limit);
458 
459         [DllImport(_IIS_NATIVE_DLL)]
MgdGetMimeMapCollection( IntPtr pConfigSystem, IntPtr appContext, out IntPtr pMimeMapCollection, out int count)460         internal static extern int MgdGetMimeMapCollection(
461             IntPtr            pConfigSystem,
462             IntPtr            appContext,
463             out IntPtr        pMimeMapCollection,
464             out int           count);
465 
466         [DllImport(_IIS_NATIVE_DLL)]
MgdGetModuleCollection( IntPtr pConfigSystem, IntPtr appContext, out IntPtr pModuleCollection, out int count)467         internal static extern int MgdGetModuleCollection(
468             IntPtr            pConfigSystem,
469             IntPtr            appContext,
470             out IntPtr        pModuleCollection,
471             out int           count);
472 
473         [DllImport(_IIS_NATIVE_DLL)]
MgdGetNextMimeMap( IntPtr pMimeMapCollection, uint dwIndex, out IntPtr bstrFileExtension, out int cchFileExtension, out IntPtr bstrMimeType, out int cchMimeType)474         internal static extern int MgdGetNextMimeMap(
475             IntPtr            pMimeMapCollection,
476             uint              dwIndex,
477             out IntPtr        bstrFileExtension,
478             out int           cchFileExtension,
479             out IntPtr        bstrMimeType,
480             out int           cchMimeType);
481 
482         [DllImport(_IIS_NATIVE_DLL)]
MgdGetNextModule( IntPtr pModuleCollection, ref uint dwIndex, out IntPtr bstrModuleName, out int cchModuleName, out IntPtr bstrModuleType, out int cchModuleType, out IntPtr bstrModulePrecondition, out int cchModulePrecondition)483         internal static extern int MgdGetNextModule(
484             IntPtr            pModuleCollection,
485             ref uint          dwIndex,
486             out IntPtr        bstrModuleName,
487             out int           cchModuleName,
488             out IntPtr        bstrModuleType,
489             out int           cchModuleType,
490             out IntPtr        bstrModulePrecondition,
491             out int           cchModulePrecondition);
492 
493         [DllImport(_IIS_NATIVE_DLL, CharSet=CharSet.Unicode)]
MgdGetVrPathCreds( IntPtr pConfigSystem, string siteName, string virtualPath, out IntPtr bstrUserName, out int cchUserName, out IntPtr bstrPassword, out int cchPassword)494         internal static extern int MgdGetVrPathCreds(
495             IntPtr            pConfigSystem,
496             string            siteName,
497             string            virtualPath,
498             out IntPtr        bstrUserName,
499             out int           cchUserName,
500             out IntPtr        bstrPassword,
501             out int           cchPassword);
502 
503         [DllImport(_IIS_NATIVE_DLL, CharSet=CharSet.Unicode)]
MgdGetAppCollection( IntPtr pConfigSystem, string siteName, string virtualPath, out IntPtr bstrPath, out int cchPath, out IntPtr pAppCollection, out int count)504         internal static extern int MgdGetAppCollection(
505             IntPtr            pConfigSystem,
506             string            siteName,
507             string            virtualPath,
508             out IntPtr        bstrPath,
509             out int           cchPath,
510             out IntPtr        pAppCollection,
511             out int           count);
512 
513         [DllImport(_IIS_NATIVE_DLL)]
MgdGetNextVPath( IntPtr pAppCollection, uint dwIndex, out IntPtr bstrPath, out int cchPath)514         internal static extern int MgdGetNextVPath(
515             IntPtr            pAppCollection,
516             uint              dwIndex,
517             out IntPtr        bstrPath,
518             out int           cchPath);
519 
520         [DllImport(_IIS_NATIVE_DLL)]
MgdInitNativeConfig()521         internal static extern int MgdInitNativeConfig();
522 
523         [DllImport(_IIS_NATIVE_DLL)]
MgdTerminateNativeConfig()524         internal static extern void MgdTerminateNativeConfig();
525 
526         [DllImport(_IIS_NATIVE_DLL, CharSet=CharSet.Unicode)]
MgdMapPathDirect( IntPtr pConfigSystem, string siteName, string virtualPath, out IntPtr bstrPhysicalPath, out int cchPath)527         internal static extern int MgdMapPathDirect(
528             IntPtr            pConfigSystem,
529             string            siteName,
530             string            virtualPath,
531             out IntPtr        bstrPhysicalPath,
532             out int           cchPath);
533 
534         [DllImport(_IIS_NATIVE_DLL, CharSet=CharSet.Unicode)]
MgdMapHandler( IntPtr pHandler, string method, string virtualPath, out IntPtr ppszTypeString, out int pcchTypeString, bool convertNativeStaticFileModule, bool ignoreWildcardMappings)535         internal static extern int MgdMapHandler(
536             IntPtr            pHandler,
537             string            method,
538             string            virtualPath,
539             out IntPtr        ppszTypeString,
540             out int           pcchTypeString,
541             bool              convertNativeStaticFileModule,
542             bool              ignoreWildcardMappings);
543 
544         [DllImport(_IIS_NATIVE_DLL, CharSet=CharSet.Unicode)]
MgdReMapHandler( IntPtr pHandler, string pszVirtualPath, out IntPtr ppszTypeString, out int pcchTypeString, out bool pfHandlerExists)545         internal static extern int MgdReMapHandler(
546             IntPtr            pHandler,
547             string            pszVirtualPath,
548             out IntPtr        ppszTypeString,
549             out int           pcchTypeString,
550             out bool          pfHandlerExists);
551 
552         [DllImport(_IIS_NATIVE_DLL, CharSet=CharSet.Unicode)]
MgdSetRemapHandler( IntPtr pHandler, string pszName, string ppszType)553         internal static extern int MgdSetRemapHandler(
554             IntPtr            pHandler,
555             string            pszName,
556             string            ppszType);
557 
558         [DllImport(_IIS_NATIVE_DLL)]
MgdSetScriptMapForRemapHandler( IntPtr pHandler)559         internal static extern int MgdSetScriptMapForRemapHandler(
560             IntPtr            pHandler);
561 
562         [DllImport(_IIS_NATIVE_DLL)]
MgdSetNativeConfiguration( IntPtr nativeConfig)563         internal static extern int MgdSetNativeConfiguration(
564             IntPtr             nativeConfig);
565 
566         [DllImport(_IIS_NATIVE_DLL, CharSet=CharSet.Unicode)]
567         [return: MarshalAs(UnmanagedType.U4)]
MgdResolveSiteName( IntPtr pConfigSystem, string siteName)568         internal static extern uint MgdResolveSiteName(
569             IntPtr           pConfigSystem,
570             string           siteName);
571 
572         [DllImport(_IIS_NATIVE_DLL)]
MgdSetResponseFilter( IntPtr context)573         internal static extern void MgdSetResponseFilter(
574             IntPtr             context);
575 
576         [DllImport(_IIS_NATIVE_DLL)]
MgdGetFileChunkInfo( IntPtr context, int chunkOffset, out long offset, out long length)577         internal static extern int MgdGetFileChunkInfo(
578             IntPtr             context,
579             int                chunkOffset,
580             out long           offset,
581             out long           length);
582 
583 
584         [DllImport(_IIS_NATIVE_DLL)]
MgdReadChunkHandle( IntPtr context, IntPtr FileHandle, long startOffset, ref int length, IntPtr chunkEntity)585         internal static extern int MgdReadChunkHandle(
586             IntPtr             context,
587             IntPtr             FileHandle,
588             long               startOffset,
589             ref int            length,
590             IntPtr             chunkEntity);
591 
592         [DllImport(_IIS_NATIVE_DLL)]
MgdExplicitFlush( IntPtr context, bool async, out bool completedSynchronously)593         internal static extern int MgdExplicitFlush(
594             IntPtr             context,
595             bool               async,
596             out bool           completedSynchronously);
597 
598         [DllImport(_IIS_NATIVE_DLL, CharSet=CharSet.Unicode)]
MgdSetServerVariableW( IntPtr context, string variableName, string variableValue)599         internal static extern int MgdSetServerVariableW(
600             IntPtr            context,
601             string            variableName,
602             string            variableValue);
603 
604         [DllImport(_IIS_NATIVE_DLL, CharSet=CharSet.Unicode)]
MgdExecuteUrl( IntPtr context, string url, bool resetQuerystring, bool preserveForm, byte[] entityBody, uint entityBodySize, string method, int numHeaders, string[] headersNames, string[] headersValues, bool preserveUser)605         internal static extern int MgdExecuteUrl(
606             IntPtr            context,
607             string            url,
608             bool              resetQuerystring,
609             bool              preserveForm,
610             byte[]            entityBody,
611             uint              entityBodySize,
612             string            method,
613             int               numHeaders,
614             string[]          headersNames,
615             string[]          headersValues,
616             bool              preserveUser);
617 
618         [DllImport(_IIS_NATIVE_DLL, CharSet=CharSet.Unicode)]
MgdGetClientCertificate( IntPtr pHandler, out IntPtr ppbClientCert, out int pcbClientCert, out IntPtr ppbClientCertIssuer, out int pcbClientCertIssuer, out IntPtr ppbClientCertPublicKey, out int pcbClientCertPublicKey, out uint pdwCertEncodingType, out long ftNotBefore, out long ftNotAfter)619         internal static extern int MgdGetClientCertificate(
620             IntPtr            pHandler,
621             out IntPtr        ppbClientCert,
622             out int           pcbClientCert,
623             out IntPtr        ppbClientCertIssuer,
624             out int           pcbClientCertIssuer,
625             out IntPtr        ppbClientCertPublicKey,
626             out int           pcbClientCertPublicKey,
627             out uint          pdwCertEncodingType,
628             out long          ftNotBefore,
629             out long          ftNotAfter);
630 
631         [DllImport(_IIS_NATIVE_DLL)]
MgdGetChannelBindingToken( IntPtr pHandler, out IntPtr ppbToken, out int pcbTokenSize)632         internal static extern int MgdGetChannelBindingToken(
633             IntPtr            pHandler,
634             out IntPtr        ppbToken,
635             out int           pcbTokenSize);
636 
637         [DllImport(_IIS_NATIVE_DLL)]
MgdGetCurrentNotificationInfo( IntPtr pHandler, out int currentModuleIndex, out bool isPostNotification, out int currentNotification)638         internal static extern void MgdGetCurrentNotificationInfo(
639             IntPtr            pHandler,
640             out int           currentModuleIndex,
641             out bool          isPostNotification,
642             out int           currentNotification);
643 
644         [DllImport(_IIS_NATIVE_DLL)]
645         [SuppressMessage("Microsoft.Security", "CA2118:ReviewSuppressUnmanagedCodeSecurityUsage", Justification = "We carefully control this method's sole caller.")]
MgdAcceptWebSocket( IntPtr pHandler)646         internal static extern int MgdAcceptWebSocket(
647             IntPtr pHandler);
648 
649         [DllImport(_IIS_NATIVE_DLL)]
650         [SuppressMessage("Microsoft.Security", "CA2118:ReviewSuppressUnmanagedCodeSecurityUsage", Justification = "We carefully control this method's sole caller.")]
MgdGetWebSocketContext( IntPtr pHandler, out IntPtr ppWebSocketContext)651         internal static extern int MgdGetWebSocketContext(
652             IntPtr pHandler,
653             out IntPtr ppWebSocketContext);
654 
655         [DllImport(_IIS_NATIVE_DLL, CharSet=CharSet.Unicode)]
MgdGetAnonymousUserToken( IntPtr pHandler, out IntPtr pToken )656         internal static extern int MgdGetAnonymousUserToken(
657             IntPtr            pHandler,
658             out IntPtr        pToken );
659 
660         [DllImport(_IIS_NATIVE_DLL)]
MgdGetIISVersionInformation( [Out] out uint pdwVersion, [Out] out bool pfIsIntegratedMode)661         internal static extern void MgdGetIISVersionInformation(
662             [Out] out uint pdwVersion,
663             [Out] out bool pfIsIntegratedMode);
664 
665         [DllImport(_IIS_NATIVE_DLL)]
666         [SuppressMessage("Microsoft.Security", "CA2118:ReviewSuppressUnmanagedCodeSecurityUsage", Justification = "We carefully control this method's callers.")]
MgdConfigureAsyncDisconnectNotification( [In] IntPtr pHandler, [In] bool fEnable, [Out] out bool pfIsClientConnected)667         internal static extern int MgdConfigureAsyncDisconnectNotification(
668             [In] IntPtr pHandler,
669             [In] bool fEnable,
670             [Out] out bool pfIsClientConnected);
671 
672         [DllImport(_IIS_NATIVE_DLL)]
673         [SuppressMessage("Microsoft.Security", "CA2118:ReviewSuppressUnmanagedCodeSecurityUsage", Justification = "We carefully control this method's callers.")]
MgdGetIsChildContext( [In] IntPtr pHandler, [Out] out bool pfIsChildContext)674         internal static extern int MgdGetIsChildContext(
675             [In] IntPtr pHandler,
676             [Out] out bool pfIsChildContext);
677 
678         [DllImport(_IIS_NATIVE_DLL)]
679         [SuppressMessage("Microsoft.Security", "CA2118:ReviewSuppressUnmanagedCodeSecurityUsage", Justification = "We carefully control this method's callers.")]
MgdGetConfigProperty( [In, MarshalAs(UnmanagedType.BStr)] string appConfigMetabasePath, [In, MarshalAs(UnmanagedType.BStr)] string sectionName, [In, MarshalAs(UnmanagedType.BStr)] string propertyName, [Out, MarshalAs(UnmanagedType.Struct)] out object value)680         internal static extern int MgdGetConfigProperty(
681             [In, MarshalAs(UnmanagedType.BStr)] string appConfigMetabasePath,
682             [In, MarshalAs(UnmanagedType.BStr)] string sectionName,
683             [In, MarshalAs(UnmanagedType.BStr)] string propertyName,
684             [Out, MarshalAs(UnmanagedType.Struct)] out object value); // marshaled as VARIANT
685 
686         [DllImport(_IIS_NATIVE_DLL)]
687         [SuppressMessage("Microsoft.Security", "CA2118:ReviewSuppressUnmanagedCodeSecurityUsage", Justification = "We carefully control this method's callers.")]
MgdPushPromise( [In] IntPtr context, [In, MarshalAs(UnmanagedType.LPWStr)] string path, [In, MarshalAs(UnmanagedType.LPWStr)] string queryString, [In, MarshalAs(UnmanagedType.LPStr)] string method, [In] int numHeaders, [In, MarshalAs(UnmanagedType.LPArray, ArraySubType=UnmanagedType.LPStr)] string[] headersNames, [In, MarshalAs(UnmanagedType.LPArray, ArraySubType = UnmanagedType.LPStr)] string[] headersValues)688         internal static extern int MgdPushPromise(
689             [In] IntPtr context,
690             [In, MarshalAs(UnmanagedType.LPWStr)] string path,
691             [In, MarshalAs(UnmanagedType.LPWStr)] string queryString,
692             [In, MarshalAs(UnmanagedType.LPStr)] string method,
693             [In] int numHeaders,
694             [In, MarshalAs(UnmanagedType.LPArray, ArraySubType=UnmanagedType.LPStr)] string[] headersNames,
695             [In, MarshalAs(UnmanagedType.LPArray, ArraySubType = UnmanagedType.LPStr)] string[] headersValues);
696 
697         [DllImport(_IIS_NATIVE_DLL)]
698         [SuppressMessage("Microsoft.Security", "CA2118:ReviewSuppressUnmanagedCodeSecurityUsage", Justification = "We carefully control this method's callers.")]
MgdIsAppPoolShuttingDown()699         internal static extern bool MgdIsAppPoolShuttingDown();
700 
701         [DllImport(_IIS_NATIVE_DLL)]
702         [SuppressMessage("Microsoft.Security", "CA2118:ReviewSuppressUnmanagedCodeSecurityUsage", Justification = "We carefully control this method's callers.")]
MgdGetTlsTokenBindingIdentifiers( [In] IntPtr pHandler, [In, Out] ref IntPtr tokenBindingHandle, [Out] out IntPtr providedToken, [Out] out uint providedTokenSize, [Out] out IntPtr referredToken, [Out] out uint referredTokenSize)703         internal static extern int MgdGetTlsTokenBindingIdentifiers(
704             [In] IntPtr pHandler,
705             [In, Out] ref IntPtr tokenBindingHandle,
706             [Out] out IntPtr providedToken,
707             [Out] out uint providedTokenSize,
708             [Out] out IntPtr referredToken,
709             [Out] out uint referredTokenSize);
710     }
711 }
712