-
Notifications
You must be signed in to change notification settings - Fork 27
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How to close a process? #72
Comments
I also want to know how to convert pi.hProcess to HWND. I would be grateful if you could help. |
Any use winsafe::{self as w, prelude::*, co};
let mut si = w::STARTUPINFO::default();
let pp = w::HPROCESS::CreateProcess(None, Some("notepad.exe"), None,
None, false, co::CREATE::NoValue, None, None, &mut si).unwrap();
// get copies of the handles
let hprocess = unsafe { pp.hProcess.raw_copy() };
let hthread = unsafe { pp.hThread.raw_copy() };
// prevent the drop of the struct
std::mem::forget(pp);
// manually release the handles
unsafe {
let _ = w::guard::CloseHandleGuard::new(hprocess);
let _ = w::guard::CloseHandleGuard::new(hthread);
} What you trying to do is called manual management of the handles, like we do in C. This is, of course, not recommended. It is always preferable to handle your object lifetime correctly, instead of bypass Rust's security features. Please don't do this.
All handles in WinSafe implement the let hwnd = unsafe { w::HWND::from_ptr( hprocess.as_ptr() ) }; I've never seen this conversion before: what's the situation where it's needed? |
I tried this, but the notepad.exe didn't close successfully. use winsafe::{self as w, prelude::*, co};
let mut si = w::STARTUPINFO::default();
let pp = w::HPROCESS::CreateProcess(None, Some("notepad.exe"), None,
None, false, co::CREATE::NoValue, None, None, &mut si).unwrap();
// get copies of the handles
let hprocess = unsafe { pp.hProcess.raw_copy() };
let hthread = unsafe { pp.hThread.raw_copy() };
// prevent the drop of the struct
std::mem::forget(pp);
// manually release the handles
unsafe {
let _ = w::guard::CloseHandleGuard::new(hprocess);
let _ = w::guard::CloseHandleGuard::new(hthread);
}
I want to create a new Notepad process and obtain its main handle. Then, I want to convert the handle to an HWND to use the SetParent() function. |
I made a mistake, the result of CreateProcess is not a window handle. However, I am still curious about how to manually kill the process |
Indeed, process and window are two completely different things. You cannot forcibly cast one handle into the other.
I think you're after |
It looks great, I hope to use it soon. |
works well in 0.0.15 |
The text was updated successfully, but these errors were encountered: