作者:罗斌强 | 来源:互联网 | 2022-12-05 17:44
可能是一个愚蠢的问题......我是C#和.Net的新手.
在MSDN上的SafeHandle类(C#)的示例中,代码让我抓狂了一下.
[SecurityPermission(SecurityAction.InheritanceDemand, UnmanagedCode = true)]
[SecurityPermission(SecurityAction.Demand, UnmanagedCode = true)]
internal class MySafeFileHandle : SafeHandleZeroOrMinusOneIsInvalid
{
private MySafeFileHandle()
: base(true)
{}
// other code here
}
[SuppressUnmanagedCodeSecurity()]
internal static class NativeMethods
{
// other code...
// Allocate a file object in the kernel, then return a handle to it.
[DllImport("kernel32", SetLastError = true, CharSet = CharSet.Unicode)]
internal extern static MySafeFileHandle CreateFile(String fileName,
int dwDesiredAccess, System.IO.FileShare dwShareMode,
IntPtr securityAttrs_MustBeZero, System.IO.FileMode
dwCreationDisposition, int dwFlagsAndAttributes,
IntPtr hTemplateFile_MustBeZero);
// other code...
}
// Later in the code the handle is created like this:
MySafeFileHandle tmpHandle;
tmpHandle = NativeMethods.CreateFile(fileName, NativeMethods.GENERIC_READ,
FileShare.Read, IntPtr.Zero, FileMode.Open, 0, IntPtr.Zero);
我的问题是: C函数的Win32 HANDLE如何CreateFile
进入MySafeFileHandle
受保护的IntPtr
"handle"变量对象?构造函数MySafeFileHandle
是私有的,甚至不IntPtr
作为参数!
对CreateFile
声明的评论说了些什么
... CLR的平台编组层将以原子方式将句柄存储到SafeHandle对象中.
我不确定我到底知道这意味着什么,有人可以解释一下吗?