PageRenderTime 27ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/mcs/class/referencesource/System.ServiceModel.Internals/System/Runtime/Interop/UnsafeNativeMethods.cs

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