Skip to content

Commit 4052186

Browse files
authored
Merge | NativeMethods/SafeNativeMethods (#2997)
* 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
1 parent 35c317b commit 4052186

File tree

11 files changed

+168
-153
lines changed

11 files changed

+168
-153
lines changed

src/Microsoft.Data.SqlClient/netfx/src/Microsoft.Data.SqlClient.csproj

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -100,8 +100,8 @@
100100
</ItemGroup>
101101
<!-- Contains common items shared between NetFx and NetCore -->
102102
<ItemGroup>
103-
<Compile Include="$(CommonSourceRoot)Interop\Windows\SystemErrors.cs">
104-
<Link>Interop\SystemErrors.cs</Link>
103+
<Compile Include="$(CommonSourceRoot)Interop\Windows\AdvApi32\AdvApi32.netfx.cs">
104+
<Link>Interop\AdvApi32\AdvApi32.netfx.cs</Link>
105105
</Compile>
106106
<Compile Include="$(CommonSourceRoot)Interop\Windows\Kernel32\FileTypes.cs">
107107
<Link>Interop\Kernel32\FileTypes.cs</Link>
@@ -115,6 +115,9 @@
115115
<Compile Include="$(CommonSourceRoot)\Interop\Windows\Kernel32\Kernel32.cs">
116116
<Link>Interop\Kernel32\Kernel32.cs</Link>
117117
</Compile>
118+
<Compile Include="$(CommonSourceRoot)\Interop\Windows\Kernel32\Kernel32Safe.netfx.cs">
119+
<Link>Interop\Kernel32\Kernel32Safe.netfx.cs</Link>
120+
</Compile>
118121
<Compile Include="$(CommonSourceRoot)\Interop\Windows\NtDll\CreateDisposition.cs">
119122
<Link>Interop\NtDll\CreateDisposition.cs</Link>
120123
</Compile>
@@ -145,6 +148,9 @@
145148
<Compile Include="$(CommonSourceRoot)\Interop\Windows\NtDll\SecurityQualityOfService.cs">
146149
<Link>Interop\NtDll\SecurityQualityOfService.cs</Link>
147150
</Compile>
151+
<Compile Include="$(CommonSourceRoot)Interop\Windows\SystemErrors.cs">
152+
<Link>Interop\SystemErrors.cs</Link>
153+
</Compile>
148154
<Compile Include="$(CommonSourceRoot)Interop\Windows\UnicodeString.cs">
149155
<Link>Interop\UnicodeString.cs</Link>
150156
</Compile>
@@ -748,8 +754,6 @@
748754
<Compile Include="Microsoft\Data\Common\DbConnectionOptions.cs" />
749755
<Compile Include="Microsoft\Data\Common\DbConnectionString.cs" />
750756
<Compile Include="Microsoft\Data\Common\GreenMethods.cs" />
751-
<Compile Include="Microsoft\Data\Common\NativeMethods.cs" />
752-
<Compile Include="Microsoft\Data\Common\SafeNativeMethods.cs" />
753757
<Compile Include="Microsoft\Data\Interop\SNINativeManagedWrapperARM64.cs" />
754758
<Compile Include="Microsoft\Data\Interop\SNINativeManagedWrapperX64.cs" />
755759
<Compile Include="Microsoft\Data\Interop\SNINativeManagedWrapperX86.cs" />

src/Microsoft.Data.SqlClient/netfx/src/Microsoft/Data/Common/NativeMethods.cs

Lines changed: 0 additions & 106 deletions
This file was deleted.

src/Microsoft.Data.SqlClient/netfx/src/Microsoft/Data/Interop/SNINativeMethodWrapper.cs

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1236,15 +1236,6 @@ private static void MarshalConsumerInfo(ConsumerInfo consumerInfo, ref Sni_Consu
12361236
}
12371237
}
12381238

1239-
namespace Microsoft.Data
1240-
{
1241-
internal static partial class SafeNativeMethods
1242-
{
1243-
[DllImport("kernel32.dll", CharSet = CharSet.Ansi, BestFitMapping = false, ThrowOnUnmappableChar = true, SetLastError = true)]
1244-
internal static extern IntPtr GetProcAddress(IntPtr HModule, [MarshalAs(UnmanagedType.LPStr), In] string funcName);
1245-
}
1246-
}
1247-
12481239
namespace Microsoft.Data
12491240
{
12501241
internal static class Win32NativeMethods

src/Microsoft.Data.SqlClient/netfx/src/Microsoft/Data/SqlClient/LocalDBAPI.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
using System.Security;
1313
using System.Text;
1414
using System.Threading;
15+
using Interop.Windows.Kernel32;
1516
using Microsoft.Data.SqlClient;
1617

1718
namespace Microsoft.Data
@@ -121,7 +122,7 @@ static LocalDBCreateInstanceDelegate LocalDBCreateInstance
121122
Monitor.Enter(s_dllLock, ref lockTaken);
122123
if (s_localDBCreateInstance == null)
123124
{
124-
IntPtr functionAddr = SafeNativeMethods.GetProcAddress(UserInstanceDLLHandle, "LocalDBCreateInstance");
125+
IntPtr functionAddr = Kernel32Safe.GetProcAddress(UserInstanceDLLHandle, "LocalDBCreateInstance");
125126

126127
if (functionAddr == IntPtr.Zero)
127128
{
@@ -162,7 +163,7 @@ static LocalDBFormatMessageDelegate LocalDBFormatMessage
162163
Monitor.Enter(s_dllLock, ref lockTaken);
163164
if (s_localDBFormatMessage == null)
164165
{
165-
IntPtr functionAddr = SafeNativeMethods.GetProcAddress(UserInstanceDLLHandle, "LocalDBFormatMessage");
166+
IntPtr functionAddr = Kernel32Safe.GetProcAddress(UserInstanceDLLHandle, "LocalDBFormatMessage");
166167

167168
if (functionAddr == IntPtr.Zero)
168169
{

src/Microsoft.Data.SqlClient/netfx/src/Microsoft/Data/SqlClient/SqlConnection.cs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,10 @@
2424
using System.Text;
2525
using System.Threading;
2626
using System.Threading.Tasks;
27+
using Interop.Windows.AdvApi32;
2728
using Microsoft.Data.ProviderBase;
2829
using Microsoft.SqlServer.Server;
30+
using Interop.Windows.Kernel32;
2931

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

25372539
string mapFileName;
25382540

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

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

2551-
hFileMap = NativeMethods.OpenFileMappingA(0x4/*FILE_MAP_READ*/, false, mapFileName);
2553+
hFileMap = Kernel32.OpenFileMappingA(0x4/*FILE_MAP_READ*/, false, mapFileName);
25522554

25532555
if (ADP.s_ptrZero != hFileMap)
25542556
{
2555-
IntPtr pMemMap = NativeMethods.MapViewOfFile(hFileMap, 0x4/*FILE_MAP_READ*/, 0, 0, IntPtr.Zero);
2557+
IntPtr pMemMap = Kernel32.MapViewOfFile(hFileMap, 0x4/*FILE_MAP_READ*/, 0, 0, IntPtr.Zero);
25562558
if (ADP.s_ptrZero != pMemMap)
25572559
{
25582560
SqlDebugContext sdc = new SqlDebugContext();
@@ -3098,12 +3100,12 @@ private void Dispose(bool disposing)
30983100
}
30993101
if (pMemMap != IntPtr.Zero)
31003102
{
3101-
NativeMethods.UnmapViewOfFile(pMemMap);
3103+
Kernel32.UnmapViewOfFile(pMemMap);
31023104
pMemMap = IntPtr.Zero;
31033105
}
31043106
if (hMemMap != IntPtr.Zero)
31053107
{
3106-
NativeMethods.CloseHandle(hMemMap);
3108+
Kernel32.CloseHandle(hMemMap);
31073109
hMemMap = IntPtr.Zero;
31083110
}
31093111
active = false;
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
// See the LICENSE file in the project root for more information.
4+
5+
using System;
6+
using System.Runtime.InteropServices;
7+
using System.Runtime.Versioning;
8+
9+
namespace Interop.Windows.AdvApi32
10+
{
11+
internal class AdvApi32
12+
{
13+
private const string DllName = "advapi32.dll";
14+
15+
[DllImport(DllName, ExactSpelling = true, CharSet = CharSet.Auto, SetLastError = true)]
16+
[ResourceExposure(ResourceScope.None)]
17+
internal static extern bool AddAccessAllowedAce(IntPtr pAcl, int dwAceRevision, uint AccessMask, IntPtr pSid);
18+
19+
[DllImport(DllName, ExactSpelling = true, CharSet = CharSet.Auto, SetLastError = true)]
20+
[ResourceExposure(ResourceScope.None)]
21+
internal static extern bool AddAccessDeniedAce(IntPtr pAcl, int dwAceRevision, int AccessMask, IntPtr pSid);
22+
23+
[DllImport(DllName, ExactSpelling = true, CharSet = CharSet.Auto, SetLastError = true)]
24+
[ResourceExposure(ResourceScope.None)]
25+
internal static extern bool AllocateAndInitializeSid(
26+
IntPtr pIdentifierAuthority,
27+
byte nSubAuthorityCount,
28+
int dwSubAuthority0,
29+
int dwSubAuthority1,
30+
int dwSubAuthority2,
31+
int dwSubAuthority3,
32+
int dwSubAuthority4,
33+
int dwSubAuthority5,
34+
int dwSubAuthority6,
35+
int dwSubAuthority7,
36+
ref IntPtr pSid);
37+
38+
39+
[DllImport(DllName, ExactSpelling = true, CharSet = CharSet.Auto, SetLastError = true)]
40+
[ResourceExposure(ResourceScope.None)]
41+
internal static extern IntPtr FreeSid(IntPtr pSid);
42+
43+
[DllImport(DllName, ExactSpelling = true, CharSet = CharSet.Auto, SetLastError = true)]
44+
[ResourceExposure(ResourceScope.None)]
45+
internal static extern int GetLengthSid(IntPtr pSid);
46+
47+
[DllImport(DllName, ExactSpelling = true, CharSet = CharSet.Auto, SetLastError = true)]
48+
[ResourceExposure(ResourceScope.None)]
49+
internal static extern bool InitializeAcl(IntPtr pAcl, int nAclLength, int dwAclRevision);
50+
51+
[DllImport(DllName, ExactSpelling = true, CharSet = CharSet.Auto, SetLastError = true)]
52+
[ResourceExposure(ResourceScope.None)]
53+
internal static extern bool InitializeSecurityDescriptor(IntPtr pSecurityDescriptor, int dwRevision);
54+
55+
[DllImport(DllName, ExactSpelling = true, CharSet = CharSet.Auto, SetLastError = true)]
56+
[ResourceExposure(ResourceScope.None)]
57+
internal static extern bool SetSecurityDescriptorDacl(
58+
IntPtr pSecurityDescriptor,
59+
bool bDaclPresent,
60+
IntPtr pDacl,
61+
bool bDaclDefaulted);
62+
}
63+
}

0 commit comments

Comments
 (0)