GithubHelp home page GithubHelp logo

How to close a process? about winsafe HOT 7 CLOSED

DaZiYuan avatar DaZiYuan commented on June 2, 2024
How to close a process?

from winsafe.

Comments (7)

rodrigocfd avatar rodrigocfd commented on June 2, 2024 1

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.

DaZiYuan avatar DaZiYuan commented on June 2, 2024

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

from winsafe.

rodrigocfd avatar rodrigocfd commented on June 2, 2024

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.

DaZiYuan avatar DaZiYuan commented on June 2, 2024

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.

DaZiYuan avatar DaZiYuan commented on June 2, 2024

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.

DaZiYuan avatar DaZiYuan commented on June 2, 2024

It looks great, I hope to use it soon.

from winsafe.

DaZiYuan avatar DaZiYuan commented on June 2, 2024

works well in 0.0.15

from winsafe.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.