How can I obtain access to a networked location?

Viewed 316

My program, when started up with the system, is unable to access a networked location:

fn main() {
    ensure_network("\\\\SERVER\\".to_string());
}

fn ensure_network(network_dir: String) {
    let timer = std::time::Instant::now();
    let mut prev_counter = 0;
    loop {
        if std::fs::read_dir(&network_dir).is_ok() {
            break;
        }
        if timer.elapsed().as_secs() > prev_counter + 60 {
            println!("Still Failing.");
            prev_counter = timer.elapsed().as_secs();
        }
        std::hint::spin_loop();
    }
    println!("Network access obtained (Time elapsed: {})",
        timer.elapsed().as_secs_f32());
}

Edit (Restating problem after much research into the issue):

This program starts up with the PC using Task Scheduler. It is set to "Run only when user is logged on" and to "Run with highest privileges." However, most of the time the program fails to find the connection and gives the error, "The user name or password is incorrect. (os error 1326)."

  • The program succeeds when run manually with administrator privilege.
  • On occasion the program will succeed on startup, but this is rare.
  • The program will succeed if any other application is started as administrator after the program enters its loop.
1 Answers

On Task Scheduler you can delay the execution of the task.

It's okay if you execute it after login, but when Active Directory or anyway a Domain system is between you and the login, the connection to the shared storage may take a while, and the program may try to execute before this happens. Try to put on a 10-20 seconds delay on it and see if this solve your problem.

If it doesn't work, again supposing that you have a domain in the middle, you may need to explicit give user and passwd to access the network where the directory you're looking for.

Related