pub struct HFILE(/* private fields */);
kernel
only.Expand description
Implementations§
Source§impl HFILE
impl HFILE
Sourcepub const unsafe fn from_ptr(p: *mut c_void) -> Self
pub const unsafe fn from_ptr(p: *mut c_void) -> Self
Constructs a new handle object by wrapping a pointer.
This method can be used as an escape hatch to interoperate with other libraries.
§Safety
Be sure the pointer has the correct type and isn’t owned by anyone else, otherwise you may cause memory access violations.
Sourcepub const unsafe fn raw_copy(&self) -> Self
pub const unsafe fn raw_copy(&self) -> Self
Returns a raw copy of the underlying handle pointer.
§Safety
As the name implies, raw_copy
returns a raw copy of the
handle, so closing one of the copies won’t close the others.
This means a handle can be used after it has been closed, what
can lead to errors and undefined behavior. Even worse: sometimes
Windows reuses handle values, so you can call a method on a
completely different handle type, what can be catastrophic.
However, in some cases the Windows API demands a copy of the
handle – raw_copy
is an escape hatch to fill this gap.
Sourcepub const unsafe fn as_mut(&mut self) -> &mut *mut c_void
pub const unsafe fn as_mut(&mut self) -> &mut *mut c_void
Returns a mutable reference to the underlying raw pointer.
This method can be used as an escape hatch to interoperate with other libraries.
§Safety
This method exposes the raw pointer used by raw Windows calls. It’s an opaque pointer to an internal Windows structure, and no dereferencings should be attempted.
Sourcepub const fn ptr(&self) -> *mut c_void
pub const fn ptr(&self) -> *mut c_void
Returns the underlying raw pointer.
This method exposes the raw pointer used by raw Windows calls. It’s an opaque pointer to an internal Windows structure, and no dereferencings should be attempted.
This method can be used as an escape hatch to interoperate with other libraries.
Source§impl HFILE
impl HFILE
Sourcepub fn CreateFile(
file_name: &str,
desired_access: GENERIC,
share_mode: Option<FILE_SHARE>,
security_attributes: Option<&SECURITY_ATTRIBUTES<'_>>,
creation_disposition: DISPOSITION,
attributes: FILE_ATTRIBUTE,
flags: Option<FILE_FLAG>,
security: Option<FILE_SECURITY>,
hfile_template: Option<&HFILE>,
) -> SysResult<(CloseHandleGuard<HFILE>, ERROR)>
pub fn CreateFile( file_name: &str, desired_access: GENERIC, share_mode: Option<FILE_SHARE>, security_attributes: Option<&SECURITY_ATTRIBUTES<'_>>, creation_disposition: DISPOSITION, attributes: FILE_ATTRIBUTE, flags: Option<FILE_FLAG>, security: Option<FILE_SECURITY>, hfile_template: Option<&HFILE>, ) -> SysResult<(CloseHandleGuard<HFILE>, ERROR)>
CreateFile
function.
The error code is also returned because it can carry information even if the file is successfully open.
Unless you need something specific, consider using the
File
high-level abstraction.
§Examples
Opening an existing file as read-only:
use winsafe::{self as w, prelude::*, co};
let (hfile, status) = w::HFILE::CreateFile(
"C:\\Temp\\test.txt",
co::GENERIC::READ,
Some(co::FILE_SHARE::READ),
None,
co::DISPOSITION::OPEN_EXISTING,
co::FILE_ATTRIBUTE::NORMAL,
None,
None,
None,
)?;
Opening a file for read and write. If the file doesn’t exist, create it:
use winsafe::{self as w, prelude::*, co};
let (hfile, status) = w::HFILE::CreateFile(
"C:\\Temp\\test.txt",
co::GENERIC::READ | co::GENERIC::WRITE,
None,
None,
co::DISPOSITION::OPEN_ALWAYS,
co::FILE_ATTRIBUTE::NORMAL,
None,
None,
None,
)?;
Sourcepub fn CreateFileMapping(
&self,
mapping_attrs: Option<&SECURITY_ATTRIBUTES<'_>>,
protect: PAGE,
sec: Option<SEC>,
max_size: Option<u64>,
mapping_name: Option<&str>,
) -> SysResult<CloseHandleGuard<HFILEMAP>>
pub fn CreateFileMapping( &self, mapping_attrs: Option<&SECURITY_ATTRIBUTES<'_>>, protect: PAGE, sec: Option<SEC>, max_size: Option<u64>, mapping_name: Option<&str>, ) -> SysResult<CloseHandleGuard<HFILEMAP>>
CreateFileMapping
function.
Unless you need something specific, consider using the
FileMapped
high-level abstraction.
Sourcepub fn GetFileInformationByHandle(
&self,
) -> SysResult<BY_HANDLE_FILE_INFORMATION>
pub fn GetFileInformationByHandle( &self, ) -> SysResult<BY_HANDLE_FILE_INFORMATION>
GetFileInformationByHandle
function.
Sourcepub fn GetFileSizeEx(&self) -> SysResult<u64>
pub fn GetFileSizeEx(&self) -> SysResult<u64>
GetFileSizeEx
function.
Sourcepub fn GetFileTime(&self) -> SysResult<(FILETIME, FILETIME, FILETIME)>
pub fn GetFileTime(&self) -> SysResult<(FILETIME, FILETIME, FILETIME)>
GetFileTime
function.
Returns, respectively:
- creation time;
- last access time;
- last write time.
§Examples
use winsafe::{self as w, prelude::*, co};
let hfile: w::HFILE; // initialized somewhere
let (creation, last_access, last_write) = hfile.GetFileTime()?;
Sourcepub fn GetFileType(&self) -> SysResult<FILE_TYPE>
pub fn GetFileType(&self) -> SysResult<FILE_TYPE>
GetFileType
function.
Sourcepub fn LockFile(
&self,
offset: u64,
num_bytes_to_lock: u64,
) -> SysResult<UnlockFileGuard<'_>>
pub fn LockFile( &self, offset: u64, num_bytes_to_lock: u64, ) -> SysResult<UnlockFileGuard<'_>>
LockFile
function.
In the original C implementation, you must call
UnlockFile
as a cleanup operation.
Here, the cleanup is performed automatically, because LockFile
returns
an UnlockFileGuard
, which
automatically calls UnlockFile
when the guard goes out of scope. You
must, however, keep the guard alive, otherwise the cleanup will be
performed right away.
§Examples
use winsafe::{self as w, prelude::*};
let hfile: w::HFILE; // initialized somewhere
let total_size = hfile.GetFileSizeEx()?;
let _lock_guard = hfile.LockFile(0, total_size as _)?; // keep guard alive
// file read/write operations...
// UnlockFile() called automatically
Sourcepub fn ReadFile(&self, buffer: &mut [u8]) -> SysResult<u32>
pub fn ReadFile(&self, buffer: &mut [u8]) -> SysResult<u32>
ReadFile
function.
Reads at most buffer.len()
bytes from the file, starting at the
current file pointer offset. Returns how many bytes were actually read.
The file pointer is then incremented by the number of bytes read.
Note that asynchronous reading – which use the
OVERLAPPED
struct – is not currently supported by
this method, because the buffer must remain untouched until the async
operation is complete, thus making the method unsound.
Sourcepub fn SetEndOfFile(&self) -> SysResult<()>
pub fn SetEndOfFile(&self) -> SysResult<()>
SetEndOfFile
function.
Sourcepub fn SetFilePointerEx(
&self,
distance_to_move: i64,
move_method: FILE_STARTING_POINT,
) -> SysResult<i64>
pub fn SetFilePointerEx( &self, distance_to_move: i64, move_method: FILE_STARTING_POINT, ) -> SysResult<i64>
SetFilePointerEx
function.
Sourcepub fn SetFileTime(
&self,
creation_time: Option<&FILETIME>,
last_access_time: Option<&FILETIME>,
last_write_time: Option<&FILETIME>,
) -> SysResult<()>
pub fn SetFileTime( &self, creation_time: Option<&FILETIME>, last_access_time: Option<&FILETIME>, last_write_time: Option<&FILETIME>, ) -> SysResult<()>
SetFileTime
function.
Sourcepub fn WriteFile(&self, data: &[u8]) -> SysResult<u32>
pub fn WriteFile(&self, data: &[u8]) -> SysResult<u32>
WriteFile
function.
Returns the number of bytes written.
Note that asynchronous writing – which use the
OVERLAPPED
struct – is not currently supported by
this method, because the buffer must remain untouched until the async
operation is complete, thus making the method unsound.