Comments (7)
I made a mistake, the result of CreateProcess is not a window handle.
Indeed, process and window are two completely different things. You cannot forcibly cast one handle into the other.
However, I am still curious about how to manually kill the process
I think you're after TerminateProcess
.
from winsafe.
I also want to know how to convert pi.hProcess to HWND. I would be grateful if you could help.
from winsafe.
I want to close it programmatically
CreateProcess
returns a CloseHandlePiGuard
, which implements Drop
to free the two handles returned in the struct: hProcess
and hThread
.
Any Drop
in Rust can be prevented to run its destructor by passing the object to std::mem::forget
. But then to free the handles manually you must keep them first, then rebuild the guards yourself:
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.
I also want to know how to convert pi.hProcess to HWND.
All handles in WinSafe implement the Handle
trait, which have a few basic operations, including raw pointer conversions:
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?
from winsafe.
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've never seen this conversion before: what's the situation where it's needed?
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.
from winsafe.
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
from winsafe.
It looks great, I hope to use it soon.
from winsafe.
works well in 0.0.15
from winsafe.
Related Issues (20)
- validate_retrieved_reg_val always fails
- [Question] - Calling windows APIs twice best practice HOT 3
- RegQueryValueEx returning TRANSACTION_REQUEST_NOT_VALID for valid data HOT 6
- nm_custom_draw should provide a mutable reference? HOT 6
- How do you envision WM_USER being used? HOT 1
- `WC::NoValue`? HOT 1
- I don't think `MultiByteToWideChar` and `WideCharToMultiByte` need to add 1 to `num_bytes` HOT 4
- resizable_layout not work as expect HOT 8
- When the new_dlg function is used to create a window, the wm_create event does not work HOT 6
- Async IO should be marked as "unsafe" HOT 1
- CreateToolhelp32Snapshot error when compiling for x86
- Panic in wstring/ QueryFullProcessImageName HOT 3
- How to set up a transparent window? HOT 1
- please font settings HOT 7
- Add missing API to open app in foreground? HOT 3
- Possible ShellExecuteEx implementation HOT 8
- Linker errors in Windows 7
- Add support for raw-window-handle HOT 3
- Build fails for the i686-pc-windows-(msvc/gnu) targets HOT 8
- winsvc.h HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from winsafe.