V
V
Vahe2018-03-29 12:07:26
Programming
Vahe, 2018-03-29 12:07:26

Rust WinApi and ReadDirectoryChangesW?

Hello, I recently started learning Rust, and I want to know what file was added or deleted in a particular directory. I found the functions winapi::um::winbase::ReadDirectoryChangesW and I wanted to implement it.

#[cfg(windows)]
extern crate winapi;

use std::ffi::CString;
extern crate schedule_recv;

use winapi::um::fileapi::{CreateFileA, OPEN_EXISTING};
use winapi::um::winbase::{ReadDirectoryChangesW, FILE_FLAG_BACKUP_SEMANTICS};

use winapi::um::winnt::{FILE_LIST_DIRECTORY, FILE_NOTIFY_CHANGE_FILE_NAME, FILE_SHARE_READ};

fn main() {
    unsafe {
        // ..
        let hDir = CreateFileA(
            CString::new("C:/Users/Admin/Desktop/tmp/")
                .unwrap()
                .as_ptr(),
            FILE_LIST_DIRECTORY,
            FILE_SHARE_READ,
            None.unwrap(),
            OPEN_EXISTING,
            FILE_FLAG_BACKUP_SEMANTICS,
            None.unwrap(),
        );
        let x = ReadDirectoryChangesW(
            hDir,
            std::ptr::null_mut(),
            0,
            0,
            FILE_NOTIFY_CHANGE_FILE_NAME,
            std::ptr::null_mut(),
            None.unwrap(),
            None.unwrap(),
        );
        // ...
    }
}

Mistake.
thread 'main' panicked at 'called `Option::unwrap()` on a `None` value', src\lib
core\option.rs:335:21
note: Run with `RUST_BACKTRACE=1` for a backtrace.
error: process didn't exit successfully: `target\debug\fw.exe` (exit code: 101)

thanks in advance

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Andrey Lesnikov, 2018-03-29
@vahe_2000

The crash is probably due to `None.unwrap()` - this expression will always cause a panic in general, because the point of unwrap is precisely this: unwrap the value if it is Some, or crash the application.
You can, for example, read https://habrahabr.ru/post/270371/ .
------
Well, yes, the task is precisely to implement this yourself? And then you can already take something ready. https://crates.io/crates/notify some, for example.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question