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