C#,在 .Net Core 中使用来自 kernel32 的 HeapAlloc 时触发访问冲突

问题描述 投票:0回答:0

我创建了一个编译为

.NET Standard 2.1
的 x.dll,当我将此库引用到 .NET Framework 控制台时,桌面应用程序成功运行,但是当我将此库引用到 .NET Core 6 控制台应用程序时,出现错误:

System.AccessViolationException。尝试读取或写入受保护的内存。 这通常表明其他内存已损坏。

[DllImport("kernel32")]
public static extern int GetProcessHeap();

[DllImport("kernel32")]
public static extern void* HeapAlloc(int hHeap, int flags, int size);

private static int _heap = GetProcessHeap();

public static void* Alloc(int size)
{
    void* result = HeapAlloc(_heap, 0x00000008, size); // <--- Exception
    if (result == null) throw new OutOfMemoryException();
    return result;
}

我该如何解决?

谢谢。

c# .net-core dll access-violation
© www.soinside.com 2019 - 2024. All rights reserved.