Возможно, глупый вопрос ... Я новичок в C # и .Net.
В примере для класса SafeHandle (C #) на MSDN код заставил меня немного почесать голову.
[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);
Мой вопрос: Как Win32 HANDLE из функции C CreateFile
попадает в MySafeFileHandle
объекты, защищенные IntPtr
переменную «handle»? Конструктор MySafeFileHandle
является приватным и даже не принимает IntPtr
в качестве аргумента!
Комментарий к выражению CreateFile
что-то говорит о
… слой маршаллинга платформы CLR будет хранить дескриптор в объекте SafeHandle атомарным способом.
Я не уверен, что точно знаю, что это значит, кто-нибудь может объяснить, пожалуйста?