Ejemplo n.º 1
0
 /// <summary>
 /// Equivalent of InitializeObjectAttributes macro with the exception that you can directly set SQOS.
 /// </summary>
 public unsafe OBJECT_ATTRIBUTES(UNICODE_STRING *objectName, ObjectAttributes attributes, IntPtr rootDirectory, SECURITY_QUALITY_OF_SERVICE *securityQualityOfService = null)
 {
     Length                   = (uint)sizeof(OBJECT_ATTRIBUTES);
     RootDirectory            = rootDirectory;
     ObjectName               = objectName;
     Attributes               = attributes;
     SecurityDescriptor       = null;
     SecurityQualityOfService = securityQualityOfService;
 }
        internal static unsafe (uint status, IntPtr handle) CreateFile(
            ReadOnlySpan <char> path,
            IntPtr rootDirectory,
            CreateDisposition createDisposition,
            DesiredAccess desiredAccess       = DesiredAccess.FILE_GENERIC_READ | DesiredAccess.SYNCHRONIZE,
            FileShare shareAccess             = FileShare.ReadWrite | FileShare.Delete,
            FileAttributes fileAttributes     = 0,
            CreateOptions createOptions       = CreateOptions.FILE_SYNCHRONOUS_IO_NONALERT,
            ObjectAttributes objectAttributes = ObjectAttributes.OBJ_CASE_INSENSITIVE,
            void *eaBuffer          = null,
            uint eaLength           = 0,
            long *preallocationSize = null,
            SECURITY_QUALITY_OF_SERVICE *securityQualityOfService = null)
        {
            fixed(char *c = &MemoryMarshal.GetReference(path))
            {
                UNICODE_STRING name = new UNICODE_STRING
                {
                    Length        = checked ((ushort)(path.Length * sizeof(char))),
                    MaximumLength = checked ((ushort)(path.Length * sizeof(char))),
                    Buffer        = (IntPtr)c
                };

                OBJECT_ATTRIBUTES attributes = new OBJECT_ATTRIBUTES(
                    &name,
                    objectAttributes,
                    rootDirectory,
                    securityQualityOfService);

                IntPtr          handle;
                IO_STATUS_BLOCK statusBlock;
                uint            status = NtCreateFile(
                    &handle,
                    desiredAccess,
                    &attributes,
                    &statusBlock,
                    AllocationSize: preallocationSize,
                    fileAttributes,
                    shareAccess,
                    createDisposition,
                    createOptions,
                    eaBuffer,
                    eaLength);

                return(status, handle);
            }
        }