mirror of
https://github.com/claunia/Claunia.IO.git
synced 2025-12-16 19:24:44 +00:00
Implemented NtCreateFile() calling to get file handles and
NtClose() to dispose of them.
This commit is contained in:
@@ -1,3 +1,11 @@
|
||||
2015-05-04 Natalia Portillo <claunia@claunia.com>
|
||||
|
||||
* Claunia.IO.csproj:
|
||||
* Interop/Windows/Interop.Windows.Files.cs:
|
||||
* Interop/Windows/Interop.Windows.types.cs:
|
||||
Implemented NtCreateFile() calling to get file handles and
|
||||
NtClose() to dispose of them.
|
||||
|
||||
2015-05-04 Natalia Portillo <claunia@claunia.com>
|
||||
|
||||
* Claunia.IO.csproj:
|
||||
|
||||
@@ -70,6 +70,8 @@
|
||||
<Compile Include="Interop\Windows\Interop.Windows.Libraries.cs" />
|
||||
<Compile Include="Interop\Windows\Interop.Windows.Errors.cs" />
|
||||
<Compile Include="Interop\Windows\Interop.Windows.EAs.cs" />
|
||||
<Compile Include="Interop\Windows\Interop.Windows.types.cs" />
|
||||
<Compile Include="Interop\Windows\Interop.Windows.Files.cs" />
|
||||
</ItemGroup>
|
||||
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
|
||||
<ItemGroup>
|
||||
|
||||
198
Claunia.IO/Interop/Windows/Interop.Windows.Files.cs
Normal file
198
Claunia.IO/Interop/Windows/Interop.Windows.Files.cs
Normal file
@@ -0,0 +1,198 @@
|
||||
//
|
||||
// Interop.Windows.Files.cs
|
||||
//
|
||||
// Author:
|
||||
// Natalia Portillo <claunia@claunia.com>
|
||||
//
|
||||
// Copyright (c) 2015 © Claunia.com
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
// of this software and associated documentation files (the "Software"), to deal
|
||||
// in the Software without restriction, including without limitation the rights
|
||||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
// copies of the Software, and to permit persons to whom the Software is
|
||||
// furnished to do so, subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included in
|
||||
// all copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
// THE SOFTWARE.
|
||||
using System.Runtime.InteropServices;
|
||||
using System;
|
||||
using Microsoft.Win32.SafeHandles;
|
||||
|
||||
internal static partial class Interop
|
||||
{
|
||||
internal static partial class Windows
|
||||
{
|
||||
|
||||
public enum CreateDispositionFlags : uint
|
||||
{
|
||||
/// <summary>
|
||||
/// Replaces the file if it exists.
|
||||
/// Creates a new file if it does not.
|
||||
/// </summary>
|
||||
FILE_SUPERSEDE = 0x00000000,
|
||||
/// <summary>
|
||||
/// Opens the file if it exists, error otherwise.
|
||||
/// </summary>
|
||||
FILE_OPEN = 0x00000001,
|
||||
/// <summary>
|
||||
/// Create the file if it does not exists, error otherwise.
|
||||
/// </summary>
|
||||
FILE_CREATE = 0x00000002,
|
||||
/// <summary>
|
||||
/// Opens the file if it exists, creates it if it does not.
|
||||
/// </summary>
|
||||
FILE_OPEN_IF = 0x00000003,
|
||||
/// <summary>
|
||||
/// Overwrites the file if it exists, error otherwise.
|
||||
/// </summary>
|
||||
FILE_OVERWRITE = 0x00000004,
|
||||
/// <summary>
|
||||
/// Overwrites the file if it exists, creates it if it does not.
|
||||
/// </summary>
|
||||
FILE_OVERWRITE_IF = 0x00000005,
|
||||
FILE_MAXIMUM_DISPOSITION = FILE_OVERWRITE_IF
|
||||
}
|
||||
|
||||
public enum CreateOptionsFlags : uint
|
||||
{
|
||||
/// <summary>
|
||||
/// The file is a directory.
|
||||
/// </summary>
|
||||
FILE_DIRECTORY_FILE = 0x00000001,
|
||||
/// <summary>
|
||||
/// System services, file-system drivers, and drivers that write data to the file must actually transfer the data to the file before any requested write operation is considered complete.
|
||||
/// </summary>
|
||||
FILE_WRITE_THROUGH = 0x00000002,
|
||||
/// <summary>
|
||||
/// All access to the file will be sequential.
|
||||
/// </summary>
|
||||
FILE_SEQUENTIAL_ONLY = 0x00000004,
|
||||
/// <summary>
|
||||
/// The file cannot be cached or buffered in a driver's internal buffers. This flag is incompatible with the <see cref="ACCESS_MASK.FILE_APPEND_DATA"/> flag.
|
||||
/// </summary>
|
||||
FILE_NO_INTERMEDIATE_BUFFERING = 0x00000008,
|
||||
|
||||
/// <summary>
|
||||
/// All operations on the file are performed synchronously. Any wait on behalf of the caller is subject to premature termination from alerts. This flag also causes the I/O system to maintain the file-position pointer. If this flag is set, <see cref="ACCESS_MASK.SYNCHRONIZE"/> must be set.
|
||||
/// </summary>
|
||||
FILE_SYNCHRONOUS_IO_ALERT = 0x00000010,
|
||||
/// <summary>
|
||||
/// All operations on the file are performed synchronously. Waits in the system that synchronize I/O queuing and completion are not subject to alerts. This flag also causes the I/O system to maintain the file-position context. If this flag is set, <see cref="ACCESS_MASK.SYNCHRONIZE"/> must be set.
|
||||
/// </summary>
|
||||
FILE_SYNCHRONOUS_IO_NONALERT = 0x00000020,
|
||||
/// <summary>
|
||||
/// The file is not a directory. The file object to open can represent a data file; a logical, virtual, or physical device; or a volume.
|
||||
/// </summary>
|
||||
FILE_NON_DIRECTORY_FILE = 0x00000040,
|
||||
/// <summary>
|
||||
/// Create a tree connection for this file in order to open it over the network.
|
||||
/// </summary>
|
||||
FILE_CREATE_TREE_CONNECTION = 0x00000080,
|
||||
|
||||
/// <summary>
|
||||
/// Complete this operation immediately with an alternate success code of <see cref="NTSTATUS.STATUS_OPLOCK_BREAK_IN_PROGRESS"/> if the target file is oplocked, rather than blocking the caller's thread. If the file is oplocked, another caller already has access to the file. This flag is not used by device and intermediate drivers.
|
||||
/// </summary>
|
||||
FILE_COMPLETE_IF_OPLOCKED = 0x00000100,
|
||||
/// <summary>
|
||||
/// If the extended attributes (EAs) for an existing file being opened indicate that the caller must understand EAs to properly interpret the file, NtCreateFile should return an error.
|
||||
/// </summary>
|
||||
FILE_NO_EA_KNOWLEDGE = 0x00000200,
|
||||
/// <summary>
|
||||
/// Undocumented
|
||||
/// </summary>
|
||||
FILE_OPEN_REMOTE_INSTANCE = 0x00000400,
|
||||
/// <summary>
|
||||
/// Access to the file can be random, so no sequential read-ahead operations should be performed by file-system drivers or by the system.
|
||||
/// </summary>
|
||||
FILE_RANDOM_ACCESS = 0x00000800,
|
||||
|
||||
/// <summary>
|
||||
/// The system deletes the file when the last handle to the file is passed to NtClose. If this flag is set, the <see cref="ACCESS_MASK.DELETE"/> flag must be set.
|
||||
/// </summary>
|
||||
FILE_DELETE_ON_CLOSE = 0x00001000,
|
||||
/// <summary>
|
||||
/// The file name that is specified by the ObjectAttributes parameter includes a binary 8-byte or 16-byte file reference number or object ID for the file, depending on the file system.
|
||||
/// </summary>
|
||||
FILE_OPEN_BY_FILE_ID = 0x00002000,
|
||||
/// <summary>
|
||||
/// The file is being opened for backup intent. Therefore, the system should check for certain access rights and grant the caller the appropriate access to the file—before checking the DesiredAccess parameter against the file's security descriptor.
|
||||
/// </summary>
|
||||
FILE_OPEN_FOR_BACKUP_INTENT = 0x00004000,
|
||||
/// <summary>
|
||||
/// Undocumented
|
||||
/// </summary>
|
||||
FILE_NO_COMPRESSION = 0x00008000,
|
||||
|
||||
/// <summary>
|
||||
/// The file is being opened and an opportunistic lock (oplock) on the file is being requested as a single atomic operation. The file system checks for oplocks before it performs the create operation, and will fail the create with a return code of <see cref="NTSTATUS.STATUS_CANNOT_BREAK_OPLOCK"/> if the result would be to break an existing oplock.
|
||||
/// </summary>
|
||||
FILE_OPEN_REQUIRING_OPLOCK = 0x00010000,
|
||||
/// <summary>
|
||||
/// Undocumented
|
||||
/// </summary>
|
||||
FILE_DISALLOW_EXCLUSIVE = 0x00020000,
|
||||
|
||||
/// <summary>
|
||||
/// This flag allows an application to request a Filter opportunistic lock (oplock) to prevent other applications from getting share violations. If there are already open handles, the create request will fail with <see cref="NTSTATUS.STATUS_OPLOCK_NOT_GRANTED"/>.
|
||||
/// </summary>
|
||||
FILE_RESERVE_OPFILTER = 0x00100000,
|
||||
/// <summary>
|
||||
/// Open a file with a reparse point and bypass normal reparse point processing for the file.
|
||||
/// </summary>
|
||||
FILE_OPEN_REPARSE_POINT = 0x00200000,
|
||||
/// <summary>
|
||||
/// Undocumented
|
||||
/// </summary>
|
||||
FILE_OPEN_NO_RECALL = 0x00400000,
|
||||
/// <summary>
|
||||
/// Undocumented
|
||||
/// </summary>
|
||||
FILE_OPEN_FOR_FREE_SPACE_QUERY = 0x00800000,
|
||||
|
||||
FILE_VALID_OPTION_FLAGS = 0x00ffffff,
|
||||
FILE_VALID_PIPE_OPTION_FLAGS = 0x00000032,
|
||||
FILE_VALID_MAILSLOT_OPTION_FLAGS = 0x00000032,
|
||||
FILE_VALID_SET_FLAGS = 0x00000036
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new file or opens an existing file.
|
||||
/// </summary>
|
||||
/// <returns>ZwCreateFile returns <see cref="NTSTATUS.STATUS_SUCCESS"/> on success or an appropriate <see cref="NTSTATUS"/> error code on failure. In the latter case, the caller can determine the cause of the failure by checking the <paramref name="IoStatusBlock"/> parameter.</returns>
|
||||
/// <param name="FileHandle">A pointer to a HANDLE variable that receives a handle to the file.</param>
|
||||
/// <param name="DesiredAccess">Specifies an <see cref="ACCESS_MASK"/> value that determines the requested access to the object. In addition to the access rights that are defined for all types of objects, the caller can specify any of the following access rights, which are specific to files.</param>
|
||||
/// <param name="ObjectAttributes">A pointer to an <see cref="OBJECT_ATTRIBUTES"/> structure that specifies the object name and other attributes.</param>
|
||||
/// <param name="IoStatusBlock">A pointer to an <see cref="IO_STATUS_BLOCK"/> structure that receives the final completion status and other information about the requested operation.</param>
|
||||
/// <param name="AllocationSize">A pointer to a LARGE_INTEGER that contains the initial allocation size, in bytes, for a file that is created or overwritten. If AllocationSize is <c>null</c>, no allocation size is specified. If no file is created or overwritten, AllocationSize is ignored.</param>
|
||||
/// <param name="FileAttributes">Specifies one or more FILE_ATTRIBUTE_XXX flags, which represent the file attributes to set if you create or overwrite a file. The caller usually specifies FILE_ATTRIBUTE_NORMAL, which sets the default attributes. For a list of valid FILE_ATTRIBUTE_XXX flags, see the CreateFile routine in the Microsoft Windows SDK documentation. If no file is created or overwritten, FileAttributes is ignored.</param>
|
||||
/// <param name="ShareAccess">Type of share access.</param>
|
||||
/// <param name="CreateDisposition">Specifies the action to perform if the file does or does not exist. <see cref="CreateDispositionFlags"/></param>
|
||||
/// <param name="CreateOptions">Specifies the options to apply when the file is created or opened. <see cref="CreateOptionsFlags"/></param>
|
||||
/// <param name="EaBuffer">Pointer to a <see cref="FILE_FULL_EA_INFORMATION"/>-structured buffer that contains the starting attributes to write to the file when creating, replacing or overwriting it.</param>
|
||||
/// <param name="EaLength">Length of the buffer pointed by <paramref name="EaBuffer"/></param>
|
||||
[DllImport(Libraries.NTDLL)]
|
||||
public static extern NTSTATUS NtCreateFile(out SafeFileHandle FileHandle,
|
||||
ACCESS_MASK DesiredAccess, ref OBJECT_ATTRIBUTES ObjectAttributes,
|
||||
ref IO_STATUS_BLOCK IoStatusBlock, ref long AllocationSize, uint FileAttributes,
|
||||
System.IO.FileShare ShareAccess, CreateDispositionFlags CreateDisposition, CreateOptionsFlags CreateOptions,
|
||||
IntPtr EaBuffer, uint EaLength);
|
||||
|
||||
/// <summary>
|
||||
/// Closes an object handle
|
||||
/// </summary>
|
||||
/// <returns>returns <see cref="NTSTATUS.STATUS_SUCCESS"/> on success, or the appropriate <see cref="NTSTATUS"/> error code on failure. In particular, it returns <see cref="NTSTATUS.STATUS_INVALID_HANDLE"/> if Handle is not a valid handle, or <see cref="NTSTATUS.STATUS_HANDLE_NOT_CLOSABLE"/> if the calling thread does not have permission to close the handle.</returns>
|
||||
/// <param name="handle">Handle to an object of any type.</param>
|
||||
[DllImport(Libraries.NTDLL)]
|
||||
public static extern NTSTATUS NtClose(SafeFileHandle handle);
|
||||
}
|
||||
}
|
||||
|
||||
235
Claunia.IO/Interop/Windows/Interop.Windows.types.cs
Normal file
235
Claunia.IO/Interop/Windows/Interop.Windows.types.cs
Normal file
@@ -0,0 +1,235 @@
|
||||
//
|
||||
// Interop.Windows.types.cs
|
||||
//
|
||||
// Author:
|
||||
// Natalia Portillo <claunia@claunia.com>
|
||||
//
|
||||
// Copyright (c) 2015 © Claunia.com
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
// of this software and associated documentation files (the "Software"), to deal
|
||||
// in the Software without restriction, including without limitation the rights
|
||||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
// copies of the Software, and to permit persons to whom the Software is
|
||||
// furnished to do so, subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included in
|
||||
// all copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
// THE SOFTWARE.
|
||||
using System.Runtime.InteropServices;
|
||||
using System;
|
||||
|
||||
internal static partial class Interop
|
||||
{
|
||||
internal static partial class Windows
|
||||
{
|
||||
[Flags]
|
||||
public enum ACCESS_MASK : UInt32
|
||||
{
|
||||
/// <summary>Right to read data from the file. (FILE)</summary>
|
||||
FILE_READ_DATA = 0x00000001,
|
||||
/// <summary>Right to list contents of a directory. (DIRECTORY)</summary>
|
||||
FILE_LIST_DIRECTORY = 0x00000001,
|
||||
|
||||
/// <summary>Right to write data to the file. (FILE)</summary>
|
||||
FILE_WRITE_DATA = 0x00000002,
|
||||
/// <summary>Right to create a file in the directory. (DIRECTORY)</summary>
|
||||
FILE_ADD_FILE = 0x00000002,
|
||||
|
||||
/// <summary>Right to append data to the file. (FILE)</summary>
|
||||
FILE_APPEND_DATA = 0x00000004,
|
||||
/// <summary>Right to create a subdirectory. (DIRECTORY)</summary>
|
||||
FILE_ADD_SUBDIRECTORY = 0x00000004,
|
||||
|
||||
/// <summary>Right to read extended attributes. (FILE/DIRECTORY)</summary>
|
||||
FILE_READ_EA = 0x00000008,
|
||||
|
||||
/// <summary>Right to write extended attributes. (FILE/DIRECTORY)</summary>
|
||||
FILE_WRITE_EA = 0x00000010,
|
||||
|
||||
/// <summary>Right to execute a file. (FILE)</summary>
|
||||
FILE_EXECUTE = 0x00000020,
|
||||
/// <summary>Right to traverse the directory. (DIRECTORY)</summary>
|
||||
FILE_TRAVERSE = 0x00000020,
|
||||
|
||||
/// <summary>
|
||||
/// Right to delete a directory and all the files it contains (its
|
||||
/// children, even if the files are read-only. (DIRECTORY)
|
||||
/// </summary>
|
||||
FILE_DELETE_CHILD = 0x00000040,
|
||||
|
||||
/// <summary>Right to read file attributes. (FILE/DIRECTORY)</summary>
|
||||
FILE_READ_ATTRIBUTES = 0x00000080,
|
||||
|
||||
/// <summary>Right to change file attributes. (FILE/DIRECTORY)</summary>
|
||||
FILE_WRITE_ATTRIBUTES = 0x00000100,
|
||||
|
||||
/// <summary>
|
||||
/// The standard rights (bits 16 to 23). Are independent of the type of
|
||||
/// object being secured.
|
||||
/// </summary>
|
||||
|
||||
/// <summary>Right to delete the object.</summary>
|
||||
DELETE = 0x00010000,
|
||||
|
||||
/// <summary>
|
||||
/// Right to read the information in the object's security descriptor,
|
||||
/// not including the information in the SACL. I.e. right to read the
|
||||
/// security descriptor and owner.
|
||||
/// </summary>
|
||||
READ_CONTROL = 0x00020000,
|
||||
|
||||
/// <summary>Right to modify the DACL in the object's security descriptor.</summary>
|
||||
WRITE_DAC = 0x00040000,
|
||||
|
||||
/// <summary>Right to change the owner in the object's security descriptor.</summary>
|
||||
WRITE_OWNER = 0x00080000,
|
||||
|
||||
/// <summary>
|
||||
/// Right to use the object for synchronization. Enables a process to
|
||||
/// wait until the object is in the signalled state. Some object types
|
||||
/// do not support this access right.
|
||||
/// </summary>
|
||||
SYNCHRONIZE = 0x00100000,
|
||||
|
||||
/// <summary>
|
||||
/// The following STANDARD_RIGHTS_* are combinations of the above for
|
||||
/// convenience and are defined by the Win32 API.
|
||||
/// </summary>
|
||||
|
||||
/// <summary>These are currently defined to READ_CONTROL.</summary>
|
||||
STANDARD_RIGHTS_READ = 0x00020000,
|
||||
STANDARD_RIGHTS_WRITE = 0x00020000,
|
||||
STANDARD_RIGHTS_EXECUTE = 0x00020000,
|
||||
|
||||
/// <summary>Combines DELETE, READ_CONTROL, WRITE_DAC, and WRITE_OWNER access.</summary>
|
||||
STANDARD_RIGHTS_REQUIRED = 0x000f0000,
|
||||
|
||||
/// <summary>
|
||||
/// Combines DELETE, READ_CONTROL, WRITE_DAC, WRITE_OWNER, and
|
||||
/// SYNCHRONIZE access.
|
||||
/// </summary>
|
||||
STANDARD_RIGHTS_ALL = 0x001f0000,
|
||||
|
||||
/// <summary>
|
||||
/// The access system ACL and maximum allowed access types (bits 24 to
|
||||
/// 25, bits 26 to 27 are reserved).
|
||||
/// </summary>
|
||||
ACCESS_SYSTEM_SECURITY = 0x01000000,
|
||||
MAXIMUM_ALLOWED = 0x02000000,
|
||||
|
||||
/// <summary>
|
||||
/// The generic rights (bits 28 to 31). These map onto the standard and
|
||||
/// specific rights.
|
||||
/// </summary>
|
||||
|
||||
/// <summary>Read, write, and execute access.</summary>
|
||||
GENERIC_ALL = 0x10000000,
|
||||
|
||||
/// <summary>Execute access.</summary>
|
||||
GENERIC_EXECUTE = 0x20000000,
|
||||
|
||||
/// <summary>
|
||||
/// Write access. For files, this maps onto:
|
||||
/// FILE_APPEND_DATA | FILE_WRITE_ATTRIBUTES | FILE_WRITE_DATA |
|
||||
/// FILE_WRITE_EA | STANDARD_RIGHTS_WRITE | SYNCHRONIZE
|
||||
/// For directories, the mapping has the same numerical value. See
|
||||
/// above for the descriptions of the rights granted.
|
||||
/// </summary>
|
||||
GENERIC_WRITE = 0x40000000,
|
||||
|
||||
/// <summary>
|
||||
/// Read access. For files, this maps onto:
|
||||
/// FILE_READ_ATTRIBUTES | FILE_READ_DATA | FILE_READ_EA |
|
||||
/// STANDARD_RIGHTS_READ | SYNCHRONIZE
|
||||
/// For directories, the mapping has the same numerical value. See
|
||||
/// above for the descriptions of the rights granted.
|
||||
/// </summary>
|
||||
GENERIC_READ = 0x80000000,
|
||||
}
|
||||
|
||||
[StructLayout(LayoutKind.Sequential)]
|
||||
public struct UNICODE_STRING : IDisposable
|
||||
{
|
||||
public ushort Length;
|
||||
public ushort MaximumLength;
|
||||
private IntPtr buffer;
|
||||
|
||||
public UNICODE_STRING(string s)
|
||||
{
|
||||
Length = (ushort)(s.Length * 2);
|
||||
MaximumLength = (ushort)(Length + 2);
|
||||
buffer = Marshal.StringToHGlobalUni(s);
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
Marshal.FreeHGlobal(buffer);
|
||||
buffer = IntPtr.Zero;
|
||||
}
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
return Marshal.PtrToStringUni(buffer);
|
||||
}
|
||||
}
|
||||
|
||||
[StructLayout(LayoutKind.Sequential)]
|
||||
public struct OBJECT_ATTRIBUTES : IDisposable
|
||||
{
|
||||
public int Length;
|
||||
public IntPtr RootDirectory;
|
||||
private IntPtr objectName;
|
||||
public uint Attributes;
|
||||
public IntPtr SecurityDescriptor;
|
||||
public IntPtr SecurityQualityOfService;
|
||||
|
||||
public OBJECT_ATTRIBUTES(string name, uint attrs)
|
||||
{
|
||||
Length = 0;
|
||||
RootDirectory = IntPtr.Zero;
|
||||
objectName = IntPtr.Zero;
|
||||
Attributes = attrs;
|
||||
SecurityDescriptor = IntPtr.Zero;
|
||||
SecurityQualityOfService = IntPtr.Zero;
|
||||
|
||||
Length = Marshal.SizeOf(this);
|
||||
ObjectName = new UNICODE_STRING(name);
|
||||
}
|
||||
|
||||
public UNICODE_STRING ObjectName
|
||||
{
|
||||
get
|
||||
{
|
||||
return (UNICODE_STRING)Marshal.PtrToStructure(
|
||||
objectName, typeof(UNICODE_STRING));
|
||||
}
|
||||
|
||||
set
|
||||
{
|
||||
bool fDeleteOld = objectName != IntPtr.Zero;
|
||||
if (!fDeleteOld)
|
||||
objectName = Marshal.AllocHGlobal(Marshal.SizeOf(value));
|
||||
Marshal.StructureToPtr(value, objectName, fDeleteOld);
|
||||
}
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
if (objectName != IntPtr.Zero)
|
||||
{
|
||||
Marshal.DestroyStructure(objectName, typeof(UNICODE_STRING));
|
||||
Marshal.FreeHGlobal(objectName);
|
||||
objectName = IntPtr.Zero;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user