Skip to content
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

Closed
GiantappMan opened this issue Mar 30, 2023 · 7 comments
Closed

How to close a process? #72

GiantappMan opened this issue Mar 30, 2023 · 7 comments
Assignees
Labels
bindings Something with the low-level WinAPI bidings question Further information is requested

Comments

@GiantappMan
Copy link

        let pi = HPROCESS::CreateProcess(
            None,
            Some("notepad.exe"),
            None,
            None,
            false,
            co::CREATE::NoValue,
            None,
            None,
            &mut startInfo,
        )
        .unwrap();

//       I want to close it programmatically
//        pi.hProcess.CloseHandle().unwrap(); 
@GiantappMan
Copy link
Author

I also want to know how to convert pi.hProcess to HWND. I would be grateful if you could help.

@rodrigocfd rodrigocfd self-assigned this Mar 30, 2023
@rodrigocfd rodrigocfd added question Further information is requested bindings Something with the low-level WinAPI bidings labels Mar 30, 2023
@rodrigocfd
Copy link
Owner

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?

@GiantappMan
Copy link
Author

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.

@GiantappMan
Copy link
Author

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

@rodrigocfd
Copy link
Owner

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.

@GiantappMan
Copy link
Author

It looks great, I hope to use it soon.

@GiantappMan
Copy link
Author

works well in 0.0.15

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bindings Something with the low-level WinAPI bidings question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants