PageRenderTime 53ms CodeModel.GetById 27ms RepoModel.GetById 1ms app.codeStats 0ms

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