Skip to content

Commit fae255b

Browse files
committed
implemented with windows
1 parent aee9796 commit fae255b

4 files changed

Lines changed: 63 additions & 62 deletions

File tree

stdlib/src/lib.rs

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@ mod cmath;
1212
mod contextvars;
1313
mod csv;
1414
mod dis;
15-
#[cfg(windows)]
16-
mod win32api;
1715
mod gc;
1816
mod hashlib;
1917
mod json;
@@ -135,11 +133,6 @@ pub fn get_module_inits() -> impl Iterator<Item = (Cow<'static, str>, StdlibInit
135133
{
136134
"_bz2" => bz2::make_module,
137135
}
138-
// Windoes-only
139-
#[cfg(windows)]
140-
{
141-
"win32api" => win32api::make_module,
142-
}
143136
// Unix-only
144137
#[cfg(unix)]
145138
{

stdlib/src/win32api.rs

Lines changed: 0 additions & 53 deletions
This file was deleted.

vm/Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,9 +113,9 @@ schannel = "0.1.19"
113113
widestring = "0.5.1"
114114

115115
[target.'cfg(windows)'.dependencies.windows]
116-
version = "0.39"
116+
version = "0.39.0"
117117
features = [
118-
"Win32_UI_Shell",
118+
"Win32_UI_Shell", "Win32_System_LibraryLoader", "Win32_Foundation"
119119
]
120120

121121
[target.'cfg(windows)'.dependencies.winapi]

vm/src/stdlib/winapi.rs

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,14 @@ mod _winapi {
1616
fileapi, handleapi, namedpipeapi, processenv, processthreadsapi, synchapi, winbase,
1717
winnt::HANDLE,
1818
};
19+
// windows
20+
use windows::Win32::Foundation::{
21+
HINSTANCE, MAX_PATH,
22+
};
23+
use windows::Win32::System::LibraryLoader::{
24+
LoadLibraryW, GetModuleFileNameW,
25+
};
26+
use windows::core::PCWSTR;
1927

2028
#[pyattr]
2129
use winapi::{
@@ -402,4 +410,57 @@ mod _winapi {
402410
})
403411
.map(drop)
404412
}
413+
414+
use std::ffi::{OsStr, OsString};
415+
use std::os::windows::prelude::*;
416+
pub trait ToWide {
417+
fn to_wide(&self) -> Vec<u16>;
418+
fn to_wide_null(&self) -> Vec<u16>;
419+
}
420+
impl<T> ToWide for T where T: AsRef<OsStr> {
421+
fn to_wide(&self) -> Vec<u16> {
422+
self.as_ref().encode_wide().collect()
423+
}
424+
fn to_wide_null(&self) -> Vec<u16> {
425+
self.as_ref().encode_wide().chain(Some(0)).collect()
426+
}
427+
}
428+
pub trait FromWide where Self: Sized {
429+
fn from_wide_null(wide: &[u16]) -> Self;
430+
}
431+
impl FromWide for OsString {
432+
fn from_wide_null(wide: &[u16]) -> OsString {
433+
let len = wide.iter().take_while(|&&c| c != 0).count();
434+
OsString::from_wide(&wide[..len])
435+
}
436+
}
437+
438+
439+
#[pyfunction]
440+
fn LoadLibrary(path: PyStrRef, vm: &VirtualMachine) -> PyResult<isize> {
441+
let path = path.as_str().to_wide_null();
442+
let handle = unsafe { LoadLibraryW(PCWSTR::from_raw(path.as_ptr())).unwrap() };
443+
if handle.is_invalid() {
444+
return Err(
445+
vm.new_runtime_error("LoadLibrary failed".to_owned())
446+
);
447+
}
448+
Ok(handle.0)
449+
}
450+
451+
#[pyfunction]
452+
fn GetModuleFileName(handle: isize, vm: &VirtualMachine) -> PyResult<String> {
453+
let mut path: Vec<u16> = vec![0; MAX_PATH as usize];
454+
let handle = HINSTANCE(handle);
455+
456+
let length = unsafe { GetModuleFileNameW(handle, &mut path) };
457+
if length == 0 {
458+
return Err(
459+
vm.new_runtime_error("GetModuleFileName failed".to_owned())
460+
);
461+
}
462+
463+
let (path, _) = path.split_at(length as usize);
464+
Ok(String::from_utf16(&path).unwrap())
465+
}
405466
}

0 commit comments

Comments
 (0)