dotnet / pinvoke

A library containing all P/Invoke code so you don't have to import it every time. Maintained and updated to support the latest Windows OS.
MIT License
2.12k stars 222 forks source link

Fix EntryPoints for MoveMemory, CopyMemory, FillMemory, ZeroMemory #431

Closed vatsan-madhavan closed 4 years ago

vatsan-madhavan commented 5 years ago

Please see https://github.com/dotnet/coreclr/issues/24008 for background.

.NET Framework has special case logic to ensure that MoveMemory, CopyMemory, FillMemory, and ZeroMemory are correctly mapped to RtlMoveMemory, RtlCopyMemory, RtlFillMemory, and RtlZeroMemory respectively.

.NET Core does not have this this special case, so the following P/Invoke definition will fail on .NET Core.

    [DllImport("kernel32.dll", EntryPoint = "CopyMemory", SetLastError = false)]
    public static extern void CopyMemory(IntPtr dest, IntPtr src, uint count);

At least two definitions in PInvoke.Kernel32 are likely to fail in .NET Core - MoveMemory, and CopyMemory. (I did not inspect to see whether FillMemory and ZeroMemory have this bug).

AArnott commented 4 years ago

FillMemory and ZeroMemory are macros that are defined in terms of memset rather than a native exported function from a Win32 DLL. I will fix MoveMemory and CopyMemory though.

Thanks for the report.

hugmyndakassi commented 3 years ago

While it is technically true that these are macros in the header, kernel32.dll has long exported RtlZeroMemory as function forwarder into a function of the same name in ntdll.dll and also provides an exported RtlFillMemory function.

As a side-note, Vanara also relies on these four functions existing in kernel32.dll.

AArnott commented 3 years ago

@hugmyndakassi Are RtlZeroMemory or RtlFillMemory as exported functions documented? I can't find any documentation for them outside of macros. We don't expose undocumented Windows features in this library.

mpolicki commented 2 years ago

I'll just leave a quick note here that I got a System.EntryPointNotFoundException: 'Unable to find an entry point named 'RtlCopyMemory' in DLL 'Kernel32'. in a unit test with a CopyMemory call. The exception went away after I set Processor Architecture for AnyCPU Projects to x64 in Visual Studio Test Explorer settings.

.NET Framework 4.7.2, PInvoke 0.7.104

AArnott commented 2 years ago

Thanks, @mpolicki. To be clear, it sounds like you're saying that x86 processes don't get a kernel32 export of RtlCopyMemory, but x64 processes do. Does that sound about right? If so, can you open a separate issue to track that?

mpolicki commented 2 years ago

@AArnott Yes, that's exactly what seems to be the case. The issue is #587.

AArnott commented 2 years ago

Thank you.