Updates referencesource to .NET 4.7
[mono.git] / mcs / class / referencesource / System.Data / System / Data / Common / SafeNativeMethods.cs
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
10 using System;
11 using System.Runtime.CompilerServices;
12 using System.Runtime.InteropServices;
13 using System.Security;
14 using System.Security.Permissions;
15 using System.Text;
16 using System.Threading;
17 using System.Runtime.ConstrainedExecution;
18 using System.Runtime.Versioning;
19
20 namespace System.Data.Common {
21
22     [SuppressUnmanagedCodeSecurityAttribute()]
23     internal static class SafeNativeMethods {
24     
25         [DllImport(ExternDll.Ole32, SetLastError=false)]
26         [ReliabilityContract(Consistency.WillNotCorruptState, Cer.MayFail)]
27         [ResourceExposure(ResourceScope.None)]
28         static internal extern IntPtr CoTaskMemAlloc(IntPtr cb);
29
30         [DllImport(ExternDll.Ole32, SetLastError=false)]
31         [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
32         [ResourceExposure(ResourceScope.None)]
33         static internal extern void CoTaskMemFree(IntPtr handle);
34
35         [DllImport(ExternDll.Kernel32, CharSet=CharSet.Unicode, PreserveSig=true)]
36         [ResourceExposure(ResourceScope.None)]
37         static internal extern int GetUserDefaultLCID();
38
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);
43
44         // <WARNING>
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.
47         // </WARNING>
48
49         // <NDPWHIDBEY 18133>
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.
52
53         [ReliabilityContract(Consistency.WillNotCorruptState, Cer.MayFail)]
54         static internal unsafe IntPtr InterlockedExchangePointer(
55                 IntPtr lpAddress,
56                 IntPtr lpValue)
57         {
58             IntPtr  previousPtr;
59             IntPtr  actualPtr = *(IntPtr *)lpAddress.ToPointer();
60
61             do {
62                 previousPtr = actualPtr;
63                 actualPtr   = Interlocked.CompareExchange(ref *(IntPtr *)lpAddress.ToPointer(), lpValue, previousPtr);
64             }
65             while (actualPtr != previousPtr);
66
67             return actualPtr;
68         }
69
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);
74
75         [DllImport(ExternDll.Kernel32, CharSet=System.Runtime.InteropServices.CharSet.Auto)]
76         [ResourceExposure(ResourceScope.Process)]
77         static internal extern int GetCurrentProcessId();
78
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*/);
83
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*/);
88
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);
93
94         [DllImport(ExternDll.Kernel32, SetLastError=true)]
95         [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
96         [ResourceExposure(ResourceScope.None)]
97         static internal extern IntPtr LocalFree(IntPtr handle);
98
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
103
104         [DllImport(ExternDll.Oleaut32)]
105         [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
106         [ResourceExposure(ResourceScope.None)]
107         internal static extern void SysFreeString(IntPtr bstr);
108
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);
114
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);
119
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);
124
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);
129
130         [DllImport(ExternDll.Ole32, PreserveSig=false)]
131         [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
132         [ResourceExposure(ResourceScope.None)]
133         static internal extern void PropVariantClear(IntPtr pObject);
134
135         [DllImport(ExternDll.Oleaut32, PreserveSig=false)]
136         [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
137         [ResourceExposure(ResourceScope.None)]
138         static internal extern void VariantClear(IntPtr pObject);
139
140         sealed internal class Wrapper {
141
142             private Wrapper() { }
143
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);
149             }
150         }
151
152
153     }
154 }