8000 implement nt.getlogin · RustPython/RustPython@5ce8604 · GitHub
[go: up one dir, main page]

Skip to content

Commit 5ce8604

Browse files
arihant2mathyouknowone
authored andcommitted
implement nt.getlogin
Signed-off-by: Ashwin Naren <arihant2math@gmail.com>
1 parent 320d745 commit 5ce8604

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

vm/src/stdlib/nt.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -388,6 +388,27 @@ pub(crate) mod module {
388388
}
389389
}
390390

391+
#[pyfunction]
392+
fn getlogin(vm: &VirtualMachine) -> PyResult<String> {
393+
let mut buffer = [0u16; 257];
394+
let mut size = buffer.len() as u32;
395+
396+
let success = unsafe {
397+
windows_sys::Win32::System::WindowsProgramming::GetUserNameW(
398+
buffer.as_mut_ptr(),
399+
&mut size,
400+
)
401+
};
402+
403+
if success != 0 {
404+
// Convert the buffer (which is UTF-16) to a Rust String
405+
let username = std::ffi::OsString::from_wide(&buffer[..(size - 1) as usize]);
406+
Ok(username.to_str().unwrap().to_string())
407+
} else {
408+
Err(vm.new_os_error(format!("Error code: {success}")))
409+
}
410+
}
411+
391412
pub fn raw_set_handle_inheritable(handle: intptr_t, inheritable: bool) -> std::io::Result<()> {
392413
let flags = if inheritable {
393414
Foundation::HANDLE_FLAG_INHERIT

0 commit comments

Comments
 (0)
0