Skip to content

Commit

Permalink
chore: replace some ntapi usage by windows-rs
Browse files Browse the repository at this point in the history
  • Loading branch information
poliorcetics committed Sep 21, 2023
1 parent 29d66fa commit 048d52c
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 21 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ once_cell = "1.0"
ntapi = "0.4"
windows = { version = "0.51", features = [
"Wdk_System_SystemInformation",
"Wdk_System_SystemServices",
"Wdk_System_Threading",
"Win32_Foundation",
"Win32_NetworkManagement_IpHelper",
Expand Down
31 changes: 10 additions & 21 deletions src/windows/process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,14 @@ use std::str;
use std::sync::Arc;

use libc::c_void;

use ntapi::ntpebteb::PEB;
use ntapi::ntrtl::RTL_USER_PROCESS_PARAMETERS;
use ntapi::ntwow64::{PEB32, RTL_USER_PROCESS_PARAMETERS32};
use once_cell::sync::Lazy;

use ntapi::ntpsapi::{ProcessCommandLineInformation, PROCESS_BASIC_INFORMATION};
use ntapi::ntrtl::{RtlGetVersion, RTL_USER_PROCESS_PARAMETERS};
use windows::core::PCWSTR;
use windows::Wdk::System::SystemServices::RtlGetVersion;
use windows::Wdk::System::Threading::{
NtQueryInformationProcess, ProcessBasicInformation, ProcessWow64Information, PROCESSINFOCLASS,
NtQueryInformationProcess, ProcessBasicInformation, ProcessCommandLineInformation,
ProcessWow64Information, PROCESSINFOCLASS,
};
use windows::Win32::Foundation::{
CloseHandle, LocalFree, ERROR_INSUFFICIENT_BUFFER, FILETIME, HANDLE, HINSTANCE, HLOCAL,
Expand All @@ -48,16 +46,11 @@ use windows::Win32::System::RemoteDesktop::ProcessIdToSessionId;
use windows::Win32::System::SystemInformation::OSVERSIONINFOEXW;
use windows::Win32::System::Threading::{
GetProcessIoCounters, GetProcessTimes, GetSystemTimes, OpenProcess, OpenProcessToken,
CREATE_NO_WINDOW, IO_COUNTERS, PROCESS_QUERY_INFORMATION, PROCESS_QUERY_LIMITED_INFORMATION,
PROCESS_VM_READ,
CREATE_NO_WINDOW, IO_COUNTERS, PEB, PROCESS_BASIC_INFORMATION, PROCESS_QUERY_INFORMATION,
PROCESS_QUERY_LIMITED_INFORMATION, PROCESS_VM_READ,
};
use windows::Win32::UI::Shell::CommandLineToArgvW;

#[inline]
const fn is_failed(st: i32) -> bool {
st < 0
}

impl fmt::Display for ProcessStatus {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.write_str(match *self {
Expand Down Expand Up @@ -231,9 +224,7 @@ static WINDOWS_8_1_OR_NEWER: Lazy<bool> = Lazy::new(|| unsafe {
let mut version_info: OSVERSIONINFOEXW = MaybeUninit::zeroed().assume_init();

version_info.dwOSVersionInfoSize = std::mem::size_of::<OSVERSIONINFOEXW>() as u32;
if is_failed(RtlGetVersion(
(&mut version_info as *mut OSVERSIONINFOEXW).cast(),
)) {
if RtlGetVersion((&mut version_info as *mut OSVERSIONINFOEXW).cast()).is_err() {
return true;
}

Expand Down Expand Up @@ -312,7 +303,7 @@ impl Process {
}
};
let (start_time, run_time) = get_start_and_run_time(*process_handler, now);
let parent = if info.InheritedFromUniqueProcessId as usize != 0 {
let parent = if info.InheritedFromUniqueProcessId != 0 {
Some(Pid(info.InheritedFromUniqueProcessId as _))
} else {
None
Expand Down Expand Up @@ -889,10 +880,8 @@ fn get_cmd_line_old<T: RtlUserProcessParameters>(
#[allow(clippy::cast_ptr_alignment)]
fn get_cmd_line_new(handle: &HandleWrapper) -> Vec<String> {
unsafe {
if let Some(buffer) = ph_query_process_variable_size(
handle,
PROCESSINFOCLASS(ProcessCommandLineInformation as _),
) {
if let Some(buffer) = ph_query_process_variable_size(handle, ProcessCommandLineInformation)
{
let buffer = (*(buffer.as_ptr() as *const UNICODE_STRING)).Buffer;

get_cmdline_from_buffer(PCWSTR::from_raw(buffer.as_ptr()))
Expand Down

0 comments on commit 048d52c

Please sign in to comment.