SafeNativeMethods.cs 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. //------------------------------------------------------------------------------
  2. // <copyright file="SafeNativeMethods.cs" company="Microsoft">
  3. // Copyright (c) Microsoft Corporation. All rights reserved.
  4. // </copyright>
  5. // <owner current="true" primary="true">Microsoft</owner>
  6. // <owner current="true" primary="false">Microsoft</owner>
  7. // <owner current="true" primary="false">Microsoft</owner>
  8. //------------------------------------------------------------------------------
  9. using System;
  10. using System.Runtime.CompilerServices;
  11. using System.Runtime.InteropServices;
  12. using System.Security;
  13. using System.Security.Permissions;
  14. using System.Text;
  15. using System.Threading;
  16. using System.Runtime.ConstrainedExecution;
  17. using System.Runtime.Versioning;
  18. namespace System.Data.Common {
  19. [SuppressUnmanagedCodeSecurityAttribute()]
  20. internal static class SafeNativeMethods {
  21. [DllImport(ExternDll.Ole32, SetLastError=false)]
  22. [ReliabilityContract(Consistency.WillNotCorruptState, Cer.MayFail)]
  23. [ResourceExposure(ResourceScope.None)]
  24. static internal extern IntPtr CoTaskMemAlloc(IntPtr cb);
  25. [DllImport(ExternDll.Ole32, SetLastError=false)]
  26. [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
  27. [ResourceExposure(ResourceScope.None)]
  28. static internal extern void CoTaskMemFree(IntPtr handle);
  29. [DllImport(ExternDll.Kernel32, CharSet=CharSet.Unicode, PreserveSig=true)]
  30. [ResourceExposure(ResourceScope.None)]
  31. static internal extern int GetUserDefaultLCID();
  32. [DllImport(ExternDll.Kernel32, PreserveSig=true)]
  33. [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
  34. [ResourceExposure(ResourceScope.None)]
  35. static internal extern void ZeroMemory(IntPtr dest, IntPtr length);
  36. // <WARNING>
  37. // Using the int versions of the Increment() and Decrement() methods is correct.
  38. // Please check \fx\src\Data\System\Data\Odbc\OdbcHandle.cs for the memory layout.
  39. // </WARNING>
  40. // <NDPWHIDBEY 18133>
  41. // The following casting operations require these three methods to be unsafe. This is
  42. // a workaround for this issue to meet the M1 exit criteria. We need to revisit this in M2.
  43. [ReliabilityContract(Consistency.WillNotCorruptState, Cer.MayFail)]
  44. static internal unsafe IntPtr InterlockedExchangePointer(
  45. IntPtr lpAddress,
  46. IntPtr lpValue)
  47. {
  48. IntPtr previousPtr;
  49. IntPtr actualPtr = *(IntPtr *)lpAddress.ToPointer();
  50. do {
  51. previousPtr = actualPtr;
  52. actualPtr = Interlocked.CompareExchange(ref *(IntPtr *)lpAddress.ToPointer(), lpValue, previousPtr);
  53. }
  54. while (actualPtr != previousPtr);
  55. return actualPtr;
  56. }
  57. // http://msdn.microsoft.com/library/default.asp?url=/library/en-us/sysinfo/base/getcomputernameex.asp
  58. [DllImport(ExternDll.Kernel32, CharSet = CharSet.Unicode, EntryPoint="GetComputerNameExW", SetLastError=true)]
  59. [ResourceExposure(ResourceScope.None)]
  60. static internal extern int GetComputerNameEx(int nameType, StringBuilder nameBuffer, ref int bufferSize);
  61. [DllImport(ExternDll.Kernel32, CharSet=System.Runtime.InteropServices.CharSet.Auto)]
  62. [ResourceExposure(ResourceScope.Process)]
  63. static internal extern int GetCurrentProcessId();
  64. [DllImport(ExternDll.Kernel32, CharSet=CharSet.Auto, BestFitMapping=false, ThrowOnUnmappableChar=true)]
  65. // [DllImport(ExternDll.Kernel32, CharSet=CharSet.Auto)]
  66. [ResourceExposure(ResourceScope.Process)]
  67. static internal extern IntPtr GetModuleHandle([MarshalAs(UnmanagedType.LPTStr), In] string moduleName/*lpctstr*/);
  68. [DllImport(ExternDll.Kernel32, CharSet = CharSet.Ansi, BestFitMapping = false, ThrowOnUnmappableChar = true, SetLastError = true)]
  69. // [DllImport(ExternDll.Kernel32, CharSet=CharSet.Ansi)]
  70. [ResourceExposure(ResourceScope.None)]
  71. static internal extern IntPtr GetProcAddress(IntPtr HModule, [MarshalAs(UnmanagedType.LPStr), In] string funcName/*lpcstr*/);
  72. [DllImport(ExternDll.Kernel32, SetLastError=true)]
  73. [ReliabilityContract(Consistency.WillNotCorruptState, Cer.MayFail)]
  74. [ResourceExposure(ResourceScope.None)]
  75. static internal extern IntPtr LocalAlloc(int flags, IntPtr countOfBytes);
  76. [DllImport(ExternDll.Kernel32, SetLastError=true)]
  77. [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
  78. [ResourceExposure(ResourceScope.None)]
  79. static internal extern IntPtr LocalFree(IntPtr handle);
  80. [DllImport(ExternDll.Oleaut32, CharSet=CharSet.Unicode)]
  81. [ReliabilityContract(Consistency.WillNotCorruptState, Cer.MayFail)]
  82. [ResourceExposure(ResourceScope.None)]
  83. internal static extern IntPtr SysAllocStringLen(String src, int len); // BSTR
  84. [DllImport(ExternDll.Oleaut32)]
  85. [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
  86. [ResourceExposure(ResourceScope.None)]
  87. internal static extern void SysFreeString(IntPtr bstr);
  88. // only using this to clear existing error info with null
  89. [DllImport(ExternDll.Oleaut32, CharSet=CharSet.Unicode, PreserveSig=false)]
  90. // TLS values are preserved between threads, need to check that we use this API to clear the error state only.
  91. [ResourceExposure(ResourceScope.Process)]
  92. static private extern void SetErrorInfo(Int32 dwReserved, IntPtr pIErrorInfo);
  93. [DllImport(ExternDll.Kernel32, SetLastError=true)]
  94. [ReliabilityContract(Consistency.WillNotCorruptState, Cer.MayFail)]
  95. [ResourceExposure(ResourceScope.Machine)]
  96. static internal extern int ReleaseSemaphore(IntPtr handle, int releaseCount, IntPtr previousCount);
  97. [DllImport(ExternDll.Kernel32, SetLastError=true)]
  98. [ReliabilityContract(Consistency.WillNotCorruptState, Cer.MayFail)]
  99. [ResourceExposure(ResourceScope.None)]
  100. static internal extern int WaitForMultipleObjectsEx(uint nCount, IntPtr lpHandles, bool bWaitAll, uint dwMilliseconds, bool bAlertable);
  101. [DllImport(ExternDll.Kernel32/*, SetLastError=true*/)]
  102. [ReliabilityContract(Consistency.WillNotCorruptState, Cer.MayFail)]
  103. [ResourceExposure(ResourceScope.None)]
  104. static internal extern int WaitForSingleObjectEx(IntPtr lpHandles, uint dwMilliseconds, bool bAlertable);
  105. [DllImport(ExternDll.Ole32, PreserveSig=false)]
  106. [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
  107. [ResourceExposure(ResourceScope.None)]
  108. static internal extern void PropVariantClear(IntPtr pObject);
  109. [DllImport(ExternDll.Oleaut32, PreserveSig=false)]
  110. [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
  111. [ResourceExposure(ResourceScope.None)]
  112. static internal extern void VariantClear(IntPtr pObject);
  113. sealed internal class Wrapper {
  114. private Wrapper() { }
  115. // SxS: clearing error information is considered safe
  116. [ResourceExposure(ResourceScope.None)]
  117. [ResourceConsumption(ResourceScope.Process, ResourceScope.Process)]
  118. static internal void ClearErrorInfo() { // MDAC 68199
  119. SafeNativeMethods.SetErrorInfo(0, ADP.PtrZero);
  120. }
  121. }
  122. }
  123. }