Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Exception thrown 'Unable to find an entry point named 'CopyMemory' in DLL 'kernel32.dll'.' on WPF project with .NET Core 3.0 #12496

Closed
vatsan-madhavan opened this issue Apr 15, 2019 · 5 comments

Comments

@vatsan-madhavan
Copy link
Member

From @Ebenezer94 on Monday, 15 April 2019 17:50:37

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

http://www.syncfusion.com/downloads/support/directtrac/general/ze/CopyMemory_NetFramework542091517.zip

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

Copied from original issue: dotnet/wpf#586

@vatsan-madhavan
Copy link
Member Author

From @vatsan-madhavan on Monday, 15 April 2019 19:44:57

This doesn't seem specific to WPF. Happens in a general .NET Core application as well.

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>bin\Debug\netcoreapp3.0\test1.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:\Temp\netcore\test1\Program.cs:line 18
Successfully read from CopyMemory ? : False

\test1>bin\Debug\net472\test1.exe
Successfully read from CopyMemory ? : True

@AaronRobinsonMSFT, should be this be moved to coreclr?

@vatsan-madhavan
Copy link
Member Author

From @AaronRobinsonMSFT on Monday, 15 April 2019 20:51:42

@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

@vatsan-madhavan
Copy link
Member Author

From @AaronRobinsonMSFT on Monday, 15 April 2019 22:00:59

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.

@AaronRobinsonMSFT AaronRobinsonMSFT self-assigned this Apr 15, 2019
@AaronRobinsonMSFT
Copy link
Member

@Ebenezer94 As an FYI, there were a few special cased P/Invoke names in .NET Framework: MoveMemory, CopyMemory, FillMemory, and ZeroMemory. All of these will work when pointing at kernel32 on .NET Framework, but will fail on .NET Core. Please use the EntryPoint property in the DllImport attribute to get the desired behavior. The proper export names can be found using dumpbin /exports kernel32.dll from a Visual Studio command prompt.

@vatsan-madhavan
Copy link
Member Author

BTW, @AaronRobinsonMSFT asked me offline whether this affects the WPF codebase itself. The answer is no. We looked through the P/Invokes in the codebase, and found that the P/Invoke for MoveMemory in the WPF codebase explicitly specifies an entrypoint (RtlMoveMemory).

amaitland referenced this issue in cefsharp/CefSharp Sep 5, 2019
.Net could fails with exception "Unable to find an entry point named 'CopyMemory' in DLL 'kernel32.dll`" as
the export doesn't exist see reference https://github.com/dotnet/coreclr/issues/24008

Issue #2053
@msftgits msftgits transferred this issue from dotnet/coreclr Jan 31, 2020
@msftgits msftgits added this to the 3.0 milestone Jan 31, 2020
@ghost ghost locked as resolved and limited conversation to collaborators Dec 13, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants