We have used the CopyMemory method in our project to copies memory from source to destination. While copying, exception thrown like "'Unable to find an entry point named 'CopyMemory' in DLL 'kernel32.dll'.'". This code works fine in WPF with .NET Framework. But, we are facing an issue in WPF with .NET Core. I have added the simple code snippet to reproduce the issue in WPF with .NETCore. The simple sample can be download from below link,
.NET Core3.0
http://www.syncfusion.com/downloads/support/directtrac/general/ze/NETCOR~1263359629.zip
.NET Framework
Code snippet :
[DllImport("kernel32.dll", EntryPoint = "CopyMemory", SetLastError = false)]
public static extern void CopyMemory(IntPtr dest, IntPtr src, uint count);
public MainWindow()
{
const int size = 200;
IntPtr memorySource = Marshal.AllocHGlobal(size);
Marshal.WriteInt32(memorySource, 35);
IntPtr memoryTarget = Marshal.AllocHGlobal(size);
CopyMemory(memoryTarget, memorySource, size);
bool val = Marshal.ReadInt32(memoryTarget) == 35;
}
Please find the required configuration below.
.NET Framework : 4.6
.NET Core : 3.0
Operating System : Windows 10
Preferred Programming Language : C#
Visual Studio : Visual Studio 2017
This doesn't seem specific to WPF. Happens in a general .NET Core application as well.
```C#
using System;
using System.Runtime.InteropServices;
namespace test1
{
class Program
{
static void Main(string[] args)
{
bool val = false;
try
{
const int size = 200;
IntPtr memorySource = Marshal.AllocHGlobal(size);
Marshal.WriteInt32(memorySource, 35);
IntPtr memoryTarget = Marshal.AllocHGlobal(size);
CopyMemory(memoryTarget, memorySource, size);
val = Marshal.ReadInt32(memoryTarget) == 35;
}
catch(EntryPointNotFoundException e)
{
Console.WriteLine(e.ToString());
}
Console.WriteLine($"Successfully read from CopyMemory ? : {val}");
}
[DllImport("kernel32.dll", EntryPoint = "CopyMemory", SetLastError = false)]
public static extern void CopyMemory(IntPtr dest, IntPtr src, uint count);
}
}
test1>binDebugnetcoreapp3.0test1.exe
System.EntryPointNotFoundException: Unable to find an entry point named 'CopyMemory' in DLL 'kernel32.dll'.
at test1.Program.CopyMemory(IntPtr dest, IntPtr src, UInt32 count)
at test1.Program.Main(String[] args) in D:Tempnetcoretest1Program.cs:line 18
Successfully read from CopyMemory ? : False
test1>binDebugnet472test1.exe
Successfully read from CopyMemory ? : True
```
@AaronRobinsonMSFT, should be this be moved to coreclr?
@vatsan-madhavan This is failing for an absolutely insane reason. There are no words for the nonsense.. Feel free to push this issue over to CoreCLR and mark it as 3.0.
cc @jeffschwMSFT
I didn't realize this was just reported via WPF.
@Ebenezer94 In .NET Framework there was a special case for a few function names and CopyMemory happened to be one of them. The special case was removed in .NET Core. In order to address this in your code, please update the attribute to include the following: [DllImport EntryPoint="RtlMoveMemory"]. If the EntryPoint property is set as above, the P/Invoke will work in both .NET Framework and .NET Core.
This issue was moved to dotnet/coreclr#24008