【发布时间】:2019-04-13 10:05:33
【问题描述】:
使用下面的代码尝试使用 kernel32 函数 SecureZeroMemory 失败,并出现 System.EntryPointNotFoundException - 即使它有很好的文档记录 here, on PInvoke 和 here, on SO。在目标 .NET Framework 4.7.2 上运行完全正常的 Windows 10 Pro。
/// <summary>
/// A kernel32 function that destroys all values in a block of memory
/// </summary>
/// <param name="destination">The pointer to the start of the block to be zeroed</param>
/// <param name="length">The number of bytes to zero</param>
/// <returns></returns>
[DllImport("kernel32.dll", CharSet = CharSet.Auto, EntryPoint = "RtlSecureZeroMemory")]
public static extern void SecureZeroMemory(IntPtr destination, IntPtr length);
【问题讨论】:
-
你在写什么样的 .net 应用?
-
WPF 应用程序,因此仅限 Windows
-
不可能,因为 RtlSecureZeroMemory 不是系统 DLL 导出的函数。它是一个函数inlined in winnt.h 本质上,当在 C/C++ 项目中包含 winnt.h 时,该函数的代码正在成为被编译代码的一部分。现在,显然,您不能在 C# 中将 C 头文件与 C 代码内联... :-(
-
所以这些记录它的人一定只是猜到了。将
__declspec(dllexport)包装到 C++ DLL 中的最佳方法是什么? -
是的,你可以这样做。
标签: c# windows dll pinvoke kernel32