1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297
//! # tasklist
//!
//!
//! `tasklist` is a crate let you easily get tasklist and process information on windows.
//! it based on [`windows-rs`](https://github.com/microsoft/windows-rs) crate.
//!
//! #### what information you can get
//! 1. Process name,pid,parrentID,theradsID.
//! 2. Process start_time,exit_time,and CPU_time(including kernel time and user time).
//! 3. Process path and commandline params.
//! 4. Process SID and Domain/User.
//! 5. Process IO infomation , including all of `IO_COUNTERS` member.
//! 6. Process memory information , including all of `PROCESS_MEMORY_COUNTERS` member.
//! 7. Process handles information , use `GetProcessHandleCount` Api.
//! 8. Process file infomation , use `GetFileVersionInfoExW` Api.
//! 9. Check whether the process is running in the WOW64 environment.
//! 10. Iterate over all processes
//!
//! _remember some infomation need higher privilege in some specific windows versions_
//! ## example
//! Get all process pid , process name and user .
//! ```rust
//! use tasklist;
//! fn main(){
//! unsafe{
//! let tl = tasklist::Tasklist::new();
//! for i in tl{
//! println!("{} {} {}",i.get_pid(),i.get_pname(),i.get_user());
//! }
//! }
//! }
//! ```
//! Get all process name , pid , company name , file description.
//! ```rust
//! use tasklist;
//!
//! fn main(){
//! for i in unsafe{tasklist::Tasklist::new()}{
//! let cpn = match i.get_file_info().get("CompanyName"){
//! Some(h)=>h.to_string(),
//! None=>"".to_string(),
//! };
//! let des = match i.get_file_info().get("FileDescription"){
//! Some(h)=>h.to_string(),
//! None=>"".to_string(),
//! };
//! println!("\t{} \t{} \t{} \t{}",i.get_pname(),i.get_pid(),cpn,des)
//! }
//! }
//!
//! ```
//!
///find the process id by the name you gave , it return a `Vec<U32>` , if the process is not exist , it will return a empty `Vec<u32>`
/// ```
/// unsafe{
/// let aid = tasklist::find_process_id_by_name("cmd.exe");
/// println!("{:#?}",aid);
/// }
/// ```
#[cfg(any(windows, doc))]
pub unsafe fn find_process_id_by_name(process_name:&str)->Vec<u32>{
use std::mem::zeroed;
use windows::Win32::Foundation::CloseHandle;
use std::mem::size_of;
use windows::Win32::System::Diagnostics::ToolHelp::{CreateToolhelp32Snapshot,TH32CS_SNAPPROCESS,PROCESSENTRY32,Process32First,Process32Next};
let mut temp:Vec<u32> = vec![];
let h = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0).unwrap();
let mut process =zeroed::<PROCESSENTRY32>();
process.dwSize= size_of::<PROCESSENTRY32>() as u32;
if Process32First(h,&mut process).as_bool(){
loop{
if Process32Next(h, &mut process).as_bool(){
if get_proc_name(process.szExeFile) == process_name {
temp.push(process.th32ProcessID);
}
}else{
break;
}
}
}
CloseHandle(h);
temp
}
/// return the first process id by the name you gave , it return the `Option<u32>` , `u32` is the process id.
/// ```
/// unsafe{
/// let pid = tasklist::find_first_process_id_by_name("cmd.exe");
/// println!("{:#?}",pid);
/// }
#[cfg(any(windows, doc))]
pub unsafe fn find_first_process_id_by_name(process_name:&str)->Option<u32>{
use std::mem::zeroed;
use windows::Win32::Foundation::CloseHandle;
use std::mem::size_of;
use windows::Win32::System::Diagnostics::ToolHelp::{CreateToolhelp32Snapshot,TH32CS_SNAPPROCESS,PROCESSENTRY32,Process32First,Process32Next};
let h = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0).unwrap();
let mut process =zeroed::<PROCESSENTRY32>();
process.dwSize= size_of::<PROCESSENTRY32>() as u32;
if Process32First(h,&mut process).as_bool(){
loop{
if Process32Next(h, &mut process).as_bool(){
if get_proc_name(process.szExeFile) == process_name {
break;
}
}else{
return None;
}
}
}
CloseHandle(h);
Some(process.th32ProcessID)
}
/// just like the name , this function will return a `Option<String>` by the id you gave, `String` is the name of process.
/// ```
/// unsafe{
/// let pname = tasklist::find_process_name_by_id(9720);
/// println!("{:#?}",pname);
/// }
///
/// ```
#[cfg(any(windows, doc))]
pub unsafe fn find_process_name_by_id(process_id:u32)->Option<String>{
use std::mem::zeroed;
use windows::Win32::Foundation::CloseHandle;
use std::mem::size_of;
use windows::Win32::System::Diagnostics::ToolHelp::{CreateToolhelp32Snapshot,TH32CS_SNAPPROCESS,PROCESSENTRY32,Process32First,Process32Next};
let h = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0).unwrap();
let mut process =zeroed::<PROCESSENTRY32>();
process.dwSize= size_of::<PROCESSENTRY32>() as u32;
if Process32First(h,&mut process).as_bool(){
loop{
if Process32Next(h, &mut process).as_bool(){
let id:u32 = process.th32ProcessID;
if id == process_id {
break;
}
}else{
return None;
}
}
}
CloseHandle(h);
Some(get_proc_name(process.szExeFile))
}
use std::collections::HashMap;
/// get the windows tasklist ,return a `HashMap<String,u32>`
/// `String` is the name of process, and `u32` is the id of process
/// ```
/// unsafe{
/// let list = tasklist::tasklist();
/// println!("{:#?}",list);
/// }
/// ```
#[cfg(any(windows, doc))]
pub unsafe fn tasklist()->HashMap<String,u32>{
use std::{mem::zeroed};
use windows::Win32::Foundation::CloseHandle;
use std::mem::size_of;
use windows::Win32::System::Diagnostics::ToolHelp::{CreateToolhelp32Snapshot,TH32CS_SNAPPROCESS,PROCESSENTRY32,Process32First,Process32Next};
let mut temp:HashMap<String, u32> = HashMap::new();
let h = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0).unwrap();
let mut process =zeroed::<PROCESSENTRY32>();
process.dwSize= size_of::<PROCESSENTRY32>() as u32;
if Process32First(h,&mut process).as_bool(){
loop{
if Process32Next(h, &mut process).as_bool(){
temp.insert(get_proc_name(process.szExeFile), process.th32ProcessID.try_into().unwrap());
}else{
break;
}
}
}
CloseHandle(h);
temp
}
///get the proc name by windows `[CHAR;260]` , retun the `String` name for human.
#[cfg(any(windows, doc))]
fn get_proc_name(name:[windows::Win32::Foundation::CHAR;260])->String{
let mut temp:Vec<u8> = vec![];
let len = name.iter().position(|&x| x == windows::Win32::Foundation::CHAR(0)).unwrap();
for i in name.iter(){
temp.push(i.0.clone());
}
let s = String::from_utf8(temp[0..len].to_vec()).unwrap();
s
}
/// enbale the debug privilege for your program , it return a `bool` to show if it success.
/// ```
/// println!("open the debug priv{:?}",tasklist::enable_debug_priv());
/// ```
pub unsafe fn enable_debug_priv()->bool{
use std::ptr::null_mut;
use std::mem::size_of;
use windows::core::PCSTR;
use windows::Win32::System::Threading::OpenProcessToken;
use windows::Win32::Foundation::{HANDLE,BOOL,LUID,CloseHandle};
use windows::Win32::System::Threading::{GetCurrentProcess};
use windows::Win32::Security::{TOKEN_ADJUST_PRIVILEGES,TOKEN_QUERY,LUID_AND_ATTRIBUTES,SE_PRIVILEGE_ENABLED,TOKEN_PRIVILEGES,LookupPrivilegeValueA,AdjustTokenPrivileges};
let mut h:HANDLE = HANDLE(0);
OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES|TOKEN_QUERY, &mut h);
let la = LUID_AND_ATTRIBUTES{ Luid: LUID{ LowPart: 0, HighPart: 0 }, Attributes: SE_PRIVILEGE_ENABLED };
let mut tp = TOKEN_PRIVILEGES{ PrivilegeCount: 1, Privileges: [la] };
let privilege = "SeDebugPrivilege\0";
if LookupPrivilegeValueA(PCSTR(null_mut()),PCSTR(privilege.as_ptr()),&mut tp.Privileges[0].Luid).as_bool(){
if AdjustTokenPrivileges(h, BOOL(0), &mut tp,size_of::<TOKEN_PRIVILEGES>() as _ ,0 as _ , 0 as _).as_bool(){
CloseHandle(h);
return true;
}else{
CloseHandle(h);
return false
}
}else{
CloseHandle(h);
return false
}
}
///kill a process by process_id . if success , it will return `true`
/// ```
/// unsafe{
/// let pid = tasklist::find_process_id_by_name("cmd.exe");
/// let pid = pid[0];
/// println!("{:#?}",tasklist::kill(pid));
/// }
///
/// ```
pub unsafe fn kill(pid:u32)->bool{
use windows::Win32::System::Threading::{OpenProcess,PROCESS_TERMINATE,TerminateProcess};
use windows::Win32::Foundation::{BOOL,CloseHandle};
let _ = match OpenProcess(PROCESS_TERMINATE, BOOL(0), pid){
Ok(h) => {
if TerminateProcess(h, 0).as_bool(){
CloseHandle(h);
return true;
}else{
CloseHandle(h);
return false;
}
},
Err(_) => return false,
};
}
//load infos::info
pub mod infos;
#[doc(inline)]
pub use infos::{Process,Tasklist,IoCounter,MemoryCounter};
#[doc(inline)]
pub use infos::info::*;
#[doc(inline)]
pub use infos::info;