Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove unused PInvokes #2828

Merged
merged 6 commits into from
Nov 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -745,13 +745,11 @@
</ItemGroup>
<ItemGroup>
<Compile Include="Common\Microsoft\Data\Common\NameValuePermission.cs" />
<Compile Include="Microsoft\Data\Common\AdapterSwitches.cs" />
<Compile Include="Microsoft\Data\Common\DbConnectionOptions.cs" />
<Compile Include="Microsoft\Data\Common\DbConnectionString.cs" />
<Compile Include="Microsoft\Data\Common\GreenMethods.cs" />
<Compile Include="Microsoft\Data\Common\NativeMethods.cs" />
<Compile Include="Microsoft\Data\Common\SafeNativeMethods.cs" />
<Compile Include="Microsoft\Data\Common\UnsafeNativeMethods.cs" />
<Compile Include="Microsoft\Data\Interop\SNINativeManagedWrapperARM64.cs" />
<Compile Include="Microsoft\Data\Interop\SNINativeManagedWrapperX64.cs" />
<Compile Include="Microsoft\Data\Interop\SNINativeManagedWrapperX86.cs" />
Expand Down Expand Up @@ -792,8 +790,6 @@
<Compile Include="Microsoft\Data\SqlClient\TdsParserStateObject.netfx.cs" />
<Compile Include="Microsoft\Data\SqlTypes\SqlStreamChars.cs" />
<Compile Include="Microsoft\Data\SqlTypes\SqlTypeWorkarounds.netfx.cs" />
<Compile Include="Microsoft\Data\SqlTypes\UnsafeNativeMethods.cs" />
<Compile Include="Misc\ExternDll.cs" />
</ItemGroup>
<!-- Resources -->
<ItemGroup>
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -8,65 +8,37 @@

namespace Microsoft.Data.Common
{

internal static class NativeMethods
{

//[Guid("0c733a1e-2a1c-11ce-ade5-00aa0044773d"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown), ComImport]
//internal interface ISourcesRowset {

// [PreserveSig] System.Data.OleDb.OleDbHResult GetSourcesRowset(
// [In] IntPtr pUnkOuter,
// [In, MarshalAs(UnmanagedType.LPStruct)] Guid riid,
// [In] int cPropertySets,
// [In] IntPtr rgProperties,
// [Out, MarshalAs(UnmanagedType.Interface)] out object ppRowset);
//}

[Guid("0C733A5E-2A1C-11CE-ADE5-00AA0044773D"), InterfaceTypeAttribute(ComInterfaceType.InterfaceIsIUnknown), ComImport]
internal interface ITransactionJoin
{
[Obsolete("not used", true)]
[PreserveSig]
int GetOptionsObject(
/*deleted parameter signature*/);

void JoinTransaction(
[In, MarshalAs(UnmanagedType.Interface)] object punkTransactionCoord,
[In] Int32 isoLevel,
[In] Int32 isoFlags,
[In] IntPtr pOtherOptions);
}

[DllImport(ExternDll.Kernel32, ExactSpelling = true, CharSet = System.Runtime.InteropServices.CharSet.Auto)]
[DllImport(Interop.Libraries.Kernel32, ExactSpelling = true, CharSet = System.Runtime.InteropServices.CharSet.Auto)]
[ResourceExposure(ResourceScope.Machine)]
static internal extern IntPtr MapViewOfFile(IntPtr hFileMappingObject, int dwDesiredAccess, int dwFileOffsetHigh, int dwFileOffsetLow, IntPtr dwNumberOfBytesToMap);

// OpenFileMappingA contains a security venerability, in the unicode->ansi conversion
// Its possible to spoof the directory and construct ../ sequences, See FxCop Warning
// Specify marshaling for pinvoke string arguments
[DllImport(ExternDll.Kernel32, CharSet = System.Runtime.InteropServices.CharSet.Ansi, BestFitMapping = false, ThrowOnUnmappableChar = true)]
[DllImport(Interop.Libraries.Kernel32, CharSet = System.Runtime.InteropServices.CharSet.Ansi, BestFitMapping = false, ThrowOnUnmappableChar = true)]
// [DllImport(ExternDll.Kernel32, CharSet=System.Runtime.InteropServices.CharSet.Ansi)]
[ResourceExposure(ResourceScope.Machine)]
static internal extern IntPtr OpenFileMappingA(int dwDesiredAccess, bool bInheritHandle, [MarshalAs(UnmanagedType.LPStr)] string lpName);

// CreateFileMappingA contains a security venerability, in the unicode->ansi conversion
// Its possible to spoof the directory and construct ../ sequences, See FxCop Warning
// Specify marshaling for pinvoke string arguments
[DllImport(ExternDll.Kernel32, CharSet = System.Runtime.InteropServices.CharSet.Ansi, BestFitMapping = false, ThrowOnUnmappableChar = true)]
[DllImport(Interop.Libraries.Kernel32, CharSet = System.Runtime.InteropServices.CharSet.Ansi, BestFitMapping = false, ThrowOnUnmappableChar = true)]
// [DllImport(ExternDll.Kernel32, CharSet=System.Runtime.InteropServices.CharSet.Ansi)]
[ResourceExposure(ResourceScope.Machine)]
static internal extern IntPtr CreateFileMappingA(IntPtr hFile, IntPtr pAttr, int flProtect, int dwMaximumSizeHigh, int dwMaximumSizeLow, [MarshalAs(UnmanagedType.LPStr)] string lpName);

[DllImport(ExternDll.Kernel32, ExactSpelling = true, CharSet = System.Runtime.InteropServices.CharSet.Auto)]
[DllImport(Interop.Libraries.Kernel32, ExactSpelling = true, CharSet = System.Runtime.InteropServices.CharSet.Auto)]
[ResourceExposure(ResourceScope.Machine)]
static internal extern bool UnmapViewOfFile(IntPtr lpBaseAddress);

[DllImport(ExternDll.Kernel32, ExactSpelling = true, CharSet = System.Runtime.InteropServices.CharSet.Auto, SetLastError = true)]
[DllImport(Interop.Libraries.Kernel32, ExactSpelling = true, CharSet = System.Runtime.InteropServices.CharSet.Auto, SetLastError = true)]
[ResourceExposure(ResourceScope.Machine)]
static internal extern bool CloseHandle(IntPtr handle);

[DllImport(ExternDll.Advapi32, ExactSpelling = true, CharSet = System.Runtime.InteropServices.CharSet.Auto, SetLastError = true)]
[DllImport(Interop.Libraries.Advapi32, ExactSpelling = true, CharSet = System.Runtime.InteropServices.CharSet.Auto, SetLastError = true)]
[ResourceExposure(ResourceScope.None)]
static internal extern bool AllocateAndInitializeSid(
IntPtr pIdentifierAuthority, // authority
Expand All @@ -82,48 +54,48 @@ static internal extern bool AllocateAndInitializeSid(
ref IntPtr pSid); // SID


[DllImport(ExternDll.Advapi32, ExactSpelling = true, CharSet = System.Runtime.InteropServices.CharSet.Auto, SetLastError = true)]
[DllImport(Interop.Libraries.Advapi32, ExactSpelling = true, CharSet = System.Runtime.InteropServices.CharSet.Auto, SetLastError = true)]
[ResourceExposure(ResourceScope.None)]
static internal extern int GetLengthSid(
IntPtr pSid); // SID to query

[DllImport(ExternDll.Advapi32, ExactSpelling = true, CharSet = System.Runtime.InteropServices.CharSet.Auto, SetLastError = true)]
[DllImport(Interop.Libraries.Advapi32, ExactSpelling = true, CharSet = System.Runtime.InteropServices.CharSet.Auto, SetLastError = true)]
[ResourceExposure(ResourceScope.None)]
static internal extern bool InitializeAcl(
IntPtr pAcl, // ACL
int nAclLength, // size of ACL
int dwAclRevision); // revision level of ACL

[DllImport(ExternDll.Advapi32, ExactSpelling = true, CharSet = System.Runtime.InteropServices.CharSet.Auto, SetLastError = true)]
[DllImport(Interop.Libraries.Advapi32, ExactSpelling = true, CharSet = System.Runtime.InteropServices.CharSet.Auto, SetLastError = true)]
[ResourceExposure(ResourceScope.None)]
static internal extern bool AddAccessDeniedAce(
IntPtr pAcl, // access control list
int dwAceRevision, // ACL revision level
int AccessMask, // access mask
IntPtr pSid); // security identifier

[DllImport(ExternDll.Advapi32, ExactSpelling = true, CharSet = System.Runtime.InteropServices.CharSet.Auto, SetLastError = true)]
[DllImport(Interop.Libraries.Advapi32, ExactSpelling = true, CharSet = System.Runtime.InteropServices.CharSet.Auto, SetLastError = true)]
[ResourceExposure(ResourceScope.None)]
static internal extern bool AddAccessAllowedAce(
IntPtr pAcl, // access control list
int dwAceRevision, // ACL revision level
uint AccessMask, // access mask
IntPtr pSid); // security identifier

[DllImport(ExternDll.Advapi32, ExactSpelling = true, CharSet = System.Runtime.InteropServices.CharSet.Auto, SetLastError = true)]
[DllImport(Interop.Libraries.Advapi32, ExactSpelling = true, CharSet = System.Runtime.InteropServices.CharSet.Auto, SetLastError = true)]
[ResourceExposure(ResourceScope.None)]
static internal extern bool InitializeSecurityDescriptor(
IntPtr pSecurityDescriptor, // SD
int dwRevision); // revision level
[DllImport(ExternDll.Advapi32, ExactSpelling = true, CharSet = System.Runtime.InteropServices.CharSet.Auto, SetLastError = true)]
[DllImport(Interop.Libraries.Advapi32, ExactSpelling = true, CharSet = System.Runtime.InteropServices.CharSet.Auto, SetLastError = true)]
[ResourceExposure(ResourceScope.None)]
static internal extern bool SetSecurityDescriptorDacl(
IntPtr pSecurityDescriptor, // SD
bool bDaclPresent, // DACL presence
IntPtr pDacl, // DACL
bool bDaclDefaulted); // default DACL

[DllImport(ExternDll.Advapi32, ExactSpelling = true, CharSet = System.Runtime.InteropServices.CharSet.Auto, SetLastError = true)]
[DllImport(Interop.Libraries.Advapi32, ExactSpelling = true, CharSet = System.Runtime.InteropServices.CharSet.Auto, SetLastError = true)]
[ResourceExposure(ResourceScope.None)]
static internal extern IntPtr FreeSid(
IntPtr pSid); // SID to free
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,137 +16,24 @@ namespace Microsoft.Data.Common
[SuppressUnmanagedCodeSecurityAttribute()]
internal static class SafeNativeMethods
{

[DllImport(ExternDll.Ole32, SetLastError = false)]
[ReliabilityContract(Consistency.WillNotCorruptState, Cer.MayFail)]
[ResourceExposure(ResourceScope.None)]
static internal extern IntPtr CoTaskMemAlloc(IntPtr cb);

[DllImport(ExternDll.Ole32, SetLastError = false)]
[ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
[ResourceExposure(ResourceScope.None)]
static internal extern void CoTaskMemFree(IntPtr handle);

[DllImport(ExternDll.Kernel32, CharSet = CharSet.Unicode, PreserveSig = true)]
[ResourceExposure(ResourceScope.None)]
static internal extern int GetUserDefaultLCID();

[DllImport(ExternDll.Kernel32, PreserveSig = true)]
[ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
[ResourceExposure(ResourceScope.None)]
static internal extern void ZeroMemory(IntPtr dest, IntPtr length);

// <WARNING>
// Using the int versions of the Increment() and Decrement() methods is correct.
// Please check \fx\src\Data\System\Data\Odbc\OdbcHandle.cs for the memory layout.
// </WARNING>

// <NDPWHIDBEY 18133>
// The following casting operations require these three methods to be unsafe. This is
// a workaround for this issue to meet the M1 exit criteria. We need to revisit this in M2.

[ReliabilityContract(Consistency.WillNotCorruptState, Cer.MayFail)]
static internal unsafe IntPtr InterlockedExchangePointer(
IntPtr lpAddress,
IntPtr lpValue)
{
IntPtr previousPtr;
IntPtr actualPtr = *(IntPtr*)lpAddress.ToPointer();

do
{
previousPtr = actualPtr;
actualPtr = Interlocked.CompareExchange(ref *(IntPtr*)lpAddress.ToPointer(), lpValue, previousPtr);
}
while (actualPtr != previousPtr);

return actualPtr;
}

// http://msdn.microsoft.com/library/default.asp?url=/library/en-us/sysinfo/base/getcomputernameex.asp
[DllImport(ExternDll.Kernel32, CharSet = CharSet.Unicode, EntryPoint = "GetComputerNameExW", SetLastError = true)]
[DllImport(Interop.Libraries.Kernel32, CharSet = CharSet.Unicode, EntryPoint = "GetComputerNameExW", SetLastError = true)]
[ResourceExposure(ResourceScope.None)]
static internal extern int GetComputerNameEx(int nameType, StringBuilder nameBuffer, ref int bufferSize);

[DllImport(ExternDll.Kernel32, CharSet = System.Runtime.InteropServices.CharSet.Auto)]
[DllImport(Interop.Libraries.Kernel32, CharSet = System.Runtime.InteropServices.CharSet.Auto)]
[ResourceExposure(ResourceScope.Process)]
static internal extern int GetCurrentProcessId();

[DllImport(ExternDll.Kernel32, CharSet = CharSet.Auto, BestFitMapping = false, ThrowOnUnmappableChar = true)]
[DllImport(Interop.Libraries.Kernel32, CharSet = CharSet.Auto, BestFitMapping = false, ThrowOnUnmappableChar = true)]
// [DllImport(ExternDll.Kernel32, CharSet=CharSet.Auto)]
[ResourceExposure(ResourceScope.Process)]
static internal extern IntPtr GetModuleHandle([MarshalAs(UnmanagedType.LPTStr), In] string moduleName/*lpctstr*/);

[DllImport(ExternDll.Kernel32, CharSet = CharSet.Ansi, BestFitMapping = false, ThrowOnUnmappableChar = true, SetLastError = true)]
[DllImport(Interop.Libraries.Kernel32, CharSet = CharSet.Ansi, BestFitMapping = false, ThrowOnUnmappableChar = true, SetLastError = true)]
// [DllImport(ExternDll.Kernel32, CharSet=CharSet.Ansi)]
[ResourceExposure(ResourceScope.None)]
static internal extern IntPtr GetProcAddress(IntPtr HModule, [MarshalAs(UnmanagedType.LPStr), In] string funcName/*lpcstr*/);

[DllImport(ExternDll.Kernel32, SetLastError = true)]
[ReliabilityContract(Consistency.WillNotCorruptState, Cer.MayFail)]
[ResourceExposure(ResourceScope.None)]
static internal extern IntPtr LocalAlloc(int flags, IntPtr countOfBytes);

[DllImport(ExternDll.Kernel32, SetLastError = true)]
[ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
[ResourceExposure(ResourceScope.None)]
static internal extern IntPtr LocalFree(IntPtr handle);

[DllImport(ExternDll.Oleaut32, CharSet = CharSet.Unicode)]
[ReliabilityContract(Consistency.WillNotCorruptState, Cer.MayFail)]
[ResourceExposure(ResourceScope.None)]
internal static extern IntPtr SysAllocStringLen(String src, int len); // BSTR

[DllImport(ExternDll.Oleaut32)]
[ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
[ResourceExposure(ResourceScope.None)]
internal static extern void SysFreeString(IntPtr bstr);

// only using this to clear existing error info with null
[DllImport(ExternDll.Oleaut32, CharSet = CharSet.Unicode, PreserveSig = false)]
// TLS values are preserved between threads, need to check that we use this API to clear the error state only.
[ResourceExposure(ResourceScope.Process)]
static private extern void SetErrorInfo(Int32 dwReserved, IntPtr pIErrorInfo);

[DllImport(ExternDll.Kernel32, SetLastError = true)]
[ReliabilityContract(Consistency.WillNotCorruptState, Cer.MayFail)]
[ResourceExposure(ResourceScope.Machine)]
static internal extern int ReleaseSemaphore(IntPtr handle, int releaseCount, IntPtr previousCount);

[DllImport(ExternDll.Kernel32, SetLastError = true)]
[ReliabilityContract(Consistency.WillNotCorruptState, Cer.MayFail)]
[ResourceExposure(ResourceScope.None)]
static internal extern int WaitForMultipleObjectsEx(uint nCount, IntPtr lpHandles, bool bWaitAll, uint dwMilliseconds, bool bAlertable);

[DllImport(ExternDll.Kernel32/*, SetLastError=true*/)]
[ReliabilityContract(Consistency.WillNotCorruptState, Cer.MayFail)]
[ResourceExposure(ResourceScope.None)]
static internal extern int WaitForSingleObjectEx(IntPtr lpHandles, uint dwMilliseconds, bool bAlertable);

[DllImport(ExternDll.Ole32, PreserveSig = false)]
[ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
[ResourceExposure(ResourceScope.None)]
static internal extern void PropVariantClear(IntPtr pObject);

[DllImport(ExternDll.Oleaut32, PreserveSig = false)]
[ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
[ResourceExposure(ResourceScope.None)]
static internal extern void VariantClear(IntPtr pObject);

sealed internal class Wrapper
{

private Wrapper() { }

// SxS: clearing error information is considered safe
[ResourceExposure(ResourceScope.None)]
[ResourceConsumption(ResourceScope.Process, ResourceScope.Process)]
static internal void ClearErrorInfo()
{ // MDAC 68199
SafeNativeMethods.SetErrorInfo(0, ADP.s_ptrZero);
}
}


}
}
Loading
Loading