// This file is part of Deja-Vu. // // Deja-Vu is free software: you can redistribute it and/or modify it // under the terms of the GNU General Public License as published by // the Free Software Foundation, either version 3 of the License, or // (at your option) any later version. // // Deja-Vu is distributed in the hope that it will be useful, but // WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. // See the GNU General Public License for more details. // // You should have received a copy of the GNU General Public License // along with Deja-Vu. If not, see . // //#![windows_subsystem = "windows"] use std::ffi::CString; use windows_sys::Win32::Foundation::NO_ERROR; use windows_sys::Win32::NetworkManagement::WNet; fn to_cstr(s: &str) -> CString { CString::new(s).unwrap() } pub fn connect_network_share( server: &str, share: &str, username: &str, password: &str, ) -> Result<(), u32> { let remote_name = to_cstr(&format!("\\\\{server}\\{share}")); // init resources let mut resources = WNet::NETRESOURCEA { dwDisplayType: WNet::RESOURCEDISPLAYTYPE_SHAREADMIN, dwScope: WNet::RESOURCE_GLOBALNET, dwType: WNet::RESOURCETYPE_DISK, dwUsage: WNet::RESOURCEUSAGE_ALL, lpComment: std::ptr::null_mut(), lpLocalName: std::ptr::null_mut(), // PUT a volume here if you want to mount as a windows volume lpProvider: std::ptr::null_mut(), lpRemoteName: remote_name.as_c_str().as_ptr() as *mut u8, }; let username = format!("{server}\\{username}"); let username = to_cstr(&username); let password = to_cstr(password); // mount let result = unsafe { let username_ptr = username.as_ptr(); let password_ptr = password.as_ptr(); WNet::WNetAddConnection2A( &mut resources as *mut WNet::NETRESOURCEA, password_ptr as *const u8, username_ptr as *const u8, //WNet::CONNECT_INTERACTIVE, // Interactive will show a system dialog in case credentials are wrong to retry with the password. Put 0 if you don't want it 0, ) }; if result == NO_ERROR { Ok(()) } else { Err(result) } }