port to windows-rs (#5080)

* Fix OpenSSL in windows CI

* bump windows-rs

* prepare windows-sys 0.48

* CloseHandle

* DuplicateHandle

* CreatePipe

* GetFileType

* GetExitCodeProcess

* TerminateProcess

* GetStdHandle

* GetCurrentProcess

* DeleteProcThreadAttributeList

* WaitForSingleObject

* CreateProcessW

* InitializeProcThreadAttributeList

* UpdateProcThreadAttribute

* clean up helpers
This commit is contained in:
Jeong, YunWon
2023-10-07 03:01:42 +09:00
committed by GitHub
parent 2fa88f94b6
commit 987d50c092
4 changed files with 213 additions and 168 deletions

View File

@@ -257,9 +257,9 @@ jobs:
run: |
choco install llvm openssl --no-progress
if [[ -d "C:\Program Files\OpenSSL-Win64" ]]; then
echo "OPENSSL_DIR=C:\Program Files\OpenSSL-Win64;" >> $GITHUB_ENV
echo "OPENSSL_DIR=C:\Program Files\OpenSSL-Win64" >> $GITHUB_ENV
else
echo "OPENSSL_DIR=C:\Program Files\OpenSSL;" >> $GITHUB_ENV
echo "OPENSSL_DIR=C:\Program Files\OpenSSL" >> $GITHUB_ENV
fi
if: runner.os == 'Windows'
- name: Set up the Mac environment

101
Cargo.lock generated
View File

@@ -253,7 +253,7 @@ dependencies = [
"js-sys",
"num-traits",
"wasm-bindgen",
"windows-targets 0.48.0",
"windows-targets 0.48.5",
]
[[package]]
@@ -2384,6 +2384,7 @@ dependencies = [
"widestring",
"winapi",
"windows",
"windows-sys 0.48.0",
"winreg",
]
@@ -3271,15 +3272,21 @@ checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f"
[[package]]
name = "windows"
version = "0.39.0"
version = "0.51.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f1c4bd0a50ac6020f65184721f758dba47bb9fbc2133df715ec74a237b26794a"
checksum = "ca229916c5ee38c2f2bc1e9d8f04df975b4bd93f9955dc69fabb5d91270045c9"
dependencies = [
"windows_aarch64_msvc 0.39.0",
"windows_i686_gnu 0.39.0",
"windows_i686_msvc 0.39.0",
"windows_x86_64_gnu 0.39.0",
"windows_x86_64_msvc 0.39.0",
"windows-core",
"windows-targets 0.48.5",
]
[[package]]
name = "windows-core"
version = "0.51.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f1f8cf84f35d2db49a46868f947758c7a1138116f7fac3bc844f43ade1292e64"
dependencies = [
"windows-targets 0.48.5",
]
[[package]]
@@ -3325,7 +3332,7 @@ version = "0.48.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "677d2418bec65e3338edb076e806bc1ec15693c5d0104683f2efe857f61056a9"
dependencies = [
"windows-targets 0.48.0",
"windows-targets 0.48.5",
]
[[package]]
@@ -3345,17 +3352,17 @@ dependencies = [
[[package]]
name = "windows-targets"
version = "0.48.0"
version = "0.48.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7b1eb6f0cd7c80c79759c929114ef071b87354ce476d9d94271031c0497adfd5"
checksum = "9a2fa6e2155d7247be68c096456083145c183cbbbc2764150dda45a87197940c"
dependencies = [
"windows_aarch64_gnullvm 0.48.0",
"windows_aarch64_msvc 0.48.0",
"windows_i686_gnu 0.48.0",
"windows_i686_msvc 0.48.0",
"windows_x86_64_gnu 0.48.0",
"windows_x86_64_gnullvm 0.48.0",
"windows_x86_64_msvc 0.48.0",
"windows_aarch64_gnullvm 0.48.5",
"windows_aarch64_msvc 0.48.5",
"windows_i686_gnu 0.48.5",
"windows_i686_msvc 0.48.5",
"windows_x86_64_gnu 0.48.5",
"windows_x86_64_gnullvm 0.48.5",
"windows_x86_64_msvc 0.48.5",
]
[[package]]
@@ -3366,9 +3373,9 @@ checksum = "8c9864e83243fdec7fc9c5444389dcbbfd258f745e7853198f365e3c4968a608"
[[package]]
name = "windows_aarch64_gnullvm"
version = "0.48.0"
version = "0.48.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "91ae572e1b79dba883e0d315474df7305d12f569b400fcf90581b06062f7e1bc"
checksum = "2b38e32f0abccf9987a4e3079dfb67dcd799fb61361e53e2882c3cbaf0d905d8"
[[package]]
name = "windows_aarch64_msvc"
@@ -3376,12 +3383,6 @@ version = "0.36.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9bb8c3fd39ade2d67e9874ac4f3db21f0d710bee00fe7cab16949ec184eeaa47"
[[package]]
name = "windows_aarch64_msvc"
version = "0.39.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ec7711666096bd4096ffa835238905bb33fb87267910e154b18b44eaabb340f2"
[[package]]
name = "windows_aarch64_msvc"
version = "0.42.1"
@@ -3390,9 +3391,9 @@ checksum = "4c8b1b673ffc16c47a9ff48570a9d85e25d265735c503681332589af6253c6c7"
[[package]]
name = "windows_aarch64_msvc"
version = "0.48.0"
version = "0.48.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b2ef27e0d7bdfcfc7b868b317c1d32c641a6fe4629c171b8928c7b08d98d7cf3"
checksum = "dc35310971f3b2dbbf3f0690a219f40e2d9afcf64f9ab7cc1be722937c26b4bc"
[[package]]
name = "windows_i686_gnu"
@@ -3400,12 +3401,6 @@ version = "0.36.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "180e6ccf01daf4c426b846dfc66db1fc518f074baa793aa7d9b9aaeffad6a3b6"
[[package]]
name = "windows_i686_gnu"
version = "0.39.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "763fc57100a5f7042e3057e7e8d9bdd7860d330070251a73d003563a3bb49e1b"
[[package]]
name = "windows_i686_gnu"
version = "0.42.1"
@@ -3414,9 +3409,9 @@ checksum = "de3887528ad530ba7bdbb1faa8275ec7a1155a45ffa57c37993960277145d640"
[[package]]
name = "windows_i686_gnu"
version = "0.48.0"
version = "0.48.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "622a1962a7db830d6fd0a69683c80a18fda201879f0f447f065a3b7467daa241"
checksum = "a75915e7def60c94dcef72200b9a8e58e5091744960da64ec734a6c6e9b3743e"
[[package]]
name = "windows_i686_msvc"
@@ -3424,12 +3419,6 @@ version = "0.36.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e2e7917148b2812d1eeafaeb22a97e4813dfa60a3f8f78ebe204bcc88f12f024"
[[package]]
name = "windows_i686_msvc"
version = "0.39.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7bc7cbfe58828921e10a9f446fcaaf649204dcfe6c1ddd712c5eebae6bda1106"
[[package]]
name = "windows_i686_msvc"
version = "0.42.1"
@@ -3438,9 +3427,9 @@ checksum = "bf4d1122317eddd6ff351aa852118a2418ad4214e6613a50e0191f7004372605"
[[package]]
name = "windows_i686_msvc"
version = "0.48.0"
version = "0.48.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4542c6e364ce21bf45d69fdd2a8e455fa38d316158cfd43b3ac1c5b1b19f8e00"
checksum = "8f55c233f70c4b27f66c523580f78f1004e8b5a8b659e05a4eb49d4166cca406"
[[package]]
name = "windows_x86_64_gnu"
@@ -3448,12 +3437,6 @@ version = "0.36.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4dcd171b8776c41b97521e5da127a2d86ad280114807d0b2ab1e462bc764d9e1"
[[package]]
name = "windows_x86_64_gnu"
version = "0.39.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6868c165637d653ae1e8dc4d82c25d4f97dd6605eaa8d784b5c6e0ab2a252b65"
[[package]]
name = "windows_x86_64_gnu"
version = "0.42.1"
@@ -3462,9 +3445,9 @@ checksum = "c1040f221285e17ebccbc2591ffdc2d44ee1f9186324dd3e84e99ac68d699c45"
[[package]]
name = "windows_x86_64_gnu"
version = "0.48.0"
version = "0.48.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ca2b8a661f7628cbd23440e50b05d705db3686f894fc9580820623656af974b1"
checksum = "53d40abd2583d23e4718fddf1ebec84dbff8381c07cae67ff7768bbf19c6718e"
[[package]]
name = "windows_x86_64_gnullvm"
@@ -3474,9 +3457,9 @@ checksum = "628bfdf232daa22b0d64fdb62b09fcc36bb01f05a3939e20ab73aaf9470d0463"
[[package]]
name = "windows_x86_64_gnullvm"
version = "0.48.0"
version = "0.48.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7896dbc1f41e08872e9d5e8f8baa8fdd2677f29468c4e156210174edc7f7b953"
checksum = "0b7b52767868a23d5bab768e390dc5f5c55825b6d30b86c844ff2dc7414044cc"
[[package]]
name = "windows_x86_64_msvc"
@@ -3484,12 +3467,6 @@ version = "0.36.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c811ca4a8c853ef420abd8592ba53ddbbac90410fab6903b3e79972a631f7680"
[[package]]
name = "windows_x86_64_msvc"
version = "0.39.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5e4d40883ae9cae962787ca76ba76390ffa29214667a111db9e0a1ad8377e809"
[[package]]
name = "windows_x86_64_msvc"
version = "0.42.1"
@@ -3498,9 +3475,9 @@ checksum = "447660ad36a13288b1db4d4248e857b510e8c3a225c822ba4fb748c0aafecffd"
[[package]]
name = "windows_x86_64_msvc"
version = "0.48.0"
version = "0.48.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1a515f5799fe4961cb532f983ce2b23082366b898e52ffbce459c86f67c8378a"
checksum = "ed94fce61571a4006852b7389a063ab983c02eb1bb37b47f8272ce92d06d9538"
[[package]]
name = "winreg"

View File

@@ -113,9 +113,25 @@ widestring = { workspace = true }
winreg = "0.10.1"
[target.'cfg(windows)'.dependencies.windows]
version = "0.39.0"
version = "0.51.1"
features = [
"Win32_UI_Shell", "Win32_System_LibraryLoader", "Win32_Foundation"
"Win32_Foundation",
"Win32_System_LibraryLoader",
"Win32_System_Threading",
"Win32_UI_Shell",
]
[target.'cfg(windows)'.dependencies.windows-sys]
version = "0.48.0"
features = [
"Win32_Foundation",
"Win32_Security",
"Win32_Storage_FileSystem",
"Win32_System_Console",
"Win32_System_LibraryLoader",
"Win32_System_Pipes",
"Win32_System_Threading",
"Win32_UI_Shell",
]
[target.'cfg(windows)'.dependencies.winapi]

View File

@@ -6,22 +6,18 @@ mod _winapi {
use crate::{
builtins::PyStrRef,
common::windows::ToWideString,
convert::ToPyException,
convert::{ToPyException, ToPyObject, ToPyResult},
function::{ArgMapping, ArgSequence, OptionalArg},
stdlib::os::errno_err,
PyObjectRef, PyResult, TryFromObject, VirtualMachine,
};
use std::ptr::{null, null_mut};
use winapi::shared::winerror;
use winapi::um::{
fileapi, handleapi, namedpipeapi, processenv, processthreadsapi, synchapi, winbase,
winnt::HANDLE,
};
use winapi::um::winbase;
use windows::{
core::PCWSTR,
Win32::Foundation::{HINSTANCE, MAX_PATH},
Win32::System::LibraryLoader::{GetModuleFileNameW, LoadLibraryW},
Win32::Foundation::{HANDLE, HINSTANCE, MAX_PATH},
};
use windows_sys::Win32::Foundation::{BOOL, HANDLE as RAW_HANDLE};
#[pyattr]
use winapi::{
@@ -66,41 +62,77 @@ mod _winapi {
unsafe { winapi::um::errhandlingapi::GetLastError() }
}
fn husize(h: HANDLE) -> usize {
h as usize
}
trait Convertible {
trait WindowsSysResultValue {
type Ok: ToPyObject;
fn is_err(&self) -> bool;
fn into_ok(self) -> Self::Ok;
}
impl Convertible for HANDLE {
impl WindowsSysResultValue for RAW_HANDLE {
type Ok = HANDLE;
fn is_err(&self) -> bool {
*self == handleapi::INVALID_HANDLE_VALUE
*self == windows_sys::Win32::Foundation::INVALID_HANDLE_VALUE
}
fn into_ok(self) -> Self::Ok {
HANDLE(self)
}
}
impl Convertible for i32 {
impl WindowsSysResultValue for BOOL {
type Ok = ();
fn is_err(&self) -> bool {
*self == 0
}
fn into_ok(self) -> Self::Ok {}
}
fn cvt<T: Convertible>(vm: &VirtualMachine, res: T) -> PyResult<T> {
if res.is_err() {
Err(errno_err(vm))
} else {
Ok(res)
struct WindowsSysResult<T>(T);
impl<T: WindowsSysResultValue> WindowsSysResult<T> {
fn is_err(&self) -> bool {
self.0.is_err()
}
fn into_pyresult(self, vm: &VirtualMachine) -> PyResult<T::Ok> {
if self.is_err() {
Err(errno_err(vm))
} else {
Ok(self.0.into_ok())
}
}
}
impl<T: WindowsSysResultValue> ToPyResult for WindowsSysResult<T> {
fn to_pyresult(self, vm: &VirtualMachine) -> PyResult {
let ok = self.into_pyresult(vm)?;
Ok(ok.to_pyobject(vm))
}
}
type HandleInt = usize; // TODO: change to isize when fully ported to windows-rs
impl TryFromObject for HANDLE {
fn try_from_object(vm: &VirtualMachine, obj: PyObjectRef) -> PyResult<Self> {
let handle = HandleInt::try_from_object(vm, obj)?;
Ok(HANDLE(handle as isize))
}
}
impl ToPyObject for HANDLE {
fn to_pyobject(self, vm: &VirtualMachine) -> PyObjectRef {
(self.0 as HandleInt).to_pyobject(vm)
}
}
#[pyfunction]
fn CloseHandle(handle: usize, vm: &VirtualMachine) -> PyResult<()> {
cvt(vm, unsafe { handleapi::CloseHandle(handle as HANDLE) }).map(drop)
fn CloseHandle(handle: HANDLE) -> WindowsSysResult<BOOL> {
WindowsSysResult(unsafe { windows_sys::Win32::Foundation::CloseHandle(handle.0) })
}
#[pyfunction]
fn GetStdHandle(std_handle: u32, vm: &VirtualMachine) -> PyResult<usize> {
cvt(vm, unsafe { processenv::GetStdHandle(std_handle) }).map(husize)
fn GetStdHandle(
std_handle: windows_sys::Win32::System::Console::STD_HANDLE,
) -> WindowsSysResult<RAW_HANDLE> {
WindowsSysResult(unsafe { windows_sys::Win32::System::Console::GetStdHandle(std_handle) })
}
#[pyfunction]
@@ -108,51 +140,63 @@ mod _winapi {
_pipe_attrs: PyObjectRef,
size: u32,
vm: &VirtualMachine,
) -> PyResult<(usize, usize)> {
let mut read = null_mut();
let mut write = null_mut();
cvt(vm, unsafe {
namedpipeapi::CreatePipe(&mut read, &mut write, null_mut(), size)
})?;
Ok((read as usize, write as usize))
) -> PyResult<(HANDLE, HANDLE)> {
let (read, write) = unsafe {
let mut read = std::mem::MaybeUninit::<isize>::uninit();
let mut write = std::mem::MaybeUninit::<isize>::uninit();
WindowsSysResult(windows_sys::Win32::System::Pipes::CreatePipe(
read.as_mut_ptr(),
write.as_mut_ptr(),
std::ptr::null(),
size,
))
.to_pyresult(vm)?;
(read.assume_init(), write.assume_init())
};
Ok((HANDLE(read), HANDLE(write)))
}
#[pyfunction]
fn DuplicateHandle(
(src_process, src): (usize, usize),
target_process: usize,
(src_process, src): (HANDLE, HANDLE),
target_process: HANDLE,
access: u32,
inherit: i32,
inherit: BOOL,
options: OptionalArg<u32>,
vm: &VirtualMachine,
) -> PyResult<usize> {
let mut target = null_mut();
cvt(vm, unsafe {
handleapi::DuplicateHandle(
src_process as _,
src as _,
target_process as _,
&mut target,
) -> PyResult<HANDLE> {
let target = unsafe {
let mut target = std::mem::MaybeUninit::<isize>::uninit();
WindowsSysResult(windows_sys::Win32::Foundation::DuplicateHandle(
src_process.0,
src.0,
target_process.0,
target.as_mut_ptr(),
access,
inherit,
options.unwrap_or(0),
)
})?;
Ok(target as usize)
))
.to_pyresult(vm)?;
target.assume_init()
};
Ok(HANDLE(target))
}
#[pyfunction]
fn GetCurrentProcess() -> usize {
unsafe { processthreadsapi::GetCurrentProcess() as usize }
fn GetCurrentProcess() -> HANDLE {
unsafe { windows::Win32::System::Threading::GetCurrentProcess() }
}
#[pyfunction]
fn GetFileType(h: usize, vm: &VirtualMachine) -> PyResult<u32> {
let ret = unsafe { fileapi::GetFileType(h as _) };
if ret == 0 && GetLastError() != 0 {
fn GetFileType(
h: HANDLE,
vm: &VirtualMachine,
) -> PyResult<windows_sys::Win32::Storage::FileSystem::FILE_TYPE> {
let file_type = unsafe { windows_sys::Win32::Storage::FileSystem::GetFileType(h.0) };
if file_type == 0 && GetLastError() != 0 {
Err(errno_err(vm))
} else {
Ok(ret)
Ok(file_type)
}
}
@@ -182,7 +226,7 @@ mod _winapi {
fn CreateProcess(
args: CreateProcessArgs,
vm: &VirtualMachine,
) -> PyResult<(usize, usize, u32, u32)> {
) -> PyResult<(HANDLE, HANDLE, u32, u32)> {
let mut si = winbase::STARTUPINFOEXW::default();
si.StartupInfo.cb = std::mem::size_of_val(&si) as _;
@@ -241,11 +285,11 @@ mod _winapi {
let procinfo = unsafe {
let mut procinfo = std::mem::MaybeUninit::uninit();
let ret = processthreadsapi::CreateProcessW(
WindowsSysResult(windows_sys::Win32::System::Threading::CreateProcessW(
app_name,
command_line,
null_mut(),
null_mut(),
std::ptr::null(),
std::ptr::null(),
args.inherit_handles,
args.creation_flags
| winbase::EXTENDED_STARTUPINFO_PRESENT
@@ -254,16 +298,14 @@ mod _winapi {
current_dir,
&mut si as *mut winbase::STARTUPINFOEXW as _,
procinfo.as_mut_ptr(),
);
if ret == 0 {
return Err(errno_err(vm));
}
))
.into_pyresult(vm)?;
procinfo.assume_init()
};
Ok((
procinfo.hProcess as usize,
procinfo.hThread as usize,
HANDLE(procinfo.hProcess),
HANDLE(procinfo.hThread),
procinfo.dwProcessId,
procinfo.dwThreadId,
))
@@ -310,7 +352,9 @@ mod _winapi {
impl Drop for AttrList {
fn drop(&mut self) {
unsafe {
processthreadsapi::DeleteProcThreadAttributeList(self.attrlist.as_mut_ptr() as _)
windows_sys::Win32::System::Threading::DeleteProcThreadAttributeList(
self.attrlist.as_mut_ptr() as *mut _,
)
};
}
}
@@ -333,49 +377,50 @@ mod _winapi {
.transpose()?;
let attr_count = handlelist.is_some() as u32;
let mut size = 0;
let ret = unsafe {
processthreadsapi::InitializeProcThreadAttributeList(
null_mut(),
attr_count,
0,
&mut size,
)
let (result, mut size) = unsafe {
let mut size = std::mem::MaybeUninit::uninit();
let result = WindowsSysResult(
windows_sys::Win32::System::Threading::InitializeProcThreadAttributeList(
std::ptr::null_mut(),
attr_count,
0,
size.as_mut_ptr(),
),
);
(result, size.assume_init())
};
if ret != 0 || GetLastError() != winerror::ERROR_INSUFFICIENT_BUFFER {
if !result.is_err()
|| GetLastError() != winapi::shared::winerror::ERROR_INSUFFICIENT_BUFFER
{
return Err(errno_err(vm));
}
let mut attrlist = vec![0u8; size];
let ret = unsafe {
processthreadsapi::InitializeProcThreadAttributeList(
attrlist.as_mut_ptr() as _,
WindowsSysResult(unsafe {
windows_sys::Win32::System::Threading::InitializeProcThreadAttributeList(
attrlist.as_mut_ptr() as *mut _,
attr_count,
0,
&mut size,
)
};
if ret == 0 {
return Err(errno_err(vm));
}
})
.into_pyresult(vm)?;
let mut attrs = AttrList {
handlelist,
attrlist,
};
if let Some(ref mut handlelist) = attrs.handlelist {
let ret = unsafe {
processthreadsapi::UpdateProcThreadAttribute(
WindowsSysResult(unsafe {
windows_sys::Win32::System::Threading::UpdateProcThreadAttribute(
attrs.attrlist.as_mut_ptr() as _,
0,
(2 & 0xffff) | 0x20000, // PROC_THREAD_ATTRIBUTE_HANDLE_LIST
handlelist.as_mut_ptr() as _,
(handlelist.len() * std::mem::size_of::<HANDLE>()) as _,
null_mut(),
null_mut(),
std::ptr::null_mut(),
std::ptr::null(),
)
};
if ret == 0 {
return Err(errno_err(vm));
}
})
.into_pyresult(vm)?;
}
Ok(attrs)
})
@@ -383,9 +428,9 @@ mod _winapi {
}
#[pyfunction]
fn WaitForSingleObject(h: usize, ms: u32, vm: &VirtualMachine) -> PyResult<u32> {
let ret = unsafe { synchapi::WaitForSingleObject(h as _, ms) };
if ret == winbase::WAIT_FAILED {
fn WaitForSingleObject(h: HANDLE, ms: u32, vm: &VirtualMachine) -> PyResult<u32> {
let ret = unsafe { windows_sys::Win32::System::Threading::WaitForSingleObject(h.0, ms) };
if ret == windows_sys::Win32::Foundation::WAIT_FAILED {
Err(errno_err(vm))
} else {
Ok(ret)
@@ -393,27 +438,33 @@ mod _winapi {
}
#[pyfunction]
fn GetExitCodeProcess(h: usize, vm: &VirtualMachine) -> PyResult<u32> {
let mut ec = 0;
cvt(vm, unsafe {
processthreadsapi::GetExitCodeProcess(h as _, &mut ec)
})?;
Ok(ec)
fn GetExitCodeProcess(h: HANDLE, vm: &VirtualMachine) -> PyResult<u32> {
unsafe {
let mut ec = std::mem::MaybeUninit::uninit();
WindowsSysResult(windows_sys::Win32::System::Threading::GetExitCodeProcess(
h.0,
ec.as_mut_ptr(),
))
.to_pyresult(vm)?;
Ok(ec.assume_init())
}
}
#[pyfunction]
fn TerminateProcess(h: usize, exit_code: u32, vm: &VirtualMachine) -> PyResult<()> {
cvt(vm, unsafe {
processthreadsapi::TerminateProcess(h as _, exit_code)
fn TerminateProcess(h: HANDLE, exit_code: u32) -> WindowsSysResult<BOOL> {
WindowsSysResult(unsafe {
windows_sys::Win32::System::Threading::TerminateProcess(h.0, exit_code)
})
.map(drop)
}
// TODO: ctypes.LibraryLoader.LoadLibrary
#[allow(dead_code)]
fn LoadLibrary(path: PyStrRef, vm: &VirtualMachine) -> PyResult<isize> {
let path = path.as_str().to_wides_with_nul();
let handle = unsafe { LoadLibraryW(PCWSTR::from_raw(path.as_ptr())).unwrap() };
let handle = unsafe {
windows::Win32::System::LibraryLoader::LoadLibraryW(PCWSTR::from_raw(path.as_ptr()))
.unwrap()
};
if handle.is_invalid() {
return Err(vm.new_runtime_error("LoadLibrary failed".to_owned()));
}
@@ -425,7 +476,8 @@ mod _winapi {
let mut path: Vec<u16> = vec![0; MAX_PATH as usize];
let handle = HINSTANCE(handle);
let length = unsafe { GetModuleFileNameW(handle, &mut path) };
let length =
unsafe { windows::Win32::System::LibraryLoader::GetModuleFileNameW(handle, &mut path) };
if length == 0 {
return Err(vm.new_runtime_error("GetModuleFileName failed".to_owned()));
}