Skip to content

Commit 3dae07c

Browse files
authored
winapi functions (#7516)
1 parent fddd7cb commit 3dae07c

2 files changed

Lines changed: 73 additions & 0 deletions

File tree

crates/vm/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@ features = [
128128
"Win32_System_Environment",
129129
"Win32_System_IO",
130130
"Win32_System_Ioctl",
131+
"Win32_System_JobObjects",
131132
"Win32_System_Kernel",
132133
"Win32_System_LibraryLoader",
133134
"Win32_System_Memory",

crates/vm/src/stdlib/_winapi.rs

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -615,6 +615,78 @@ mod _winapi {
615615
})
616616
}
617617

618+
#[pyfunction]
619+
fn CreateJobObject(
620+
_security_attributes: PyObjectRef,
621+
name: OptionalArg<Option<PyStrRef>>,
622+
vm: &VirtualMachine,
623+
) -> PyResult<WinHandle> {
624+
let handle = unsafe {
625+
match name.flatten() {
626+
Some(name) => {
627+
let name_wide = name.as_wtf8().to_wide_with_nul();
628+
windows_sys::Win32::System::JobObjects::CreateJobObjectW(
629+
null(),
630+
name_wide.as_ptr(),
631+
)
632+
}
633+
None => windows_sys::Win32::System::JobObjects::CreateJobObjectW(null(), null()),
634+
}
635+
};
636+
if handle.is_null() {
637+
return Err(vm.new_last_os_error());
638+
}
639+
Ok(WinHandle(handle))
640+
}
641+
642+
#[pyfunction]
643+
fn AssignProcessToJobObject(
644+
job: WinHandle,
645+
process: WinHandle,
646+
vm: &VirtualMachine,
647+
) -> PyResult<()> {
648+
let ret = unsafe {
649+
windows_sys::Win32::System::JobObjects::AssignProcessToJobObject(job.0, process.0)
650+
};
651+
if ret == 0 {
652+
return Err(vm.new_last_os_error());
653+
}
654+
Ok(())
655+
}
656+
657+
#[pyfunction]
658+
fn TerminateJobObject(job: WinHandle, exit_code: u32, vm: &VirtualMachine) -> PyResult<()> {
659+
let ret =
660+
unsafe { windows_sys::Win32::System::JobObjects::TerminateJobObject(job.0, exit_code) };
661+
if ret == 0 {
662+
return Err(vm.new_last_os_error());
663+
}
664+
Ok(())
665+
}
666+
667+
#[pyfunction]
668+
fn SetJobObjectKillOnClose(job: WinHandle, vm: &VirtualMachine) -> PyResult<()> {
669+
use windows_sys::Win32::System::JobObjects::{
670+
JOBOBJECT_EXTENDED_LIMIT_INFORMATION, JobObjectExtendedLimitInformation,
671+
SetInformationJobObject,
672+
};
673+
let mut info: JOBOBJECT_EXTENDED_LIMIT_INFORMATION = unsafe { core::mem::zeroed() };
674+
info.BasicLimitInformation.LimitFlags =
675+
windows_sys::Win32::System::JobObjects::JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE;
676+
let ret = unsafe {
677+
SetInformationJobObject(
678+
job.0,
679+
JobObjectExtendedLimitInformation,
680+
&info as *const _ as *const core::ffi::c_void,
681+
core::mem::size_of::<JOBOBJECT_EXTENDED_LIMIT_INFORMATION>() as u32,
682+
)
683+
};
684+
if ret == 0 {
685+
return Err(vm.new_last_os_error());
686+
}
687+
Ok(())
688+
}
689+
618690
#[pyfunction]
619691
fn GetModuleFileName(handle: isize, vm: &VirtualMachine) -> PyResult<String> {
620692
let mut path: Vec<u16> = vec![0; MAX_PATH as usize];

0 commit comments

Comments
 (0)