- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我刚刚在 C# 中遇到了 DllImport 的奇怪行为,我无法解释。我想知道它是如何可能的,以及我可以在哪里阅读它。案例是通过 DllImport 可以调用不真正导出表单 dll 的函数。就我而言,它是 kernel32.dll 和函数 ZeroMemory(但具有复制/移动/填充内存这样的行为)。所以,我的代码:
[DllImport("kernel32", EntryPoint = "LoadLibraryW", CharSet = CharSet.Unicode, SetLastError = true)]
public static extern IntPtr LoadLibrary(string libName);
[DllImport("kernel32.dll", CharSet = CharSet.Ansi, ExactSpelling = true, SetLastError = true)]
public static extern IntPtr GetProcAddress(IntPtr module, string procName);
//WTF???
[DllImport("kernel32.dll", SetLastError = true)]
public static extern bool ZeroMemory(IntPtr address, int size);
static void TestMemory()
{
IntPtr mem = Marshal.AllocHGlobal(100); //Allocate memory block of 100 bytes size
Marshal.WriteByte(mem, 55); //Write some value in the first byte
ZeroMemory(mem, 100); //Clearing block of memory
byte firstByte = Marshal.ReadByte(mem); //Read the first byte of memory block
Console.WriteLine(firstByte); //Output 0 (not 55) - ZeroMemory is working
//Getting address of ZeroMemory from kernel32.dll
IntPtr kernelHandle = LoadLibrary("kernel32.dll");
IntPtr address = GetProcAddress(kernelHandle, "ZeroMemory");
Console.WriteLine(address.ToString("X")); //Output 0 - Library kernel32.dll DOESN'T export function ZeroMemory!!!
//Testing GetProcAddress via getting address of some exported function
Console.WriteLine(GetProcAddress(kernelHandle, "AllocConsole").ToString("X")); //Output some address value - all is OK.
}
没有抛出 EntryPointNotFoundException - 代码工作正常。如果将 ZeroMemory 的名称更改为 ZeroMemory1 或类似的名称 - 将引发异常。但是在kernel32.dll的导出表中我们看到:
#define RtlMoveMemory memmove
#define RtlCopyMemory memcpy
#define RtlFillMemory(d,l,f) memset((d), (f), (l))
#define RtlZeroMemory(d,l) RtlFillMemory((d),(l),0)
#define MoveMemory RtlMoveMemory
#define CopyMemory RtlCopyMemory
#define FillMemory RtlFillMemory
#define ZeroMemory RtlZeroMemory
显然,在 C++ ZeroMemory 中,它实际上是通过 ntdll.dll 中的 RtlFillMemory 工作的。但它是在 C++ 中的!!!为什么它在 C# 中工作?关于 DllImport 属性的官方文档
here我们可以阅读下一个:
As a minimum requirement, you must supply the name of the DLLcontaining the entry point.
最佳答案
OP 是正确的 kernel32.dll
没有导出ZeroMemory
导出,但 C# DllImport
不知何故成功地神奇地解决了 ZeroMemory
引用正确的 RtlZeroMemory
.NET 应用程序中针对框架(但不是核心)的导出。
事实证明,框架代码专门检查了少数记录为内联/宏( MoveMemory
、 CopyMemory
FillMemory
、 ZeroMemory
)的 Win32 API,并在内部重新路由到正确的导出。虽然没有正式记录,但这在 MS 批准的 comment 中得到了承认。下.NET Runtime问题。
As an FYI, there were a few special cased P/Invoke names in .NET Framework:
MoveMemory
,CopyMemory
,FillMemory
, andZeroMemory
. All of these will work when pointing at kernel32 on .NET Framework, but will fail on .NET Core. Please use theEntryPoint
property in theDllImport
attribute to get the desired behavior. The proper export names can be found usingdumpbin /exports kernel32.dll
from a Visual Studio command prompt.
EntryPoint
使声明在 Framework 和 Core 中都有效,例如:
[DllImport("kernel32.dll", EntryPoint = "RtlMoveMemory", SetLastError = false)]
public static extern void ZeroMemory(IntPtr address, IntPtr count);
神奇的名称重映射发生在开源 .NET 代码之外,但可以在
Simon Mourier 提供的反汇编中清楚地看到在评论中。
关于c# - 带有非导出函数的 DllImport。为什么有效?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66596448/
我有一个类,里面有一些静态成员函数。假设类 B,这个类有一个基类,它来自第三方库,比如 A。现在类 A 已经用 dllimport 和用mingw 我可以毫不费力地构建 sharedlibs 或那个类
在 C# 中,我有这个: [DllImport("user32.dll", EntryPoint = "GetDesktopWindow")] public static extern Int
我正在尝试编写一个使用 C++ 中定义的函数的 WPF 应用程序。 C++ 应用程序被编译为 DLL。 我正在做的是使用 DllImport 属性导入函数并使用它。这在我的机器上运行良好,但是当我尝试
有没有办法为一组外部函数指定相同的 DllImport 属性,类似于 extern "C"{ … } ?我不想为每个函数声明重复它:-) 最佳答案 没有。避免它的唯一方法是在 C++/CLI 中编写一
我正在尝试将短类型的参数传递给从 DLL 导入的 C++ 非托管函数。在我的 C++ DLL 代码中,我有以下函数: __declspec(dllexport)void ChangeIcon(char
我有一个第三方 c dll,我想在我的 c# 项目中使用它。我设法导入了一种读取文件头的方法。现在我想访问读取数据的方法。我认为问题在于包含字符串数组的结构,因此我尝试了各种方法,例如 StringB
如何使用反射枚举程序集中的所有 DLLImports? 最佳答案 遍历每个类中的每个方法,并检查 GetCustomAttributes(typeof(DllImportAttribute)) 是否返
在 asp.net 中,如果我使用 DLLImport 调用一个 dll,有人知道该实例的范围吗?它是在应用程序级别,所以任何后续调用都会转到 dll 的同一实例,直到应用程序结束? 我有一个 dll
我想了解 DllImport 的真正工作原理。我需要一个通俗易懂的英语解释——意思是简单的解释。 它是否与 DLL 中导出的方法静态链接,如“包含文件”指令/静态库? 还是在C#程序中到达执行点时动态
我正在尝试编写一个 C# 托管类来包装 SHGetKnownFolderPath,到目前为止它可以在 Vista 上运行,但由于在 shell32.dll 中找不到正确的函数而在 XP 上崩溃,正如预
我有一个带有 C++ 组件的 C# 应用程序。我在模块之间使用 DllImport 进行通信。该应用程序可以正常工作很多天,有时会意外崩溃。 [DllImport("recorder", Callin
这个问题在这里已经有了答案: Loading a 32-bit dll in a 64-bit process [duplicate] (1 个回答) 关闭 8 年前。 我希望我的 C# 应用程序有
我正在通过 C# 与 native 第 3 方 C++ DLL 进行交互,所提供的互操作层如下所示: C#: [DllImport("csvcomm.dll")] public static exte
我正在使用 [DLLImport] 属性访问我的 .NET 代码中的一堆 C++ 函数。现在,我通过以下方式拥有所有功能: const string DLL_Path = "path\\to\\my\
假设在 Native.dll 中有一个 c++ 方法 int NativeMethod(double, double *)。我第一次尝试从托管代码调用此方法(假设我不需要指定入口点) [DllImpo
我正在做一个P/Invoke,我正在使用下面的方法 [DllImport("Authz.dll", SetLastError = true)] public static extern BOO
static class Class { public static void methodRequiringStuffFromKernel32() { // c
我正在使用 [DllImport]属性将 native DLL 导入我的应用程序,但它加载的 DLL 不在本地 bin 文件夹中。它是从系统的其他地方加载的,但我不知道在哪里。 它适用于我的开发机器,
我刚刚在 C# 中遇到了 DllImport 的奇怪行为,我无法解释。我想知道它是如何可能的,以及我可以在哪里阅读它。案例是通过 DllImport 可以调用不真正导出表单 dll 的函数。就我而言,
我尝试使用 P/Invoke 将示例 .net 应用程序转换为 javascript JSIL . C# 代码: [DllImport("JSTestLib", EntryPoint = "Get42
我是一名优秀的程序员,十分优秀!