Skip to content

Commit

Permalink
Merge | NativeMethods/SafeNativeMethods (#2997)
Browse files Browse the repository at this point in the history
* Moving SafeNativeMethods to Interop.Windows.Kernel32 namespace, migrating usages

* Move Kernel32 calls from NativeMethods to Interop.Windows.Kernel32

* Moving AdvApi32 interop calls to Interop.Windows.AdvApi32, migrating usages

* Remove redundant safe method in SNINativeMethodWrapper, migrate usages
  • Loading branch information
benrr101 authored Nov 13, 2024
1 parent 35c317b commit 4052186
Show file tree
Hide file tree
Showing 11 changed files with 168 additions and 153 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,8 @@
</ItemGroup>
<!-- Contains common items shared between NetFx and NetCore -->
<ItemGroup>
<Compile Include="$(CommonSourceRoot)Interop\Windows\SystemErrors.cs">
<Link>Interop\SystemErrors.cs</Link>
<Compile Include="$(CommonSourceRoot)Interop\Windows\AdvApi32\AdvApi32.netfx.cs">
<Link>Interop\AdvApi32\AdvApi32.netfx.cs</Link>
</Compile>
<Compile Include="$(CommonSourceRoot)Interop\Windows\Kernel32\FileTypes.cs">
<Link>Interop\Kernel32\FileTypes.cs</Link>
Expand All @@ -115,6 +115,9 @@
<Compile Include="$(CommonSourceRoot)\Interop\Windows\Kernel32\Kernel32.cs">
<Link>Interop\Kernel32\Kernel32.cs</Link>
</Compile>
<Compile Include="$(CommonSourceRoot)\Interop\Windows\Kernel32\Kernel32Safe.netfx.cs">
<Link>Interop\Kernel32\Kernel32Safe.netfx.cs</Link>
</Compile>
<Compile Include="$(CommonSourceRoot)\Interop\Windows\NtDll\CreateDisposition.cs">
<Link>Interop\NtDll\CreateDisposition.cs</Link>
</Compile>
Expand Down Expand Up @@ -145,6 +148,9 @@
<Compile Include="$(CommonSourceRoot)\Interop\Windows\NtDll\SecurityQualityOfService.cs">
<Link>Interop\NtDll\SecurityQualityOfService.cs</Link>
</Compile>
<Compile Include="$(CommonSourceRoot)Interop\Windows\SystemErrors.cs">
<Link>Interop\SystemErrors.cs</Link>
</Compile>
<Compile Include="$(CommonSourceRoot)Interop\Windows\UnicodeString.cs">
<Link>Interop\UnicodeString.cs</Link>
</Compile>
Expand Down Expand Up @@ -748,8 +754,6 @@
<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\Interop\SNINativeManagedWrapperARM64.cs" />
<Compile Include="Microsoft\Data\Interop\SNINativeManagedWrapperX64.cs" />
<Compile Include="Microsoft\Data\Interop\SNINativeManagedWrapperX86.cs" />
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -1236,15 +1236,6 @@ private static void MarshalConsumerInfo(ConsumerInfo consumerInfo, ref Sni_Consu
}
}

namespace Microsoft.Data
{
internal static partial class SafeNativeMethods
{
[DllImport("kernel32.dll", CharSet = CharSet.Ansi, BestFitMapping = false, ThrowOnUnmappableChar = true, SetLastError = true)]
internal static extern IntPtr GetProcAddress(IntPtr HModule, [MarshalAs(UnmanagedType.LPStr), In] string funcName);
}
}

namespace Microsoft.Data
{
internal static class Win32NativeMethods
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
using System.Security;
using System.Text;
using System.Threading;
using Interop.Windows.Kernel32;
using Microsoft.Data.SqlClient;

namespace Microsoft.Data
Expand Down Expand Up @@ -121,7 +122,7 @@ static LocalDBCreateInstanceDelegate LocalDBCreateInstance
Monitor.Enter(s_dllLock, ref lockTaken);
if (s_localDBCreateInstance == null)
{
IntPtr functionAddr = SafeNativeMethods.GetProcAddress(UserInstanceDLLHandle, "LocalDBCreateInstance");
IntPtr functionAddr = Kernel32Safe.GetProcAddress(UserInstanceDLLHandle, "LocalDBCreateInstance");

if (functionAddr == IntPtr.Zero)
{
Expand Down Expand Up @@ -162,7 +163,7 @@ static LocalDBFormatMessageDelegate LocalDBFormatMessage
Monitor.Enter(s_dllLock, ref lockTaken);
if (s_localDBFormatMessage == null)
{
IntPtr functionAddr = SafeNativeMethods.GetProcAddress(UserInstanceDLLHandle, "LocalDBFormatMessage");
IntPtr functionAddr = Kernel32Safe.GetProcAddress(UserInstanceDLLHandle, "LocalDBFormatMessage");

if (functionAddr == IntPtr.Zero)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,10 @@
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Interop.Windows.AdvApi32;
using Microsoft.Data.ProviderBase;
using Microsoft.SqlServer.Server;
using Interop.Windows.Kernel32;

[assembly: InternalsVisibleTo("System.Data.DataSetExtensions, PublicKey=" + Microsoft.Data.SqlClient.AssemblyRef.EcmaPublicKeyFull)] // DevDiv Bugs 92166
// NOTE: The current Microsoft.VSDesigner editor attributes are implemented for System.Data.SqlClient, and are not publicly available.
Expand Down Expand Up @@ -2532,7 +2534,7 @@ internal void OnInfoMessage(SqlInfoMessageEventArgs imevent, out bool notified)
private void CheckSQLDebugOnConnect()
{
IntPtr hFileMap;
uint pid = (uint)SafeNativeMethods.GetCurrentProcessId();
uint pid = (uint)Kernel32Safe.GetCurrentProcessId();

string mapFileName;

Expand All @@ -2548,11 +2550,11 @@ private void CheckSQLDebugOnConnect()

mapFileName = mapFileName + pid.ToString(CultureInfo.InvariantCulture);

hFileMap = NativeMethods.OpenFileMappingA(0x4/*FILE_MAP_READ*/, false, mapFileName);
hFileMap = Kernel32.OpenFileMappingA(0x4/*FILE_MAP_READ*/, false, mapFileName);

if (ADP.s_ptrZero != hFileMap)
{
IntPtr pMemMap = NativeMethods.MapViewOfFile(hFileMap, 0x4/*FILE_MAP_READ*/, 0, 0, IntPtr.Zero);
IntPtr pMemMap = Kernel32.MapViewOfFile(hFileMap, 0x4/*FILE_MAP_READ*/, 0, 0, IntPtr.Zero);
if (ADP.s_ptrZero != pMemMap)
{
SqlDebugContext sdc = new SqlDebugContext();
Expand Down Expand Up @@ -3098,12 +3100,12 @@ private void Dispose(bool disposing)
}
if (pMemMap != IntPtr.Zero)
{
NativeMethods.UnmapViewOfFile(pMemMap);
Kernel32.UnmapViewOfFile(pMemMap);
pMemMap = IntPtr.Zero;
}
if (hMemMap != IntPtr.Zero)
{
NativeMethods.CloseHandle(hMemMap);
Kernel32.CloseHandle(hMemMap);
hMemMap = IntPtr.Zero;
}
active = false;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;

namespace Interop.Windows.AdvApi32
{
internal class AdvApi32
{
private const string DllName = "advapi32.dll";

[DllImport(DllName, ExactSpelling = true, CharSet = CharSet.Auto, SetLastError = true)]
[ResourceExposure(ResourceScope.None)]
internal static extern bool AddAccessAllowedAce(IntPtr pAcl, int dwAceRevision, uint AccessMask, IntPtr pSid);

[DllImport(DllName, ExactSpelling = true, CharSet = CharSet.Auto, SetLastError = true)]
[ResourceExposure(ResourceScope.None)]
internal static extern bool AddAccessDeniedAce(IntPtr pAcl, int dwAceRevision, int AccessMask, IntPtr pSid);

[DllImport(DllName, ExactSpelling = true, CharSet = CharSet.Auto, SetLastError = true)]
[ResourceExposure(ResourceScope.None)]
internal static extern bool AllocateAndInitializeSid(
IntPtr pIdentifierAuthority,
byte nSubAuthorityCount,
int dwSubAuthority0,
int dwSubAuthority1,
int dwSubAuthority2,
int dwSubAuthority3,
int dwSubAuthority4,
int dwSubAuthority5,
int dwSubAuthority6,
int dwSubAuthority7,
ref IntPtr pSid);


[DllImport(DllName, ExactSpelling = true, CharSet = CharSet.Auto, SetLastError = true)]
[ResourceExposure(ResourceScope.None)]
internal static extern IntPtr FreeSid(IntPtr pSid);

[DllImport(DllName, ExactSpelling = true, CharSet = CharSet.Auto, SetLastError = true)]
[ResourceExposure(ResourceScope.None)]
internal static extern int GetLengthSid(IntPtr pSid);

[DllImport(DllName, ExactSpelling = true, CharSet = CharSet.Auto, SetLastError = true)]
[ResourceExposure(ResourceScope.None)]
internal static extern bool InitializeAcl(IntPtr pAcl, int nAclLength, int dwAclRevision);

[DllImport(DllName, ExactSpelling = true, CharSet = CharSet.Auto, SetLastError = true)]
[ResourceExposure(ResourceScope.None)]
internal static extern bool InitializeSecurityDescriptor(IntPtr pSecurityDescriptor, int dwRevision);

[DllImport(DllName, ExactSpelling = true, CharSet = CharSet.Auto, SetLastError = true)]
[ResourceExposure(ResourceScope.None)]
internal static extern bool SetSecurityDescriptorDacl(
IntPtr pSecurityDescriptor,
bool bDaclPresent,
IntPtr pDacl,
bool bDaclDefaulted);
}
}
Loading

0 comments on commit 4052186

Please sign in to comment.