Пример #1
0
        public void ThrowIfFailedTest()
        {
            NTStatus nts = NTStatus.STATUS_ACCESS_DENIED;

            Assert.That(() => nts.ThrowIfFailed(), Throws.Exception);
            Assert.That(() => nts.ThrowIfFailed("Bad"), Throws.TypeOf <UnauthorizedAccessException>().With.Message.EqualTo("Bad"));
            Assert.That(() => NTStatus.ThrowIfFailed(0), Throws.Nothing);
        }
Пример #2
0
        private unsafe static string GetFileInformationString(SafeFileHandle fileHandle, FileInformationClass fileInformationClass)
        {
            return(BufferHelper.BufferInvoke((HeapBuffer buffer) =>
            {
                NTStatus status = NTStatus.STATUS_BUFFER_OVERFLOW;

                // Start with MAX_PATH
                uint byteLength = 260 * sizeof(char);

                FILE_NAME_INFORMATION *value = null;

                while (status == NTStatus.STATUS_BUFFER_OVERFLOW)
                {
                    // Add space for the FileNameLength
                    buffer.EnsureByteCapacity(byteLength + sizeof(uint));

                    status = Imports.NtQueryInformationFile(
                        FileHandle: fileHandle,
                        IoStatusBlock: out _,
                        FileInformation: buffer.VoidPointer,
                        Length: checked ((uint)buffer.ByteCapacity),
                        FileInformationClass: fileInformationClass);

                    if (status == NTStatus.STATUS_SUCCESS || status == NTStatus.STATUS_BUFFER_OVERFLOW)
                    {
                        value = (FILE_NAME_INFORMATION *)buffer.VoidPointer;
                        byteLength = value->FileNameLength;
                    }
                }

                status.ThrowIfFailed();

                return value->FileName.CreateString();
            }));
        }