PageRenderTime 47ms CodeModel.GetById 16ms app.highlight 21ms RepoModel.GetById 1ms app.codeStats 1ms

/WCFWebApi/src/Microsoft.Server.Common/Microsoft/Server/Common/Interop/UnsafeNativeMethods.cs

#
C# | 238 lines | 205 code | 27 blank | 6 comment | 5 complexity | ff70f92c25eff24f8396779feeb3b38d MD5 | raw file
  1//------------------------------------------------------------
  2// Copyright (c) Microsoft Corporation.  All rights reserved.
  3//------------------------------------------------------------
  4
  5namespace Microsoft.Server.Common.Interop
  6{
  7    using System;
  8    using System.Diagnostics.CodeAnalysis;
  9    using System.Runtime.InteropServices;
 10    using System.Runtime.Versioning;
 11    using System.Security;
 12    using System.Text;
 13    using Microsoft.Server.Common.Diagnostics;
 14    using Microsoft.Win32.SafeHandles;
 15
 16    [SuppressUnmanagedCodeSecurity]    
 17    static class UnsafeNativeMethods
 18    {
 19        public const string KERNEL32 = "kernel32.dll";
 20        public const String ADVAPI32 = "advapi32.dll";
 21
 22        public const int ERROR_INVALID_HANDLE = 6;
 23        public const int ERROR_MORE_DATA = 234;
 24        public const int ERROR_ARITHMETIC_OVERFLOW = 534;
 25        public const int ERROR_NOT_ENOUGH_MEMORY = 8;
 26
 27        [StructLayout(LayoutKind.Explicit, Size = 16)]
 28        public struct EventData
 29        {
 30            [FieldOffset(0)]
 31            internal UInt64 DataPointer;
 32            [FieldOffset(8)]
 33            internal uint Size;
 34            [FieldOffset(12)]
 35            internal int Reserved;
 36        }       
 37
 38        [SuppressMessage("Microsoft.Security", "CA2118:ReviewSuppressUnmanagedCodeSecurityUsage",
 39            Justification = "This PInvoke call has been reviewed")]
 40        [SuppressMessage("Microsoft.Interoperability", "CA1414:MarkBooleanPInvokeArgumentsWithMarshalAs", Justification = "Ported from WCF")]
 41        [DllImport(KERNEL32, BestFitMapping = false, CharSet = CharSet.Auto)]
 42        [ResourceExposure(ResourceScope.Process)]
 43        [SecurityCritical]
 44        public static extern SafeWaitHandle CreateWaitableTimer(IntPtr mustBeZero, bool manualReset, string timerName);
 45
 46        [SuppressMessage("Microsoft.Security", "CA2118:ReviewSuppressUnmanagedCodeSecurityUsage",
 47            Justification = "This PInvoke call has been reviewed")]
 48        [SuppressMessage("Microsoft.Interoperability", "CA1414:MarkBooleanPInvokeArgumentsWithMarshalAs", Justification = "Ported from WCF")]
 49        [DllImport(KERNEL32, ExactSpelling = true)]
 50        [ResourceExposure(ResourceScope.None)]
 51        [SecurityCritical]
 52        public static extern bool SetWaitableTimer(SafeWaitHandle handle, ref long dueTime, int period, IntPtr mustBeZero, IntPtr mustBeZeroAlso, bool resume);
 53
 54        [SuppressMessage("Microsoft.Security", "CA2118:ReviewSuppressUnmanagedCodeSecurityUsage",
 55            Justification = "This PInvoke call has been reviewed")]
 56        [DllImport(KERNEL32, SetLastError = true)]
 57        [ResourceExposure(ResourceScope.None)]
 58        [SecurityCritical]
 59        public static extern int QueryPerformanceCounter(out long time);
 60
 61        [SuppressMessage("Microsoft.Security", "CA2118:ReviewSuppressUnmanagedCodeSecurityUsage",
 62            Justification = "This PInvoke call has been reviewed")]
 63        [DllImport(KERNEL32, SetLastError = false)]
 64        [ResourceExposure(ResourceScope.None)]
 65        [SecurityCritical]
 66        public static extern uint GetSystemTimeAdjustment(
 67            [Out] out int adjustment,
 68            [Out] out uint increment,
 69            [Out] out uint adjustmentDisabled
 70            );
 71
 72        [SuppressMessage("Microsoft.Security", "CA2118:ReviewSuppressUnmanagedCodeSecurityUsage",
 73            Justification = "This PInvoke call has been reviewed")]
 74        [DllImport(KERNEL32, SetLastError = true)]
 75        [ResourceExposure(ResourceScope.None)]
 76        [SecurityCritical]
 77        public static extern void GetSystemTimeAsFileTime([Out] out long time);
 78
 79        [SuppressMessage("Microsoft.Security", "CA2101:SpecifyMarshalingForPInvokeStringArguments", Justification = "")]
 80        [DllImport(KERNEL32, SetLastError = true, CharSet = CharSet.Auto)]
 81        [return: MarshalAs(UnmanagedType.Bool)]
 82        [ResourceExposure(ResourceScope.None)]
 83        [SecurityCritical]
 84        static extern bool GetComputerNameEx
 85            (
 86            [In] ComputerNameFormat nameType,
 87            [In, Out, MarshalAs(UnmanagedType.LPTStr)] StringBuilder lpBuffer,
 88            [In, Out] ref int size
 89            );
 90
 91        [SecurityCritical]
 92        internal static string GetComputerName(ComputerNameFormat nameType)
 93        {
 94            int length = 0;
 95            if (!GetComputerNameEx(nameType, null, ref length))
 96            {
 97                int error = Marshal.GetLastWin32Error();
 98
 99                if (error != ERROR_MORE_DATA)
100                {
101                    throw Fx.Exception.AsError(new System.ComponentModel.Win32Exception(error));
102                }
103            }
104
105            if (length < 0)
106            {
107                Fx.AssertAndThrow("GetComputerName returned an invalid length: " + length);
108            }
109
110            StringBuilder stringBuilder = new StringBuilder(length);
111            if (!GetComputerNameEx(nameType, stringBuilder, ref length))
112            {
113                int error = Marshal.GetLastWin32Error();
114                throw Fx.Exception.AsError(new System.ComponentModel.Win32Exception(error));
115            }
116
117            return stringBuilder.ToString();
118        }
119
120        [SuppressMessage("Microsoft.Security", "CA2118:ReviewSuppressUnmanagedCodeSecurityUsage",
121            Justification = "This PInvoke call has been reviewed")]
122        [SuppressMessage("Microsoft.Usage", "CA2205:UseManagedEquivalentsOfWin32Api", Justification = "Preserve WCF compat")]
123        [SuppressMessage("Microsoft.Interoperability", "CA1414:MarkBooleanPInvokeArgumentsWithMarshalAs", Justification = "Ported from WCF")]
124        [DllImport(KERNEL32)]
125        [ResourceExposure(ResourceScope.None)]
126        [SecurityCritical]
127        internal static extern bool IsDebuggerPresent();
128
129        [SuppressMessage("Microsoft.Security", "CA2118:ReviewSuppressUnmanagedCodeSecurityUsage",
130            Justification = "This PInvoke call has been reviewed")]
131        [SuppressMessage("Microsoft.Usage", "CA2205:UseManagedEquivalentsOfWin32Api", Justification = "Preserve WCF compat")]
132        [DllImport(KERNEL32)]
133        [ResourceExposure(ResourceScope.Process)]
134        [SecurityCritical]
135        internal static extern void DebugBreak();
136
137        [SuppressMessage("Microsoft.Security", "CA2118:ReviewSuppressUnmanagedCodeSecurityUsage",
138            Justification = "This PInvoke call has been reviewed")]
139        [SuppressMessage("Microsoft.Usage", "CA2205:UseManagedEquivalentsOfWin32Api", Justification = "Preserve WCF compat")]
140        [DllImport(KERNEL32, CharSet = CharSet.Unicode)]
141        [ResourceExposure(ResourceScope.Process)]
142        [SecurityCritical]
143        internal static extern void OutputDebugString(string lpOutputString);
144
145        //
146        // Callback
147        //
148        [SecurityCritical]
149        internal unsafe delegate void EtwEnableCallback(
150            [In] ref Guid sourceId,
151            [In] int isEnabled,
152            [In] byte level,
153            [In] long matchAnyKeywords,
154            [In] long matchAllKeywords,
155            [In] void* filterData,
156            [In] void* callbackContext
157            );
158
159        [SuppressMessage("Microsoft.Security", "CA2118:ReviewSuppressUnmanagedCodeSecurityUsage",
160            Justification = "This PInvoke call has been reviewed")]
161        [DllImport(ADVAPI32, ExactSpelling = true, EntryPoint = "EventRegister", CharSet = System.Runtime.InteropServices.CharSet.Unicode)]
162        [SecurityCritical]
163        internal static extern unsafe uint EventRegister(
164                    [In] ref Guid providerId,
165                    [In]EtwEnableCallback enableCallback,
166                    [In]void* callbackContext,
167                    [In][Out]ref long registrationHandle
168                    );
169
170        [SuppressMessage("Microsoft.Security", "CA2118:ReviewSuppressUnmanagedCodeSecurityUsage",
171            Justification = "This PInvoke call has been reviewed")]
172        [DllImport(ADVAPI32, ExactSpelling = true, EntryPoint = "EventUnregister", CharSet = System.Runtime.InteropServices.CharSet.Unicode)]
173        [SecurityCritical]
174        internal static extern uint EventUnregister([In] long registrationHandle);
175
176        [SuppressMessage("Microsoft.Security", "CA2118:ReviewSuppressUnmanagedCodeSecurityUsage",
177            Justification = "This PInvoke call has been reviewed")]
178        [DllImport(ADVAPI32, ExactSpelling = true, EntryPoint = "EventWrite", CharSet = System.Runtime.InteropServices.CharSet.Unicode)]
179        [SecurityCritical]
180        internal static extern unsafe uint EventWrite(
181                [In] long registrationHandle,
182                [In] ref EventDescriptor eventDescriptor,
183                [In] uint userDataCount,
184                [In] EventData* userData
185                );
186
187        [SuppressMessage("Microsoft.Security", "CA2118:ReviewSuppressUnmanagedCodeSecurityUsage",
188            Justification = "This PInvoke call has been reviewed")]
189        [DllImport(ADVAPI32, ExactSpelling = true, EntryPoint = "EventWriteTransfer", CharSet = System.Runtime.InteropServices.CharSet.Unicode)]
190        [SecurityCritical]
191        internal static extern unsafe uint EventWriteTransfer(
192                [In] long registrationHandle,
193                [In] ref EventDescriptor eventDescriptor,
194                [In] ref Guid activityId,
195                [In] ref Guid relatedActivityId,
196                [In] uint userDataCount,
197                [In] EventData* userData
198                );
199
200        [SuppressMessage("Microsoft.Security", "CA2118:ReviewSuppressUnmanagedCodeSecurityUsage",
201            Justification = "This PInvoke call has been reviewed")]
202        [DllImport(ADVAPI32, ExactSpelling = true, EntryPoint = "EventWriteString", CharSet = System.Runtime.InteropServices.CharSet.Unicode)]
203        [SecurityCritical]
204        internal static extern unsafe uint EventWriteString(
205                [In] long registrationHandle,
206                [In] byte level,
207                [In] long keywords,
208                [In] char* message
209                );
210
211        [SuppressMessage("Microsoft.Security", "CA2118:ReviewSuppressUnmanagedCodeSecurityUsage",
212            Justification = "This PInvoke call has been reviewed")]
213        [DllImport(ADVAPI32, ExactSpelling = true, EntryPoint = "EventActivityIdControl", CharSet = System.Runtime.InteropServices.CharSet.Unicode)]
214        [SecurityCritical]
215        internal static extern unsafe uint EventActivityIdControl([In] int ControlCode, [In][Out] ref Guid ActivityId);
216
217        [SuppressMessage("Microsoft.Security", "CA2118:ReviewSuppressUnmanagedCodeSecurityUsage",
218            Justification = "This PInvoke call has been reviewed")]
219        [SuppressMessage("Microsoft.Usage", "CA2205:UseManagedEquivalentsOfWin32Api", Justification = "Ported from WCF")]
220        [SuppressMessage("Microsoft.Interoperability", "CA1414:MarkBooleanPInvokeArgumentsWithMarshalAs", Justification = "Ported from WCF")]
221        [DllImport(ADVAPI32, CharSet = System.Runtime.InteropServices.CharSet.Unicode, SetLastError = true)]
222        [ResourceExposure(ResourceScope.None)]
223        [SecurityCritical]
224        [Fx.Tag.SecurityNote(Critical = "Accesses security critical type SafeHandle")]
225        internal static extern bool ReportEvent(SafeHandle hEventLog, ushort type, ushort category,
226                                                uint eventID, byte[] userSID, ushort numStrings, uint dataLen, HandleRef strings,
227                                                byte[] rawData);
228
229        [SuppressMessage("Microsoft.Security", "CA2118:ReviewSuppressUnmanagedCodeSecurityUsage",
230            Justification = "This PInvoke call has been reviewed")]
231        [SuppressMessage("Microsoft.Usage", "CA2205:UseManagedEquivalentsOfWin32Api", Justification = "Preserve WCF compat")]
232        [DllImport(ADVAPI32, CharSet = System.Runtime.InteropServices.CharSet.Unicode, SetLastError = true)]
233        [ResourceExposure(ResourceScope.Machine)]
234        [Fx.Tag.SecurityNote(Critical = "Returns security critical type SafeEventLogWriteHandle")]
235        [SecurityCritical]
236        internal static extern SafeEventLogWriteHandle RegisterEventSource(string uncServerName, string sourceName);
237    }
238}