Skip to content
This repository has been archived by the owner on Jan 23, 2023. It is now read-only.
/ corefx Public archive

Commit

Permalink
Refactor DllImports in System.Reflection.Metadata (#40579)
Browse files Browse the repository at this point in the history
  • Loading branch information
madmir authored and safern committed Oct 21, 2019
1 parent 776aae5 commit 90706ad
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@
<Configurations>netcoreapp-Debug;netcoreapp-Release;netstandard-Debug;netstandard-Release;netstandard1.1-Debug;netstandard1.1-Release</Configurations>
</PropertyGroup>
<ItemGroup>
<Compile Include="$(CommonPath)\Interop\Windows\Interop.Libraries.cs">
<Link>Common\Interop\Windows\Interop.Libraries.cs</Link>
</Compile>
<Compile Include="$(CommonPath)\CoreLib\Interop\Windows\Kernel32\Interop.ReadFile_SafeHandle_IntPtr.cs">
<Link>Common\CoreLib\Interop\Windows\kernel32\Interop.ReadFile_SafeHandle_IntPtr.cs</Link>
</Compile>
<Compile Include="System\Reflection\Internal\Utilities\PinnedObject.cs" />
<Compile Include="System\Reflection\Internal\Utilities\CriticalDisposableObject.cs" Condition="'$(TargetFramework)' != 'netstandard1.1'" />
<Compile Include="System\Reflection\Internal\Utilities\CriticalDisposableObject.netstandard1.1.cs" Condition="'$(TargetFramework)' == 'netstandard1.1'" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,20 +53,20 @@ internal static unsafe bool TryReadFile(Stream stream, byte* buffer, long start,
return false;
}

bool result = false;
int bytesRead = 0;
int result;
int bytesRead;

try
{
result = ReadFile(handle, buffer, size, out bytesRead, IntPtr.Zero);
result = Interop.Kernel32.ReadFile(handle, buffer, size, out bytesRead, IntPtr.Zero);
}
catch
{
readFileNotAvailable = true;
return false;
}

if (!result || bytesRead != size)
if (result == 0 || bytesRead != size)
{
// We used to throw here, but this is where we land if the FileStream was
// opened with useAsync: true, which is currently the default on .NET Core.
Expand All @@ -78,15 +78,5 @@ internal static unsafe bool TryReadFile(Stream stream, byte* buffer, long start,

return true;
}

[DllImport(@"kernel32.dll", EntryPoint = "ReadFile", ExactSpelling = true, SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
internal static extern unsafe bool ReadFile(
SafeHandle fileHandle,
byte* buffer,
int byteCount,
out int bytesRead,
IntPtr overlapped
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -100,20 +100,20 @@ internal static unsafe bool TryReadFile(Stream stream, byte* buffer, long start,
return false;
}

bool result = false;
int bytesRead = 0;
int result;
int bytesRead;

try
{
result = ReadFile(handle, buffer, size, out bytesRead, IntPtr.Zero);
result = Interop.Kernel32.ReadFile(handle, buffer, size, out bytesRead, IntPtr.Zero);
}
catch
{
readFileNotAvailable = true;
return false;
}

if (!result || bytesRead != size)
if (result == 0 || bytesRead != size)
{
// We used to throw here, but this is where we land if the FileStream was
// opened with useAsync: true, which is currently the default on .NET Core.
Expand All @@ -125,15 +125,5 @@ internal static unsafe bool TryReadFile(Stream stream, byte* buffer, long start,

return true;
}

[DllImport(@"kernel32.dll", EntryPoint = "ReadFile", ExactSpelling = true, SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
internal static extern unsafe bool ReadFile(
SafeHandle fileHandle,
byte* buffer,
int byteCount,
out int bytesRead,
IntPtr overlapped
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
<PropertyGroup>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<ExternallyShipping>false</ExternallyShipping>
<NoWarn>436</NoWarn> <!-- Type conflicts on "Interop" due to InternalsVisibleTo access -->
<Configurations>netcoreapp-Debug;netcoreapp-Release;netfx-Debug;netfx-Release</Configurations>
</PropertyGroup>
<ItemGroup>
Expand Down

0 comments on commit 90706ad

Please sign in to comment.