static CheckResult CheckDevice(string name, bool writable, EaBuffer ea_buffer)
        {
            CheckResult result = new CheckResult(name, NtStatus.STATUS_INVALID_PARAMETER, FileDeviceType.UNKNOWN);

            try
            {
                using (var imp = NtToken.Impersonate(_pid,
                                                     _identify_only ? SecurityImpersonationLevel.Identification : SecurityImpersonationLevel.Impersonation))
                {
                    FileAccessRights access_mask = FileAccessRights.GenericRead;
                    if (writable)
                    {
                        access_mask |= FileAccessRights.GenericWrite;
                    }

                    FileOpenOptions opts = _open_as_dir ? FileOpenOptions.DirectoryFile : FileOpenOptions.NonDirectoryFile;
                    using (NtFile file = NtFile.Create(name, null, access_mask, NtApiDotNet.FileAttributes.Normal,
                                                       FileShareMode.All, opts, FileDisposition.Open, ea_buffer))
                    {
                        result = new CheckResult(name, NtStatus.STATUS_SUCCESS, file.DeviceType);
                    }
                }
            }
            catch (NtException ex)
            {
                result = new CheckResult(name, ex.Status, FileDeviceType.UNKNOWN);
            }

            return(result);
        }
 /// <summary>
 /// Method to create an object from a set of object attributes.
 /// </summary>
 /// <param name="obj_attributes">The object attributes to create/open from.</param>
 /// <returns>The newly created object.</returns>
 protected override object CreateObject(ObjectAttributes obj_attributes)
 {
     using (Transaction?.Enable())
     {
         return(NtFile.Create(obj_attributes, Access, Attributes,
                              ShareMode, Options | (Directory ? FileOpenOptions.DirectoryFile : FileOpenOptions.None), Disposition, EaBuffer));
     }
 }
 private static NtResult <NtFile> OpenUnderImpersonation(TokenEntry token, string path, FileOpenOptions open_options, EaBuffer ea_buffer)
 {
     using (var obja = new ObjectAttributes(path, AttributeFlags.CaseInsensitive))
     {
         return(token.Token.RunUnderImpersonate(() => NtFile.Create(obja, FileAccessRights.MaximumAllowed,
                                                                    FileAttributes.None, FileShareMode.None, open_options, FileDisposition.Open, ea_buffer, false)));
     }
 }
 /// <summary>
 /// Method to create an object from a set of object attributes.
 /// </summary>
 /// <param name="obj_attributes">The object attributes to create/open from.</param>
 /// <returns>The newly created object.</returns>
 protected override object CreateObject(ObjectAttributes obj_attributes)
 {
     using (Transaction?.Enable())
     {
         FileOpenOptions opts = Options;
         if (OpenById)
         {
             opts |= FileOpenOptions.OpenByFileId;
         }
         if (Directory)
         {
             opts |= FileOpenOptions.DirectoryFile;
         }
         return(NtFile.Create(obj_attributes, Access, FileAttribute,
                              ShareMode, opts, Disposition, EaBuffer, AllocationSize));
     }
 }
 /// <summary>
 /// Method to create an object from a set of object attributes.
 /// </summary>
 /// <param name="obj_attributes">The object attributes to create/open from.</param>
 /// <returns>The newly created object.</returns>
 protected override object CreateObject(ObjectAttributes obj_attributes)
 {
     return(NtFile.Create(obj_attributes, Access, Attributes, ShareMode, Options, Disposition, EaBuffer));
 }